Skip to content

Instantly share code, notes, and snippets.

@adatta02
Created August 3, 2018 15:58
Show Gist options
  • Save adatta02/9b9336398c55569bd78f1904f7fe183c to your computer and use it in GitHub Desktop.
Save adatta02/9b9336398c55569bd78f1904f7fe183c to your computer and use it in GitHub Desktop.
getWordCloudCounts(reviews: string[]): {} {
const words = {};
const numReviews = reviews.length;
const wordCounts: IWordCount[] = [];
const stopWords = StopWords.get();
for (let i = 0; i < numReviews; i++) {
reviews[i] = reviews[i].toString();
const splitter = reviews[i].toLowerCase().split(' ');
for (let j = 0; j < splitter.length; j++) {
if (splitter[j].endsWith('.') || splitter[j].endsWith(',') || splitter[j].endsWith('?') || splitter[j].endsWith('!')
|| splitter[j].endsWith(';') || splitter[j].endsWith(':') || splitter[j].endsWith('"') || splitter[j].endsWith('\'')
|| splitter[j].endsWith('&') || splitter[j].endsWith(')')) {
splitter[j] = splitter[j].slice(0, -1);
}
if (splitter[j].startsWith('"') || splitter[j].startsWith('\'') || splitter[j].startsWith('&') || splitter[j].startsWith(' ')
|| splitter[j].startsWith('(')) {
splitter[j] = splitter[j].slice(1);
}
if (splitter[j].endsWith('."')) {
splitter[j] = splitter[j].slice(0, -1);
splitter[j] = splitter[j].slice(0, -1);
}
if ((!(splitter[j] in words) && splitter[j].length > 3)) {
words[splitter[j]] = 1;
}
else if (splitter[j].length > 3){
words[splitter[j]]++;
}
}
}
return words;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment