🚧 basic ranking by symbol intersection similarity
This commit is contained in:
parent
da6470762a
commit
64a346dcac
3 changed files with 74 additions and 37 deletions
|
|
@ -10,6 +10,7 @@ import { getBasename } from "../util";
|
|||
|
||||
import { getAst, getScopeAroundRange, getTreePathAtCursor } from "./ast";
|
||||
import { AutocompleteLanguageInfo, LANGUAGES, Typescript } from "./languages";
|
||||
import { rankSnippets } from "./ranking";
|
||||
import { slidingWindowMatcher } from "./slidingWindow";
|
||||
|
||||
export function languageForFilepath(
|
||||
|
|
@ -82,7 +83,7 @@ export async function constructAutocompletePrompt(
|
|||
completeMultiline: boolean;
|
||||
}> {
|
||||
// Find external snippets
|
||||
const snippets: FileWithContents[] = (await Promise.all(
|
||||
let snippets: FileWithContents[] = (await Promise.all(
|
||||
recentlyEditedRanges
|
||||
.map(async (r) => {
|
||||
const scope = await getScopeAroundRange(r);
|
||||
|
|
@ -145,6 +146,17 @@ export async function constructAutocompletePrompt(
|
|||
completeMultiline = shouldCompleteMultiline(treePath, cursorLine);
|
||||
}
|
||||
|
||||
// Rank / order the snippets
|
||||
snippets = rankSnippets(
|
||||
snippets,
|
||||
fullPrefix,
|
||||
fullSuffix,
|
||||
options.slidingWindowPrefixPercentage,
|
||||
options.slidingWindowSize
|
||||
);
|
||||
|
||||
// How to add snippets to the prefix? Count separately? Always keep some of the prefix??
|
||||
|
||||
// Construct basic prefix / suffix
|
||||
const formattedSnippets = snippets
|
||||
.map((snippet) =>
|
||||
|
|
|
|||
59
core/autocomplete/ranking.ts
Normal file
59
core/autocomplete/ranking.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { FileWithContents } from "..";
|
||||
|
||||
function getSymbolsForSnippet(snippet: string): Set<string> {
|
||||
const symbols = snippet
|
||||
.split(
|
||||
new RegExp(
|
||||
`[s\.\:\'\,\;\"\*\&\^\|\\\#\@\$\!\%\(\)\[\]\{\}\<\>\/\?\+\-\/]`,
|
||||
"g"
|
||||
)
|
||||
)
|
||||
.map((s) => s.trim());
|
||||
return new Set(symbols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate similarity as number of shared symbols divided by total number of unique symbols between both.
|
||||
*/
|
||||
export function symbolSimilarity(a: string, b: string): number {
|
||||
const aSet = getSymbolsForSnippet(a);
|
||||
const bSet = getSymbolsForSnippet(b);
|
||||
const totalSet = new Set([...aSet, ...bSet]);
|
||||
|
||||
// Avoid division by zero
|
||||
if (totalSet.size === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let intersection = 0;
|
||||
for (const symbol of aSet) {
|
||||
if (bSet.has(symbol)) {
|
||||
intersection++;
|
||||
}
|
||||
}
|
||||
|
||||
return intersection / totalSet.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rank code snippets to be used in tab-autocomplete prompt. Returns a sorted version of the snippet array.
|
||||
*/
|
||||
export function rankSnippets(
|
||||
snippets: FileWithContents[],
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
slidingWindowPrefixPercentage: number,
|
||||
windowSize: number
|
||||
): FileWithContents[] {
|
||||
const scores = snippets.map((snippet) => {
|
||||
const b =
|
||||
prefix.slice(-windowSize * slidingWindowPrefixPercentage) +
|
||||
suffix.slice(0, windowSize * (1 - slidingWindowPrefixPercentage));
|
||||
|
||||
return {
|
||||
score: symbolSimilarity(snippet.contents, b),
|
||||
snippet,
|
||||
};
|
||||
});
|
||||
return scores.sort((a, b) => a.score - b.score).map((s) => s.snippet);
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { FileWithContents } from "..";
|
||||
import { symbolSimilarity } from "./ranking";
|
||||
|
||||
function* slidingWindow(
|
||||
content: string,
|
||||
|
|
@ -24,41 +25,6 @@ function* slidingWindow(
|
|||
}
|
||||
}
|
||||
|
||||
function getSymbolsForSnippet(snippet: string): Set<string> {
|
||||
const symbols = snippet
|
||||
.split(
|
||||
new RegExp(
|
||||
`[s\.\:\'\,\;\"\*\&\^\|\\\#\@\$\!\%\(\)\[\]\{\}\<\>\/\?\+\-\/]`,
|
||||
"g"
|
||||
)
|
||||
)
|
||||
.map((s) => s.trim());
|
||||
return new Set(symbols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate similarity as number of shared symbols divided by total number of unique symbols between both.
|
||||
*/
|
||||
function slidingWindowSimilarity(a: string, b: string): number {
|
||||
const aSet = getSymbolsForSnippet(a);
|
||||
const bSet = getSymbolsForSnippet(b);
|
||||
const totalSet = new Set([...aSet, ...bSet]);
|
||||
|
||||
// Avoid division by zero
|
||||
if (totalSet.size === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let intersection = 0;
|
||||
for (const symbol of aSet) {
|
||||
if (bSet.has(symbol)) {
|
||||
intersection++;
|
||||
}
|
||||
}
|
||||
|
||||
return intersection / totalSet.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match by similarity over sliding windows of recent documents.
|
||||
* @param recentDocuments
|
||||
|
|
@ -78,7 +44,7 @@ export async function slidingWindowMatcher(
|
|||
|
||||
for (const { filepath, contents } of recentDocuments) {
|
||||
for (const window of slidingWindow(contents, windowSize)) {
|
||||
const similarity = slidingWindowSimilarity(
|
||||
const similarity = symbolSimilarity(
|
||||
window,
|
||||
prefix.slice(-windowSize * slidingWindowPrefixPercentage) +
|
||||
suffix.slice(windowSize * (1 - slidingWindowPrefixPercentage))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue