Last active
February 27, 2020 23:45
-
-
Save jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653 to your computer and use it in GitHub Desktop.
word_count
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
| const englishString = | |
| "The administration will encourage federal data and universities to share data that can drive the administration of automated systems data"; | |
| const toSplitBySeparator = (splitTarget, separator) => | |
| splitTarget.toLowerCase().split(separator); | |
| const countWord = stringArray => { | |
| let word = ""; | |
| let count = 0; | |
| const processingEndWords = []; | |
| for (let i = 0; i < stringArray.length; i++) { | |
| word = stringArray[i]; | |
| count = 0; | |
| for (let j = 0; j < stringArray.length; j++) { | |
| if (word === stringArray[j]) { | |
| count++; | |
| } | |
| } | |
| let find = processingEndWords.find(item => word === item); | |
| if (!find) { | |
| processingEndWords.push(word); | |
| console.log(`${word}: ${count}`); | |
| } | |
| } | |
| }; | |
| countWord(toSplitBySeparator(englishString, " ")); |
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
| const str = | |
| "Until last week, Ms. Morales kept the episode to herself. But after another But woman came forward to file. Her a criminal complaint against the former president alleging a sexual assault, Ms. Morales summoned the courage to add her voice."; | |
| const filterToPureWords = englishSentence => | |
| englishSentence | |
| .toLowerCase() | |
| .replace(/\W/g, " ") | |
| .split(" ") | |
| .filter(word => /\w/.test(word)); | |
| const saveCountWordToMap = (stringArray, map) => { | |
| for (let word of stringArray) { | |
| let count = 0; | |
| stringArray.find(targetWord => { | |
| if (word === targetWord) { | |
| count++; | |
| } | |
| map.set(word, count); | |
| }); | |
| } | |
| return map; | |
| }; | |
| const wordCountMap = new Map(); | |
| const result = saveCountWordToMap(filterToPureWords(str), wordCountMap); | |
| console.log(result); |
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
| const str = | |
| "Until last week, Ms. Morales kept the episode to herself. But after another But woman came forward to file. Her a criminal complaint against the former president alleging a sexual assault, Ms. Morales summoned the courage to add her voice."; | |
| const filterToPureWords = englishSentence => | |
| englishSentence | |
| .toLowerCase() | |
| .replace(/\W/g, " ") | |
| .split(" ") | |
| .filter(word => /\w/.test(word)); | |
| const countWord = (stringArray, word) => | |
| stringArray.filter(item => item === word).length; | |
| const saveToMap = (stringArray, map, countFunc) => { | |
| for (let word of stringArray) { | |
| map.set(word, countFunc(stringArray, word)); | |
| } | |
| return map; | |
| }; | |
| const result = saveToMap(filterToPureWords(str), new Map(), countWord); | |
| console.log(result); |
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
| const str = | |
| "independent independent independent similar similar similar similar similar similar successful successful enormous busy basic massive massive massive massive massive sings sings sings sings"; | |
| const filterToPureWords = englishSentence => | |
| englishSentence | |
| .toLowerCase() | |
| .replace(/\W/g, " ") | |
| .split(" ") | |
| .filter(word => /\w/.test(word)); | |
| const countWord = (stringArray, word) => | |
| stringArray.filter(item => item === word).length; | |
| const sortCount = ({ sortTarget, desc }) => { | |
| if (desc) { | |
| return sortTarget.sort( | |
| (compare1, compare2) => | |
| Object.values(compare2)[0] - Object.values(compare1)[0] | |
| ); | |
| } | |
| return sortTarget.sort( | |
| (compare1, compare2) => | |
| Object.values(compare1)[0] - Object.values(compare2)[0] | |
| ); | |
| }; | |
| const saveToArray = (stringArray, countFunc) => { | |
| const arrayWithWordCounts = [] | |
| for (let word of stringArray) { | |
| /** | |
| * TODO: how get object key in array ? | |
| */ | |
| arrayWithWordCounts.push({ [word]: countFunc(stringArray, word) }); | |
| } | |
| return arrayWithWordCounts; | |
| }; | |
| const result = saveToArray( | |
| filterToPureWords(str), | |
| countWord | |
| ); | |
| sortCount({ sortTarget: result, desc: true }); | |
| console.log(result.slice(0, 5)); |
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
| const str = | |
| "Until last week, Ms. Morales kept the episode to herself. But after another But woman came forward to file. Her a criminal complaint against the former president alleging a sexual assault, Ms. Morales summoned the courage to add her voice."; | |
| const filterToPureCharacter = englishSentence => | |
| Array.from(englishSentence) | |
| const saveCountCharacterToMap = (stringArray, map) => { | |
| for (let c of stringArray) { | |
| let count = 0; | |
| stringArray.find(targetCharacter => { | |
| if (c === targetCharacter) { | |
| count++; | |
| map.set(targetCharacter, count); | |
| } | |
| }); | |
| } | |
| return map; | |
| }; | |
| const characterCountMap = new Map(); | |
| const result = saveCountCharacterToMap(filterToPureCharacter(str), characterCountMap); | |
| console.log(result); |
Author
Author
saveCountCharacterToMap関数の中のロジック中、
map.set()するタイミングが間違えていた!下記のように修正しました。:pray: @kogai
https://gist.github.com/jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653#file-word_count_v5-js-L13
Author
.replace()で文字ではない.,,,を削除する
の話は仕様には無かった。。
仕様は., ,, を一つの文字としてカウントして欲しいという内容だったので、
., ,, をフィルターする必要はなくなった。
.filter(character => /\w/.test(character)); のところを削除した。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
今は
filterToPureWordsという関数で単語を区切った配列を返しているfilterToPureWords関数の中を単語基準ではなく文字基準で区切るようにするArray.from(文字列)というAPIが文字列をアルファベット基準でArrayにしてくれることを分かった。.split()の中に正規表現(文字列を区切る基準は正規表現)を入れれるのか調査するArray.from(文字列)を使うことで.split()は不要になった単語ではなく、文字を区切るようになる。そのため不要なJavaScript API関数を削除する
.toLowerCase(),.filter()は不要ではないか?-> 試してみる.toLowerCase()はまず、仕様を確認が必要、.filter()はここにかく必要はないfilterToPureWordsからfilterToPureCharacterに関数名を変える小飼さんと相談後に実装に反映したこと
saveCountWordToMap、wordCountMapなどの変数、関数名を
saveCountCharacterToMap、characterCountMapに変更結果:
https://gist.github.com/jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653#file-word_count_v5-js
https://repl.it/@jinma/TrustingCalculatingViruses