MCPcopy Create free account
hub / github.com/codemix/graph / tokenize

Function tokenize

packages/text-search/src/tokenizer.ts:89–122  ·  view source on GitHub ↗
(text: string, options: TokenizeOptions = {})

Source from the content-addressed store, hash-verified

87 * tokenize("running dogs", { stem: true }) // ["run", "dog"]
88 */
89export function tokenize(text: string, options: TokenizeOptions = {}): Token[] {
90 const { stem: applyStem = true, removeStopwords = true, minLength = 1 } = options;
91
92 // Convert to lowercase and extract words
93 const words = text
94 .toLowerCase()
95 .replace(/[^a-z0-9\s]/g, " ")
96 .split(/\s+/)
97 .filter((w) => w.length >= minLength);
98
99 const tokens: Token[] = [];
100
101 for (let i = 0; i < words.length; i++) {
102 const word = words[i]!;
103
104 // Skip stopwords if configured
105 if (removeStopwords && STOPWORDS.has(word)) {
106 continue;
107 }
108
109 // Skip very short words
110 if (word.length < minLength) {
111 continue;
112 }
113
114 tokens.push({
115 original: word,
116 stemmed: applyStem ? stem(word) : word,
117 position: i,
118 });
119 }
120
121 return tokens;
122}
123
124/**
125 * Extract just the stemmed terms from text.

Callers 5

extractTermsFunction · 0.85
tokenizer.test.tsFile · 0.85
prepareQueryFunction · 0.85
matchWithDetailsFunction · 0.85

Calls 4

stemFunction · 0.85
filterMethod · 0.80
hasMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected