tfIdf.ts ×8

Frontier kind: Code frontier

unlabeled · c_67114f016586

10 tests · 4505 LOC · 23 files · introduces 0 tests · 127 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
8 ranges127 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
619 ranges4505 lines · 23 files · Browse complete extent
All tests (intent)
10 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Introduced files, introduced tests, and structurally relevant concept specializationtfIdf.ts ×1 · 2 introduced LOCtfIdf.ts ×1tfIdf.ts ×1 · 18 introduced LOCtfIdf.ts ×1tfIdf.ts ×2 · 5 introduced LOCtfIdf.ts ×2tfIdf.test|title=TF-IDF Calculator Should weigh less common term higher|occurrence=1, tfIdf.test|title=TF-IDF Calculator Should weigh chunks with less common terms higher|occurrence=1 · 0 introduced LOCtfIdf.test|title=TF-IDF …tfIdf.ts ×1 · 3 introduced LOCtfIdf.ts ×1tfIdf.ts ×5 · 17 introduced LOCtfIdf.ts ×5tfIdf.test|title=TF-IDF Calculator Should return no scores for term not in document|occurrence=1 · 0 introduced LOCtfIdf.test|title=TF-IDF …tfIdf.ts ×8 · 48 introduced LOCtfIdf.ts ×8tfIdf.ts ×1 · 1 introduced LOCtfIdf.ts ×1cancellation.test|title=CancellationToken None|occurrence=1 · 0 introduced LOCcancellation.test|title=…cancellation.ts ×18 · 90 introduced LOCcancellation.ts ×18event.ts ×93 · 864 introduced LOCevent.ts ×93uri.test|title=URI URI#file, win-speciale|occurrence=1, charCode.test|title=CharCode has good values|occurrence=1, +2 · 0 introduced LOCuri.test|title=URI URI#f…utils.ts ×3 · 9 introduced LOCutils.ts ×3linkedList.ts ×13 · 44 introduced LOClinkedList.ts ×13lifecycle.ts ×2 · 4 introduced LOClifecycle.ts ×2lifecycle.ts ×1 · 2 introduced LOClifecycle.ts ×1lifecycle.ts ×4 · 8 introduced LOClifecycle.ts ×4lifecycle.ts ×6 · 16 introduced LOClifecycle.ts ×6lifecycle.ts ×1 · 2 introduced LOClifecycle.ts ×1lifecycle.ts ×1 · 2 introduced LOClifecycle.ts ×1lifecycle.ts ×1 · 3 introduced LOClifecycle.ts ×1map.ts ×97 · 3334 introduced LOCmap.ts ×97src/vs/base/common/arrays.ts · 949 LOCcommon/arrays.tssrc/vs/base/common/arraysFind.ts · 226 LOCcommon/arraysFind.tssrc/vs/base/common/assert.ts · 91 LOCcommon/assert.tssrc/vs/base/common/cancellation.ts · 206 LOCcommon/cancellation.tssrc/vs/base/common/charCode.ts · 450 LOCcommon/charCode.tssrc/vs/base/common/collections.ts · 176 LOCcommon/collections.tssrc/vs/base/common/errors.ts · 357 LOCcommon/errors.tssrc/vs/base/common/event.ts · 1964 LOCcommon/event.tssrc/vs/base/common/functional.ts · 32 LOCcommon/functional.tssrc/vs/base/common/iterator.ts · 194 LOCcommon/iterator.tssrc/vs/base/common/lifecycle.ts · 974 LOCcommon/lifecycle.tssrc/vs/base/common/linkedList.ts · 151 LOCcommon/linkedList.tssrc/vs/base/common/map.ts · 1016 LOCcommon/map.tssrc/vs/base/common/marshallingIds.ts · 33 LOCcommon/marshallingIds.tssrc/vs/base/common/path.ts · 1589 LOCcommon/path.tssrc/vs/base/common/platform.ts · 281 LOCcommon/platform.tssrc/vs/base/common/process.ts · 76 LOCcommon/process.tssrc/vs/base/common/stopwatch.ts · 41 LOCcommon/stopwatch.tssrc/vs/base/common/tfIdf.ts · 240 LOCcommon/tfIdf.tssrc/vs/base/common/types.ts · 410 LOCcommon/types.tssrc/vs/base/common/uri.ts · 754 LOCcommon/uri.tssrc/vs/base/test/common/utils.ts · 107 LOCcommon/utils.tssrc/vs/nls.ts · 244 LOCvs/nls.tscancellation.test|title=CancellationToken None|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/cancellation.test|title=CancellationToken None|occurrence=1cancellation.test|title=…charCode.test|title=CharCode has good values|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/charCode.test|title=CharCode has good values|occurrence=1charCode.test|title=Char…path.test|title=Paths (Node Implementation) path|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/path.test|title=Paths (Node Implementation) path|occurrence=1path.test|title=Paths (N…tfIdf.test|title=TF-IDF Calculator Should ignore case and punctuation|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should ignore case and punctuation|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should match on camelCase words|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should match on camelCase words|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should not match document after delete|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should not match document after delete|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should return document with more matches first when term appears in all documents|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should return document with more matches first when term appears in all documents|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should return document with more matches first|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should return document with more matches first|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should return no scores for term not in document|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should return no scores for term not in document|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should return no scores when no documents are given|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should return no scores when no documents are given|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should return scores for document with exact match|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should return scores for document with exact match|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should weigh chunks with less common terms higher|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should weigh chunks with less common terms higher|occurrence=1tfIdf.test|title=TF-IDF …tfIdf.test|title=TF-IDF Calculator Should weigh less common term higher|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/tfIdf.test|title=TF-IDF Calculator Should weigh less common term higher|occurrence=1tfIdf.test|title=TF-IDF …uri.test|title=URI File paths containing apostrophes break URI parsing and cannot be opened #276075|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/uri.test|title=URI File paths containing apostrophes break URI parsing and cannot be opened #276075|occurrence=1uri.test|title=URI File …uri.test|title=URI URI#file, win-speciale|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/base/test/common/uri.test|title=URI URI#file, win-speciale|occurrence=1uri.test|title=URI URI#f…Focused concept · tfIdf.ts ×8 · 127 introduced LOCtfIdf.ts ×8

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 127 introduced LOC across 8 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/common/tfIdf.ts 127 introduced LOC · 8 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- tfIdf.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { CancellationToken } from './cancellation.js';
7 >
8 > type SparseEmbedding = Record</* word */ string, /* weight */number>;
9 > type TermFrequencies = Map</* word */ string, /*occurrences*/ number>;
10 > type DocumentOccurrences = Map</* word */ string, /*documentOccurrences*/ number>;
11 >
12 > function countMapFrom<K>(values: Iterable<K>): Map<K, number> {
13 > const map = new Map<K, number>();
14 > for (const value of values) {
15 > map.set(value, (map.get(value) ?? 0) + 1);
16 > }
17 > return map;
18 > }
19 >
20 > interface DocumentChunkEntry {
21 > readonly text: string;
22 > readonly tf: TermFrequencies;
23 > }
24 >
25 > export interface TfIdfDocument {
26 > readonly key: string;
27 > readonly textChunks: readonly string[];
28 > }
29 >
30 > export interface TfIdfScore {
31 > readonly key: string;
32 > /**
33 > * An unbounded number.
34 > */
35 > readonly score: number;
36 > }
37 >
38 > export interface NormalizedTfIdfScore {
39 > readonly key: string;
40 > /**
41 > * A number between 0 and 1.
42 > */
43 > readonly score: number;
44 > }
45 >
46 > /**
47 > * Implementation of tf-idf (term frequency-inverse document frequency) for a set of
48 > * documents where each document contains one or more chunks of text.
49 > * Each document is identified by a key, and the score for each document is computed
50 > * by taking the max score over all the chunks in the document.
51 > */
52 > export class TfIdfCalculator {
53 > calculateScores(query: string, token: CancellationToken): TfIdfScore[] {
54 > const embedding = this.computeEmbedding(query);
55 > const idfCache = new Map<string, number>();
56 > const scores: TfIdfScore[] = [];
57 > // For each document, generate one score
58 > for (const [key, doc] of this.documents) {
59 if (token.isCancellationRequested) {
60 return [];
68 }
69 }
70 > tfIdf.ts
71 > return scores;
72 > }
73 >
74 > /**
75 > * Count how many times each term (word) appears in a string.
76 > */
77 > private static termFrequencies(input: string): TermFrequencies {
78 > return countMapFrom(TfIdfCalculator.splitTerms(input));
79 > }
80 >
81 > /**
82 > * Break a string into terms (words).
83 > */
84 > private static *splitTerms(input: string): Iterable<string> {
85 > const normalize = (word: string) => word.toLowerCase();
86 >
87 > // Only match on words that are at least 3 characters long and start with a letter
88 > for (const [word] of input.matchAll(/\b\p{Letter}[\p{Letter}\d]{2,}\b/gu)) {
89 > yield normalize(word);
90 >
91 > const camelParts = word.replace(/([a-z])([A-Z])/g, '$1 $2').split(/\s+/g);
92 > if (camelParts.length > 1) {
93 for (const part of camelParts) {
94 // Require at least 3 letters in the parts of a camel case word
98 }
99 }
100 > } tfIdf.ts
101 > }
102 >
103 > /**
104 > * Total number of chunks
105 > */
106 > private chunkCount = 0;
107 >
108 > private readonly chunkOccurrences: DocumentOccurrences = new Map</* word */ string, /*documentOccurrences*/ number>();
109 >
110 > private readonly documents = new Map</* key */ string, {
111 > readonly chunks: ReadonlyArray<DocumentChunkEntry>;
112 > }>();
113 >
114 > updateDocuments(documents: ReadonlyArray<TfIdfDocument>): this {
115 for (const { key } of documents) {
116 this.deleteDocument(key);
139 return this;
140 }
141 > tfIdf.ts
142 > deleteDocument(key: string) {
143 const doc = this.documents.get(key);
144 if (!doc) {
164 }
165 }
166 > tfIdf.ts
167 > private computeSimilarityScore(chunk: DocumentChunkEntry, queryEmbedding: SparseEmbedding, idfCache: Map<string, number>): number {
168 // Compute the dot product between the chunk's embedding and the query embedding
169
191 return sum;
192 }
193 > tfIdf.ts
194 > private computeEmbedding(input: string): SparseEmbedding {
195 > const tf = TfIdfCalculator.termFrequencies(input);
196 > return this.computeTfidf(tf);
197 > }
198 >
199 > private computeIdf(term: string): number {
200 > const chunkOccurrences = this.chunkOccurrences.get(term) ?? 0;
201 > return chunkOccurrences > 0
202 ? Math.log((this.chunkCount + 1) / chunkOccurrences)
203 : 0;
204 > } tfIdf.ts
205 >
206 > private computeTfidf(termFrequencies: TermFrequencies): SparseEmbedding {
207 > const embedding = Object.create(null);
208 > for (const [word, occurrences] of termFrequencies) {
209 > const idf = this.computeIdf(word);
210 > if (idf > 0) {
211 embedding[word] = occurrences * idf;
212 }
213 > } tfIdf.ts
214 > return embedding;
215 > }
216 > }
217 >
218 > /**
219 > * Normalize the scores to be between 0 and 1 and sort them decending.
220 > * @param scores array of scores from {@link TfIdfCalculator.calculateScores}
221 > * @returns normalized scores
222 > */
223 > export function normalizeTfIdfScores(scores: TfIdfScore[]): NormalizedTfIdfScore[] {
224
225 // copy of scores