2017-02-13 9 views
-2

私はこのJavaScriptコードを使用しようとしている:テキストからすべてのストップワードを削除するには?

var aStopWords = new Array ("a", "the", "blah"...); 

(code to make it run, full code can be found here: https://jsfiddle.net/j2kbpdjr/) 

// sText is the body of text that the keywords are being extracted from. 
// It's being separated into an array of words. 

// remove stop words 
for (var m = 0; m < aStopWords.length; m++) { 
    sText = sText.replace(' ' + aStopWords[m] + ' ', ' '); 
} 

テキストの本文からキーワードを取得します。しかし、かなりうまくいきますが、私が抱えている問題は、配列aStopWordsの単語の1つのインスタンスを繰り返して無視するように見えるということです。

だから私は、テキストの次の体がある場合:

how are you today? Are you well?

を私はvar aStopWords = new Array("are","well")を置くが、それはareの最初のインスタンスを無視し、まだキーワードとして第2 areが表示されますようです。それは、キーワードから完全に除外/無視されますが、wellです。

キーワードからaStopWordsの単語のすべてのインスタンスを無視するのに役立つ人は、大変ありがとうございます。

+0

テキストから単語のリストのすべての発生を削除するにはあなたの目標ですか? –

+0

@ T.J.Crowder、Apologies。私は質問を更新しました。 – Jack

+0

@ ssc-hrep3うん、そうだよ – Jack

答えて

1

このように簡単に行うことができます。

まず、テキストをキーワードに分割します。それから、すべてのキーワードを通過します。通過中に、ストップワードかどうかをチェックします。もしそうなら、それは無視されます。そうでない場合は、resultオブジェクト内のこのキーワードのオカレンス番号が増加します。

{ "this": 1, "that": 2 } 

オブジェクトをJavaScriptでソート可能ではありませんが、配列は以下のとおりです。

はその後、キーワードは次の形式でJavaScriptオブジェクトです。だから、次のような構造に再マッピングが必要です:

[ 
    { "keyword": "this", "counter": 1 }, 
    { "keyword": "that", "counter": 2 } 
] 

その後、アレイはcounter属性を使用してソートすることができます。 slice()関数を使用すると、ソートされたリストから上位のX値のみを抽出できます。

var stopwords = ["about", "all", "alone", "also", "am", "and", "as", "at", "because", "before", "beside", "besides", "between", "but", "by", "etc", "for", "i", "of", "on", "other", "others", "so", "than", "that", "though", "to", "too", "trough", "until"]; 
 
var text = document.getElementById("main").innerHTML; 
 

 
var keywords = text.split(/[\s\.;:"]+/); 
 
var keywordsAndCounter = {}; 
 
for(var i=0; i<keywords.length; i++) { 
 
    var keyword = keywords[i]; 
 
    
 
    // keyword is not a stopword and not empty 
 
    if(stopwords.indexOf(keyword.toLowerCase()) === -1 && keyword !== "") { 
 
    if(!keywordsAndCounter[keyword]) { 
 
     keywordsAndCounter[keyword] = 0; 
 
    } 
 
    keywordsAndCounter[keyword]++; 
 
    } 
 
} 
 

 
// remap from { keyword: counter, keyword2: counter2, ... } to [{ "keyword": keyword, "counter": counter }, {...} ] to make it sortable 
 
var result = []; 
 
var nonStopKeywords = Object.keys(keywordsAndCounter); 
 
for(var i=0; i<nonStopKeywords.length; i++) { 
 
    var keyword = nonStopKeywords[i]; 
 
    result.push({ "keyword": keyword, "counter": keywordsAndCounter[keyword] }); 
 
} 
 

 
// sort the values according to the number of the counter 
 
result.sort(function(a, b) { 
 
    return b.counter - a.counter; 
 
}); 
 

 
var topFive = result.slice(0, 5); 
 
console.log(topFive);
<div id="main">This is a test to show that it is all about being between others. I am there until 8 pm event though it will be late. Because it is "cold" outside even though it is besides me.</div>

+0

ありがとう!それはストップワードのすべてのインスタンスを削除するために完全に動作します、私はこれ(痛みであることに申し訳ありません)と一緒に持っている1つの問題があります。問題は、再発する単語の上位X個だけではなく、すべてのノンストップワードがリストされていることです。 – Jack

+0

@Jack、私は以下の答えを更新しました:問題は、オブジェクトをソートできないため、オブジェクトから配列(オブジェクトを含む)に変換する必要があるということです。 –

+0

ありがとうございます! – Jack

関連する問題