I am trying to make a program which will count up to max 3 words in a phrase, and count repetitions of same words (max 3).
Edit:
My problem is :
When i type special letters such žđč
program doesnt scale well, and its not counting words properly.
JSFiddle : Link
Example:
We have a words such:
Tom is super and epic.
Tom is super gg.
Tom is super.
Tom is not.
Tom is.
Output should be :
Tom is super - 3
(number of repetitions of Tom is super )(first 3 words) and 3 is number of repetitions of "Tom is super"
Reason why "Tom is super and epic" is not in "equation" is that is 5 words, and we take only 3, and "Tom is not" is not there because it repeated only once. So program takes only words which apear more times, and max 3 times.
My code looks like:
function parse() {
alert('hi');
s = document.getElementById('inputText').value;
s = s.replace(/[\.\!\?:;,]+/g, '');
a = s.match(/\b([\w+\s*\w*]+)\b(?=.*\b\1\b)/g);
a = a.filter(function(n){ return n.length > 1 });
var res = document.getElementById('res');
var ul = document.createElement('ul');
var result = new Array();
for (var i = 0; i < a.length; i++) {
if (result[a[i]] == null) {
result[a[i]] = 1;
} else {
result[a[i]]++;
}
};
for(var key in result) {
var li=document.createElement('li');
ul.appendChild(li);
li.innerHTML = key + ' ---> ' + result[key];
}
res.appendChild(ul);
}
And html :
<html>
<head>
<title></title>
</head>
<body>
<textarea id="inputText"></textarea>
<div id="res">
</div>
<input type="button" onClick="parse()" />
</body>
</html>
if the pattern with 3 words is repeated only once, it is ignored as result, and it should run check how many times it is repeated as first 2 words and if the pattern with 2 words is repeated only once, then it runs a check with the first word only.
Thanks,
Michael