0

頻度で並べ替える必要のある単語の配列があります。その前に、「the」、「it」などの単語(実際には3文字未満)、およびすべての数字と#で始まる単語(単語の配列はから取得されます)を削除する必要があります。 Twitter、ただし以下の例はウィキペディアからのランダムな段落です)。

1つの単語を削除することはできますが、複数の単語または範囲を削除しようと夢中になっています。助言がありますか?ありがとうございました!

http://jsfiddle.net/9NzAC/6/

HTML:

<div id="text" style="background-color:Teal;position:absolute;left:100px;top:10px;height:500px;width:500px;">
Phrenology is a pseudoscience primarily focused on measurements of the human skull, based on the concept that the brain is the organ of the mind, and that certain brain areas have localized, specific functions or modules. The distinguishing feature of phrenology is the idea that the sizes of brain areas were meaningful and could be inferred by examining the skull of an individual.
</div>

JS:

//this is the function to remove words
<script type="text/javascript">
    function removeA(arr){
        var what, a= arguments, L= a.length, ax;
        while(L> 1 && arr.length){
            what= a[--L];
            while((ax= arr.indexOf(what))!= -1){
                arr.splice(ax, 1);
            }
        }
            return arr;
        }
</script>

//and this does the sorting & counting
<script type="text/javascript">
    var getMostFrequentWords = function(words) {
        var freq={}, freqArr=[], i;

        // Map each word to its frequency in "freq".
            for (i=0; i<words.length; i++) {
            freq[words[i]] = (freq[words[i]]||0) + 1;
        }

        // Sort from most to least frequent.
            for (i in freq) freqArr.push([i, freq[i]]);
            return freqArr.sort(function(a,b) { return b[1] - a[1]; });
        };

        var words = $('#text').get(0).innerText.split(/\s+/);

        //Remove articles & words we don't care about.
        var badWords = "the";
            removeA(words,badWords);
        var mostUsed = getMostFrequentWords(words);
        alert(words);

</script>
4

2 に答える 2

2

元の配列から新しい配列に削除する代わりにpush、それはより単純であり、コードをより短く、より読みやすくします。

var words = ['the', 'it', '12', '#twit', 'aloha', 'hello', 'bye']
var filteredWords = []

for (var i = 0, l = words.length, w; i < l; i++) {
    w = words[i]
    if (!/^(#|\d+)/.test(w) && w.length > 3)
        filteredWords.push(w)
}

console.log(filteredWords) // ['aloha', 'hello']

デモ: http: //jsfiddle.net/VcfvU/

于 2012-08-01T03:39:20.340 に答える
1

array[i] = null実行(または"")してから、配列の空のノードをクリーンアップすることをお勧めします。あなたはそれを使って簡単に達成することができますArray#filter

テスト: http: //jsfiddle.net/6LPep/ コード:

var FORGETABLE_WORDS = ',the,of,an,and,that,which,is,was,';

var words = text.innerText.split(" ");

for(var i = 0, word; word = words[i++]; ) {
    if (FORGETABLE_WORDS.indexOf(',' + word + ',') > -1 || word.length < 3) {
      words[i-1] = "";
    }
}

// falsy will get deleted
words.filter(function(e){return e});
// as example
output.innerHTML = words.join(" ");

// just continue doing your stuff with "words" array.
// ...​

現在のやり方よりもきれいだと思います。他に何か必要な場合は、この回答を更新します。

于 2012-08-01T03:32:22.223 に答える