JavaScriptでキーワードをカウントする最良の方法は何ですか?
-
02-07-2019 - |
質問
JavaScriptでキーワードをカウントするための最良かつ最も効率的な方法は何ですか?基本的に、文字列を取得し、主にタグを提案するために、文字列内で発生する上位N個の単語またはフレーズを取得します。概念的なヒントや実際のコードへのリンクを実際のコードよりも探していますが、コードを共有したい場合は気にしないでしょう。役立つ特定の機能がある場合は、それもありがたいです。
今、私はsplit()関数を使用して文字列をスペースで区切り、正規表現で句読点を削除していると思います。また、大文字と小文字を区別しないようにします。
解決
カット、ペースト+デモの実行:
var text = "Text to be examined to determine which n words are used the most";
// Find 'em!
var wordRegExp = /\w+(?:'\w{1,2})?/g;
var words = {};
var matches;
while ((matches = wordRegExp.exec(text)) != null)
{
var word = matches[0].toLowerCase();
if (typeof words[word] == "undefined")
{
words[word] = 1;
}
else
{
words[word]++;
}
}
// Sort 'em!
var wordList = [];
for (var word in words)
{
if (words.hasOwnProperty(word))
{
wordList.push([word, words[word]]);
}
}
wordList.sort(function(a, b) { return b[1] - a[1]; });
// Come back any time, straaanger!
var n = 10;
var message = ["The top " + n + " words are:"];
for (var i = 0; i < n; i++)
{
message.push(wordList[i][0] + " - " + wordList[i][1] + " occurance" +
(wordList[i][1] == 1 ? "" : "s"));
}
alert(message.join("\n"));
再利用可能な関数:
function getTopNWords(text, n)
{
var wordRegExp = /\w+(?:'\w{1,2})?/g;
var words = {};
var matches;
while ((matches = wordRegExp.exec(text)) != null)
{
var word = matches[0].toLowerCase();
if (typeof words[word] == "undefined")
{
words[word] = 1;
}
else
{
words[word]++;
}
}
var wordList = [];
for (var word in words)
{
if (words.hasOwnProperty(word))
{
wordList.push([word, words[word]]);
}
}
wordList.sort(function(a, b) { return b[1] - a[1]; });
var topWords = [];
for (var i = 0; i < n; i++)
{
topWords.push(wordList[i][0]);
}
return topWords;
}
他のヒント
単語の配列がクリーンアップされたら、それを wordArray
と呼ぶことにしましょう:
var keywordRegistry = {};
for(var i = 0; i < wordArray.length; i++) {
if(keywordRegistry.hasOwnProperty(wordArray[i]) == false) {
keywordRegistry[wordArray[i]] = 0;
}
keywordRegistry[wordArray[i]] = keywordRegistry[wordArray[i]] + 1;
}
// now keywordRegistry will have, as properties, all of the
// words in your word array with their respective counts
// this will alert (choose something better than alert) all words and their counts
for(var keyword in keywordRegistry) {
alert("The keyword '" + keyword + "' occurred " + keywordRegistry[keyword] + " times");
}
これは、作業のこの部分を実行するための基本を提供するはずです。
単語の文字列を分割して、結果の単語をカウントしてから、カウントでソートしてみてください。
これは、ループを1つだけ持つことにより、 insin による以前の回答に基づいて構築されます。
function top_words(text, n) {
// Split text on non word characters
var words = text.toLowerCase().split(/\W+/)
var positions = new Array()
var word_counts = new Array()
for (var i=0; i<words.length; i++) {
var word = words[i]
if (!word) {
continue
}
if (typeof positions[word] == 'undefined') {
positions[word] = word_counts.length
word_counts.push([word, 1])
} else {
word_counts[positions[word]][1]++
}
}
// Put most frequent words at the beginning.
word_counts.sort(function (a, b) {return b[1] - a[1]})
// Return the first n items
return word_counts.slice(0, n)
}
// Let's see if it works.
var text = "Words in here are repeated. Are repeated, repeated!"
alert(top_words(text, 3))
例の結果: [['repeated'、3]、['are'、2]、['words'、1]]
各単語を分離するために、上記で説明したとおりに行います。次に、各単語を配列のインデックスとして追加し、出現回数を値として追加します。
例:
var a = new Array;
a[word] = a[word]?a[word]+1:1;
これで、一意の単語の数(a.length)と各単語の出現回数(a [word])がわかりました。
所属していません StackOverflow