diff --git a/src/lib/domain/network-search.ts b/src/lib/domain/network-search.ts index ffd5a3f..2b9e8fd 100644 --- a/src/lib/domain/network-search.ts +++ b/src/lib/domain/network-search.ts @@ -244,6 +244,8 @@ function tokenVariants(value: string) { return Array.from(variants).filter(Boolean); } +const PHRASE_END = /\b(who|that|with|and|or|about|last|this|where|when|working|raised|met|emailed|never|not)\b/i; + function extractAfterPhrases(query: string, phrases: string[]) { const values: string[] = []; for (const phrase of phrases) { @@ -252,7 +254,7 @@ function extractAfterPhrases(query: string, phrases: string[]) { const match = query.match(regex); if (match?.[1]) { const cleaned = match[1] - .split(/\b(who|that|with|and|or|about|last|this|where|when|working|raised|met|emailed|never|not)\b/i)[0] + .split(PHRASE_END)[0] .replace(/[?.!,]+$/g, "") .trim(); if (cleaned) values.push(cleaned); @@ -261,6 +263,27 @@ function extractAfterPhrases(query: string, phrases: string[]) { return values; } +// "working in climate" and "investors in fintech" name a sector, not a place. +const SECTOR_WORDS = "work|works|worked|working|invest|invests|invested|investing|investor|investors|interested|focus|focused|focusing|specialize|specialized|specializing|experience|expertise"; +const SECTOR_BEFORE_IN = new RegExp(`\\b(?:${SECTOR_WORDS})\\s+$`); +const SECTOR_WORD_AT_END = new RegExp(`\\s*\\b(?:${SECTOR_WORDS})$`); + +function placesAfterPrepositions(text: string) { + const values: string[] = []; + // The lookahead lets every "in" or "near" be read, also when one follows another. + for (const match of text.matchAll(/\b(in|near)\s+(?=([a-z0-9][a-z0-9 .&+-]{1,80}))/gi)) { + if (match[1]!.toLowerCase() === "in" && SECTOR_BEFORE_IN.test(text.slice(0, match.index))) continue; + const place = match[2]! + .split(PHRASE_END)[0]! + .split(/\b(?:in|near)\b/i)[0]! + .replace(/[?.!,]+$/g, "") + .trim() + .replace(SECTOR_WORD_AT_END, ""); + if (place && place.length <= 40) values.push(place); + } + return values; +} + function parseDateRange(query: string, manual?: string, now = new Date()) { const value = (manual || query).toLowerCase(); const year = now.getUTCFullYear(); @@ -322,7 +345,7 @@ export function parseNetworkObjective(input: z.infer text.includes(geo)), - ...extractAfterPhrases(text, ["in", "near"]).filter((value) => value.length <= 40), + ...placesAfterPrepositions(text), ]); const institutions = unique([ text.includes("berkeley") ? "Berkeley" : null, diff --git a/tests/network-search.test.ts b/tests/network-search.test.ts index 312fb6e..8c6b5f3 100644 --- a/tests/network-search.test.ts +++ b/tests/network-search.test.ts @@ -124,6 +124,20 @@ describe("general network search", () => { expect(parsed.topics).not.toContain("Ukraine"); }); + it("reads a sector after working in or investors in as a topic, not a geography", () => { + const climate = parseNetworkObjective({ query: "Find founders working in climate in Berlin", strictness: "balanced" }, now); + expect(climate.geographies).toEqual(["berlin"]); + expect(climate.topics).toContain("climate"); + + const fintech = parseNetworkObjective({ query: "Find investors in fintech", strictness: "balanced" }, now); + expect(fintech.geographies).toEqual([]); + expect(fintech.topics).toContain("fintech"); + + const kyiv = parseNetworkObjective({ query: "Find founders in Kyiv interested in robotics", strictness: "balanced" }, now); + expect(kyiv.geographies).toEqual(["kyiv"]); + expect(kyiv.topics).toContain("robotics"); + }); + it("supports recency and no-follow-up conditions", () => { const results = search("Find founders I met but never followed up with", [ candidate({