113
114
updateDocuments(documents: ReadonlyArray<TfIdfDocument>): this {
115
>
for (const { key } of documents) {
tfIdf.ts
116
>
this.deleteDocument(key);
117
>
}
118
>
119
>
for (const doc of documents) {
120
>
const chunks: Array<{ text: string; tf: TermFrequencies }> = [];
121
>
for (const text of doc.textChunks) {
122
>
// TODO: See if we can compute the tf lazily
123
>
// The challenge is that we need to also update the `chunkOccurrences`
124
>
// and all of those updates need to get flushed before the real TF-IDF of
125
>
// anything is computed.
126
>
const tf = TfIdfCalculator.termFrequencies(text);
127
>
128
>
// Update occurrences list
129
>
for (const term of tf.keys()) {
130
>
this.chunkOccurrences.set(term, (this.chunkOccurrences.get(term) ?? 0) + 1);
131
>
}
132
>
133
>
chunks.push({ text, tf });
134
>
}
135
>
136
>
this.chunkCount += chunks.length;
137
>
this.documents.set(doc.key, { chunks });
138
>
}
139
>
return this;
140
>
}
141
142
deleteDocument(key: string) {
143
>
const doc = this.documents.get(key);
tfIdf.ts
144
>
if (!doc) {
145
>
return;
146
>
}
147
148
this.documents.delete(key);