0

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

Michael
  • 199
  • 2
  • 16
  • 1
    Hi Michael. You didn't actually ask a question :) Normally, you'll get the best response if you boil your question down to specifically the code that's causing you issue, what you've tried to do so far to resolve it & what specifically isn't working as you'd expect. – anotherdave Jun 04 '14 at 16:35
  • 1
    Ok i'll edit then. Thanks @anotherdave edited. – Michael Jun 04 '14 at 16:36
  • @anotherdave i hope its covered now :\ – Michael Jun 04 '14 at 16:43
  • I think it's becoming more interesting :) — what do you mean though that it doesn't scale well? E.g. It runs slower than expected? Also, what do you mean that it's not counting words properly? — how does it differ between when you use special chars & without? Maybe if you include a link to a http://jsfiddle.net/ that can reproduce your problem too, it would be really useful. E.g. if I run this version (http://jsfiddle.net/vRU8L) at present, I get "Tom is super ---> 1" which is not what you're seeing (though maybe I've transferred it incorrectly). – anotherdave Jun 04 '14 at 16:50
  • @anotherdave well if u put some other words rather than on english (peča peća etc) they will not count.(maybe i have to define utf-8 standard or something) – Michael Jun 04 '14 at 16:59
  • great — now you've narrowed down the problem to specifically the regex & its ability to match non-ASCII characters. This question might be useful to continue with where you need to go from here: http://stackoverflow.com/questions/280712/javascript-unicode-regexes – anotherdave Jun 04 '14 at 17:25
  • @anotherdave aha okay thanks, also is my JS valid for such purpose, or i have to change something? :) – Michael Jun 04 '14 at 18:56
  • If you mean valid as in, does it follow best practice? I'd suggest our sister site: http://codereview.stackexchange.com – anotherdave Jun 04 '14 at 19:56

0 Answers0