MCPcopy Create free account
hub / github.com/idosal/git-mcp / extractKeywords

Function extractKeywords

src/api/utils/vectorStore.ts:136–187  ·  view source on GitHub ↗

* Extract keywords and their importance from text

(text: string)

Source from the content-addressed store, hash-verified

134 * Extract keywords and their importance from text
135 */
136function extractKeywords(text: string): Array<{ term: string; score: number }> {
137 const results: Array<{ term: string; score: number }> = [];
138 const words = text
139 .toLowerCase()
140 .split(/\W+/)
141 .filter((w) => w.length > 3);
142
143 // Count word frequencies
144 const wordCounts: { [key: string]: number } = {};
145 for (const word of words) {
146 wordCounts[word] = (wordCounts[word] || 0) + 1;
147 }
148
149 // Find interesting terms (high frequency or within heading patterns)
150 const headings = text.match(/#{1,6}\s+([^\n]+)/g) || [];
151 const headingTerms = new Set<string>();
152
153 // Extract terms from headings with higher weight
154 for (const heading of headings) {
155 const cleanHeading = heading.replace(/^#+\s+/, "").toLowerCase();
156 const terms = cleanHeading.split(/\W+/).filter((w) => w.length > 3);
157 terms.forEach((t) => headingTerms.add(t));
158 }
159
160 // Calculate term scores based on frequency and position
161 const totalWords = words.length;
162
163 for (const word in wordCounts) {
164 // Skip common words or very rare words
165 if (commonWords.has(word) || wordCounts[word] < 2) continue;
166
167 // Calculate score based on frequency
168 let score = wordCounts[word] / totalWords;
169
170 // Boost score for terms in headings
171 if (headingTerms.has(word)) {
172 score *= 3;
173 }
174
175 // Boost for terms in the first paragraph (likely important)
176 const firstPara = text.split("\n\n")[0].toLowerCase();
177 if (firstPara.includes(word)) {
178 score *= 1.5;
179 }
180
181 results.push({ term: word, score });
182 }
183
184 // Sort by score and take top 20
185 results.sort((a, b) => b.score - a.score);
186 return results.slice(0, 20);
187}
188
189/**
190 * Normalize a vector to unit length

Callers 1

getEmbeddingsFunction · 0.85

Calls 1

replaceMethod · 0.65

Tested by

no test coverage detected