Spaces:
Running
Running
Improve subset merge detection for short semantic variants
Browse files- lib/idea-dedup.ts +83 -9
lib/idea-dedup.ts
CHANGED
|
@@ -2,17 +2,86 @@ const STOP_WORDS = new Set([
|
|
| 2 |
"the", "and", "for", "with", "that", "this", "from", "into", "your", "about", "would", "should", "could", "their", "there",
|
| 3 |
"have", "has", "had", "are", "was", "were", "will", "can", "our", "you", "they", "them", "but", "not", "use", "using",
|
| 4 |
"app", "platform", "idea", "build", "make", "need", "want", "users", "user", "solution", "project", "feature",
|
|
|
|
| 5 |
]);
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
function tokenize(value: string): Set<string> {
|
| 8 |
-
const
|
| 9 |
.toLowerCase()
|
| 10 |
.replace(/[^a-z0-9\s]/g, " ")
|
| 11 |
.split(/\s+/)
|
| 12 |
-
.map((
|
| 13 |
-
.filter((
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
function intersectionSize(a: Set<string>, b: Set<string>): number {
|
|
@@ -35,13 +104,13 @@ export function detectMergeTarget(
|
|
| 35 |
candidates: DedupCandidate[]
|
| 36 |
): { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null {
|
| 37 |
const inputTokens = tokenize(inputText);
|
| 38 |
-
if (inputTokens.size <
|
| 39 |
|
| 40 |
let best: { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null = null;
|
| 41 |
|
| 42 |
for (const candidate of candidates) {
|
| 43 |
const candidateTokens = tokenize(candidate.text);
|
| 44 |
-
if (candidateTokens.size <
|
| 45 |
|
| 46 |
const overlap = intersectionSize(inputTokens, candidateTokens);
|
| 47 |
if (overlap === 0) continue;
|
|
@@ -56,9 +125,14 @@ export function detectMergeTarget(
|
|
| 56 |
if (jaccard >= 0.62) {
|
| 57 |
reason = "DUPLICATE";
|
| 58 |
score = jaccard;
|
| 59 |
-
} else
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
if (!reason) continue;
|
|
|
|
| 2 |
"the", "and", "for", "with", "that", "this", "from", "into", "your", "about", "would", "should", "could", "their", "there",
|
| 3 |
"have", "has", "had", "are", "was", "were", "will", "can", "our", "you", "they", "them", "but", "not", "use", "using",
|
| 4 |
"app", "platform", "idea", "build", "make", "need", "want", "users", "user", "solution", "project", "feature",
|
| 5 |
+
"good", "great", "best", "get", "let", "please", "just",
|
| 6 |
]);
|
| 7 |
|
| 8 |
+
const SYNONYMS: Record<string, string> = {
|
| 9 |
+
notified: "notify",
|
| 10 |
+
notification: "notify",
|
| 11 |
+
notifications: "notify",
|
| 12 |
+
alert: "notify",
|
| 13 |
+
alerts: "notify",
|
| 14 |
+
deal: "deal",
|
| 15 |
+
deals: "deal",
|
| 16 |
+
offer: "deal",
|
| 17 |
+
offers: "deal",
|
| 18 |
+
discount: "deal",
|
| 19 |
+
discounts: "deal",
|
| 20 |
+
promo: "deal",
|
| 21 |
+
promos: "deal",
|
| 22 |
+
promotion: "deal",
|
| 23 |
+
promotions: "deal",
|
| 24 |
+
trip: "travel",
|
| 25 |
+
trips: "travel",
|
| 26 |
+
traveling: "travel",
|
| 27 |
+
traveller: "travel",
|
| 28 |
+
travellers: "travel",
|
| 29 |
+
traveler: "travel",
|
| 30 |
+
travelers: "travel",
|
| 31 |
+
discover: "find",
|
| 32 |
+
search: "find",
|
| 33 |
+
scan: "find",
|
| 34 |
+
track: "find",
|
| 35 |
+
rewards: "points",
|
| 36 |
+
reward: "points",
|
| 37 |
+
miles: "points",
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
function normalizeToken(raw: string): string {
|
| 41 |
+
let token = raw.toLowerCase().trim();
|
| 42 |
+
if (!token) return "";
|
| 43 |
+
|
| 44 |
+
if (SYNONYMS[token]) {
|
| 45 |
+
token = SYNONYMS[token];
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
if (token.endsWith("ies") && token.length > 4) {
|
| 49 |
+
token = `${token.slice(0, -3)}y`;
|
| 50 |
+
} else if (token.endsWith("ing") && token.length > 5) {
|
| 51 |
+
token = token.slice(0, -3);
|
| 52 |
+
} else if (token.endsWith("ed") && token.length > 4) {
|
| 53 |
+
token = token.slice(0, -2);
|
| 54 |
+
} else if (token.endsWith("s") && token.length > 3) {
|
| 55 |
+
token = token.slice(0, -1);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
if (SYNONYMS[token]) {
|
| 59 |
+
token = SYNONYMS[token];
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
return token;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
function tokenize(value: string): Set<string> {
|
| 66 |
+
const words = value
|
| 67 |
.toLowerCase()
|
| 68 |
.replace(/[^a-z0-9\s]/g, " ")
|
| 69 |
.split(/\s+/)
|
| 70 |
+
.map((word) => normalizeToken(word))
|
| 71 |
+
.filter((word) => word.length >= 3 && !STOP_WORDS.has(word));
|
| 72 |
+
|
| 73 |
+
const tokens = new Set(words);
|
| 74 |
|
| 75 |
+
// Bigrams improve semantic matching for short prompts like "travel deal".
|
| 76 |
+
for (let i = 0; i < words.length - 1; i += 1) {
|
| 77 |
+
const a = words[i];
|
| 78 |
+
const b = words[i + 1];
|
| 79 |
+
if (a && b) {
|
| 80 |
+
tokens.add(`${a}_${b}`);
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return tokens;
|
| 85 |
}
|
| 86 |
|
| 87 |
function intersectionSize(a: Set<string>, b: Set<string>): number {
|
|
|
|
| 104 |
candidates: DedupCandidate[]
|
| 105 |
): { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null {
|
| 106 |
const inputTokens = tokenize(inputText);
|
| 107 |
+
if (inputTokens.size < 3) return null;
|
| 108 |
|
| 109 |
let best: { targetIdeaId: string; reason: MergeReason; similarityScore: number } | null = null;
|
| 110 |
|
| 111 |
for (const candidate of candidates) {
|
| 112 |
const candidateTokens = tokenize(candidate.text);
|
| 113 |
+
if (candidateTokens.size < 3) continue;
|
| 114 |
|
| 115 |
const overlap = intersectionSize(inputTokens, candidateTokens);
|
| 116 |
if (overlap === 0) continue;
|
|
|
|
| 125 |
if (jaccard >= 0.62) {
|
| 126 |
reason = "DUPLICATE";
|
| 127 |
score = jaccard;
|
| 128 |
+
} else {
|
| 129 |
+
const shortInputSubset = inputTokens.size <= 8 && overlap >= 2 && newCoveredByExisting >= 0.5 && jaccard >= 0.18;
|
| 130 |
+
const normalSubset = overlap >= 3 && newCoveredByExisting >= 0.68 && jaccard >= 0.25;
|
| 131 |
+
|
| 132 |
+
if (shortInputSubset || normalSubset) {
|
| 133 |
+
reason = "SUBSET";
|
| 134 |
+
score = Math.max(newCoveredByExisting, jaccard);
|
| 135 |
+
}
|
| 136 |
}
|
| 137 |
|
| 138 |
if (!reason) continue;
|