(text: string, options: TokenizeOptions = {})
| 87 | * tokenize("running dogs", { stem: true }) // ["run", "dog"] |
| 88 | */ |
| 89 | export 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. |
no test coverage detected