-
-
Save martin-mok/52cfd369f5960c88ec77f98e4c05c471 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment