Tofu/app/tools/text/buildBigrams.js

10 lines
407 B
JavaScript

// Character bigrams of an already normalized text, used by the Dice similarity.
// "abc" -> ["ab", "bc"]. A text shorter than two characters has no bigram.
export function buildBigrams(text) {
if (typeof text !== 'string' || text.length < 2) return []
const bigrams = []
for (let index = 0; index < text.length - 1; index += 1) {
bigrams.push(text.slice(index, index + 2))
}
return bigrams
}