Created
November 17, 2016 22:19
-
-
Save loretoparisi/ce18887b0195f315e208f19aee1bf41d to your computer and use it in GitHub Desktop.
Count repeating occurences of Bag of Words in JavaScript by reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r,words="We don't talk anymore\nWe don't talk anymore\nWe don't talk anymore\nLike we used to do\nWe don't love anymore\nWhat was all of it for?\nOh, we don't talk anymore\nLike we used to do\n\nI just heard you found the one you've been looking\nYou've been looking for" | |
words=words.split(/[\s]+/g); | |
r=[...words.reduce( (m, v) => m.set(v, (m.get(v) || 0) + 1), new Map() )]; | |
console.log(r) |
To join the array of words in a string of word delimited lines
var r,words="We don't talk anymore\nWe don't talk anymore\nWe don't talk anymore\nLike we used to do\nWe don't love anymore\nWhat was all of it for?\nOh, we don't talk anymore\nLike we used to do\n\nI just heard you found the one you've been looking\nYou've been looking for"
words=words.split(/[\s]+/g);
r=[...words.reduce( (m, v) => m.set(v, (m.get(v) || 0) + 1), new Map() )].join('\n');
console.log(r)
[UPDATE]
Iterate over multiple documents
var docs=["We don't talk anymore\nWe don't talk anymore\nWe don't talk anymore\nLike we used to do\nWe don't love anymore\nWhat was all of it for?\nOh, we don't talk anymore\nLike we used to do\n\nI just heard you found the one you've been looking\nYou've been looking for","I don't like your little games\nDon\'t like your tilted stage\nThe role you made me play\nOf the fool\nNo, I don't like you"]
docs=docs.map(words => words.split(/[\s]+/g) )
var m = new Map()
docs.forEach(words => words.reduce( (m, v) => m.set(v, (m.get(v) || 0) + 1), m ) )
[...m].join("\n")
"We,4
don't,7
talk,4
anymore,5
Like,2
we,3
used,2
to,2
do,2
love,1
What,1
was,1
all,1
of,1
it,1
for?,1
Oh,,1
I,3
just,1
heard,1
you,3
found,1
the,2
one,1
you've,1
been,2
looking,2
You've,1
for,1
like,3
your,2
little,1
games,1
Don't,1
tilted,1
stage,1
The,1
role,1
made,1
me,1
play,1
Of,1
fool,1
No,,1"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To add
term frequency