Skip to content

Instantly share code, notes, and snippets.

@jinmayamashita
Last active February 27, 2020 23:45
Show Gist options
  • Select an option

  • Save jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653 to your computer and use it in GitHub Desktop.

Select an option

Save jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653 to your computer and use it in GitHub Desktop.
word_count
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, " "));
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);
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);
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));
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);
@jinmayamashita

jinmayamashita commented Feb 13, 2019

Copy link
Copy Markdown
Author

単語ではなく、文字の出現回数をカウントして下さい

  • 今はfilterToPureWordsという関数で単語を区切った配列を返している

  • filterToPureWords関数の中を単語基準で区切らない。文字基準で区切るように変更する

    • 文字列を区切る基準は正規表現で判定している
    • 正規表現で単語ではなく、文字を判定するpatternがあると思う。-> 調査する
    • または、文字列を文字別でArrayのアイテムにするAPIがあると思う -> 正規表現のpatternが見つからなかったら、調査する
    • .replace()で文字ではない ., ,, を削除する
    • .split()の中に正規表現の文字を判定するpatternで区切って配列に入れる
  • 単語ではなく、文字を区切るようになる。そのため不要なJavaScript API関数を削除する

    • .toLowerCase(), .filter()は不要ではないか?-> 試してみる
  • filterToPureWordsからfilterToPureCharacterに関数名を変える

  • 文字の出現回数のカウントを確認しやすくするため、変数englishStringの中身を変える

  • filterToPureCharacterenglishString以外の部分は変更無し


  • これは分かったない。調べる

    • 正規表現で単語ではなく、文字を判定するpatternがあるか調査する
    • 文字列を文字別でArrayのアイテムにするAPIを調査する
  • これは分かったない。聞いてみる

    • 大文字と小文字は同じ文字として判断するのか、それとも違う文字として判断するのか?
    • word_count_v4.jsは論理的ではない実装をしている。文字の出現回数をカウントするのを word_count_v2.jsで実装してもいいか相談してみる @kogai

@jinmayamashita

jinmayamashita commented Feb 13, 2019

Copy link
Copy Markdown
Author

単語ではなく、文字の出現回数をカウントして下さい

  • 今はfilterToPureWordsという関数で単語を区切った配列を返している

  • filterToPureWords関数の中を単語基準ではなく文字基準で区切るようにする

    • 文字列を区切る基準は正規表現で判定している
    • 正規表現で文字を判定するpatternを調査する
      • MDN Documentでは文字(アルファベット)を判定するpatternが分からない -> 一旦止める
    • または、文字列を文字別でArrayに変換出来るかAPIを調査する
      • Array.from(文字列)というAPIが文字列をアルファベット基準でArrayにしてくれることを分かった。
    • .split()の中に正規表現(文字列を区切る基準は正規表現)を入れれるのか調査する
      • Array.from(文字列)を使うことで.split()は不要になった
  • 単語ではなく、文字を区切るようになる。そのため不要なJavaScript API関数を削除する

    • .toLowerCase(), .filter()は不要ではないか?-> 試してみる
      • .toLowerCase()はまず、仕様を確認が必要、 .filter()はここにかく必要はない
  • filterToPureWordsからfilterToPureCharacterに関数名を変える


小飼さんと相談後に実装に反映したこと

  • 大文字と小文字は区分する
  • 単語ではなく、文字の出現回数ので、既存のsaveCountWordToMapwordCountMapなどの
    変数、関数名をsaveCountCharacterToMapcharacterCountMapに変更

結果:
https://gist.github.com/jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653#file-word_count_v5-js
https://repl.it/@jinma/TrustingCalculatingViruses

@jinmayamashita

jinmayamashita commented Feb 13, 2019

Copy link
Copy Markdown
Author

saveCountCharacterToMap関数の中のロジック中、
map.set()するタイミングが間違えていた!下記のように修正しました。:pray: @kogai

https://gist.github.com/jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653#file-word_count_v5-js-L13

repl:
https://repl.it/@jinma/TrustingCalculatingViruses

@jinmayamashita

jinmayamashita commented Feb 13, 2019

Copy link
Copy Markdown
Author
  • .replace()で文字ではない ., ,, を削除する

の話は仕様には無かった。。

仕様は., ,, を一つの文字としてカウントして欲しいという内容だったので、

., ,, をフィルターする必要はなくなった。
.filter(character => /\w/.test(character)); のところを削除した。

diff:
https://gist.github.com/jinmayamashita/6bd4cc21ada3f502197ada9d25e6f653/revisions#diff-8ff03d2e1306d7e1d86e5f2d06b0474b

repl:
https://repl.it/@jinma/TrustingCalculatingViruses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment