我试图在Javascript中创建一些东西来计算每个单词或短语在某些输入中出现的次数的密度百分比。但是,需要在整个输入中计算所有可能的组合,并为每个组合显示百分比。
例如,请考虑以下事项:
input:
green apple pie recipe
output:
"green apple pie recipe" = 100%
"green apple pie" = 75%
"apple pie recipe" = 75%
"green apple" = 50%
"pie recipe" = 50%
"apple pie" = 50%
"green" = 25%
"apple" = 25%
"pie" = 25%
"recipe" = 25%
以上只是一个简单的例子,但脚本需要计算从几百到几千字的任何地方。有什么想法吗?
答案 0 :(得分:0)
好的,这样的事情:
https://jsfiddle.net/8uf8o7zj/
var input = "green apple pie recipe";
var sentances = [
"green apple pie recipe",
"green apple pie",
"apple pie recipe",
"green apple",
"pie recipe",
"apple pie",
"green",
"apple",
"pie",
"recipe"];
var count = input.split(' ').length;
var pattern = input.split(' ').join('|');
var r = new RegExp(pattern,'g');
var output = [];
sentances.forEach(function (sentance) {
var matches = sentance.match(r);
output.push({
'sentance' : sentance,
'percentage' : (matches) ? (matches.length / count) * 100 : 0,
toString: function () { return this.sentance + ' - '+ this.percentage + '%' }
});
});
output.forEach(function (result) {
alert(result.toString());
});