Created
April 14, 2017 01:07
-
-
Save Natumsol/6d7f30fa3929b4f5f4ad77569b3a0546 to your computer and use it in GitHub Desktop.
统计关键字词频
This file contains 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
/** | |
* @param article String | |
* @param keywords Array | |
*/ | |
function statistics(articles, keywords) { | |
var frequency = {} | |
, alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
for (var i = 0; i < keywords.length; i++) { | |
frequency[keywords[i]] = 0; | |
} | |
var word = []; | |
for (var i = 0; i < articles.length; i++) { | |
if (alphabet.indexOf(articles[i]) != -1) { | |
if (i == 0 || alphabet.indexOf(articles[i - 1]) == -1) { | |
word.length = 0; | |
word.push(articles[i]); | |
} else { | |
word.push(articles[i]); | |
} | |
} else { | |
if (word.length && frequency[word.join("")] != undefined) { | |
frequency[word.join("")]++; | |
word.length = 0; | |
} | |
} | |
} | |
if (word.length && frequency[word.join("")] != undefined) { | |
frequency[word.join("")]++; | |
word.length = 0; | |
} | |
return frequency; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment