Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/lib/domain/network-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -322,7 +345,7 @@ export function parseNetworkObjective(input: z.infer<typeof networkSearchRequest
const geographies = unique([
...manual.geographies,
...COMMON_GEOS.filter((geo) => text.includes(geo)),
...extractAfterPhrases(text, ["in", "near"]).filter((value) => value.length <= 40),
...placesAfterPrepositions(text),
]);
const institutions = unique([
text.includes("berkeley") ? "Berkeley" : null,
Expand Down
14 changes: 14 additions & 0 deletions tests/network-search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading