Skip to content

Instantly share code, notes, and snippets.

@SimonBrandner
Created January 30, 2024 18:01
Show Gist options
  • Save SimonBrandner/7c96c70c141c7e0e6c6b5febb69c79d2 to your computer and use it in GitHub Desktop.
Save SimonBrandner/7c96c70c141c7e0e6c6b5febb69c79d2 to your computer and use it in GitHub Desktop.
Creates all possible n-grams from a string
const MAX_NGRAM_LENGTH = 5;
const inputString = "Hello Alice how are you doing at the moment";
const findNgram = (
words: String[],
length: number,
startIndex: number
): String | undefined => {
if (startIndex + length > words.length) return;
return words.slice(startIndex, startIndex + length).join(" ");
};
const findNgrams = (inputString: String): String[] => {
const words = inputString.split(" ");
const ngrams: String[] = [];
for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {
for (
let ngram_length = 1;
ngram_length <= MAX_NGRAM_LENGTH;
ngram_length++
) {
const ngram = findNgram(words, ngram_length, wordIndex);
if (ngram) {
ngrams.push(ngram);
}
}
}
return ngrams;
};
const ngrams = findNgrams(inputString);
console.log(ngrams);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment