Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created April 14, 2017 01:07
Show Gist options
  • Save Natumsol/6d7f30fa3929b4f5f4ad77569b3a0546 to your computer and use it in GitHub Desktop.
Save Natumsol/6d7f30fa3929b4f5f4ad77569b3a0546 to your computer and use it in GitHub Desktop.
统计关键字词频
/**
* @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