Skip to content

Instantly share code, notes, and snippets.

@adatta02
Created August 3, 2018 15:57
Show Gist options
  • Save adatta02/a0fb5fe02dcc0ffc652e83961821f8a2 to your computer and use it in GitHub Desktop.
Save adatta02/a0fb5fe02dcc0ffc652e83961821f8a2 to your computer and use it in GitHub Desktop.
export class Wordcloud {
public getWordCloudCounts(reviews: Review[]): IWordCount[] {
const wordsWithCounts: IWordCount[] = new Array();
const numReviews = reviews.length;
wordsWithCounts.push({
word: 'ThisIsAPlaceHolderThatWillBeRemoved',
count: 0,
});
for (let i = 0; i < numReviews; i++) {
const splitter = reviews[i].body.split(' ');
let found: boolean = false;
for (let j = 0; j < splitter.length; j++) {
if (splitter[j].endsWith('.')) {
splitter[j] = splitter[j].slice(0, -1);
}
for (let k = 0; k < wordsWithCounts.length; k++) {
if (splitter[j] === wordsWithCounts[k].word) {
wordsWithCounts[k].count++;
found = true;
}
else if (found === false && k === wordsWithCounts.length - 1) {
wordsWithCounts.push({
word: splitter[j],
count: 0,
});
}
}
found = false;
}
}
wordsWithCounts.reverse();
wordsWithCounts.pop();
console.log(wordsWithCounts);
return wordsWithCounts;
}
}
interface IWordCount {
word: string;
count: number;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment