Skip to content

Commit

Permalink
Match improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshma committed Feb 4, 2025
1 parent cf7c7b0 commit b12552e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
43 changes: 21 additions & 22 deletions ts/packages/knowPro/src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Match<T = any> {
value: T;
score: number;
hitCount: number;
exactMatch: boolean;
}

/**
Expand Down Expand Up @@ -72,16 +73,22 @@ export class MatchAccumulator<T = any> {
}
}

public add(value: T, score: number): void {
public add(value: T, score: number, isNewMatchExact: boolean): void {
let match = this.matches.get(value);
if (match !== undefined) {
match.hitCount += 1;
if (match) {
// Increment the existing match
if (isNewMatchExact) {
match.hitCount += 1;
match.exactMatch = true;
}
match.score += score;
} else {
// New match
match = {
value,
score,
hitCount: 1,
exactMatch: isNewMatchExact,
};
this.matches.set(value, match);
}
Expand All @@ -94,7 +101,11 @@ export class MatchAccumulator<T = any> {
for (const otherMatch of other.matches.values()) {
const existingMatch = this.matches.get(otherMatch.value);
if (existingMatch) {
existingMatch.hitCount += otherMatch.hitCount;
if (otherMatch.exactMatch) {
existingMatch.hitCount += otherMatch.hitCount;
} else if (existingMatch.hitCount < otherMatch.hitCount) {
existingMatch.hitCount = otherMatch.hitCount;
}
existingMatch.score += otherMatch.score;
if (existingMatch.hitCount > this.maxHitCount) {
this.maxHitCount = existingMatch.hitCount;
Expand Down Expand Up @@ -191,7 +202,11 @@ export class SemanticRefAccumulator extends MatchAccumulator<SemanticRefIndex> {
if (semanticRefs) {
scoreBoost ??= searchTerm.score ?? 0;
for (const match of semanticRefs) {
this.add(match.semanticRefIndex, match.score + scoreBoost);
this.add(
match.semanticRefIndex,
match.score + scoreBoost,
true,
);
}
this.searchTermMatches.add(searchTerm.text);
}
Expand Down Expand Up @@ -222,6 +237,7 @@ export class SemanticRefAccumulator extends MatchAccumulator<SemanticRefIndex> {
value: semanticRef.semanticRefIndex,
score,
hitCount: 1,
exactMatch: false,
};
this.setMatch(match);
}
Expand Down Expand Up @@ -327,23 +343,6 @@ export class SemanticRefAccumulator extends MatchAccumulator<SemanticRefIndex> {
this.clearMatches();
}

public intersect(other: SemanticRefAccumulator): SemanticRefAccumulator {
const intersection = new SemanticRefAccumulator();
let to = this.size > other.size ? this : other;
let from = this.size > other.size ? other : this;
for (const matchFrom of from.getMatches()) {
const matchTo = to.getMatch(matchFrom.value);
if (matchTo !== undefined) {
intersection.setMatch({
hitCount: matchFrom.hitCount + matchTo.hitCount,
score: matchFrom.score + matchTo.score,
value: matchFrom.value,
});
}
}
return intersection;
}

public toScoredSemanticRefs(): ScoredSemanticRef[] {
return this.getSortedByScore(0).map((m) => {
return {
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/knowPro/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export class MatchQualifiedSearchTermExpr extends QueryOpExpr<
facetSearchTerm.facetValue,
);
if (valueMatches.size > 0) {
facetMatches = facetMatches.intersect(valueMatches);
facetMatches.addUnion(valueMatches);
}
}

Expand Down

0 comments on commit b12552e

Please sign in to comment.