2012-02-22 5 views
0

文字列から数百の「停止」単語を除外する必要があります。多くの「停止」という言葉があるように私はこのような何かを行うことをお勧めだろうとは思わない:Javascript:辞書を使用して文字列から単語を除外しますか?

sentence.replace(/\b(?:the|it is|we all|an?|by|to|you|[mh]e|she|they|we...)\b/ig, ''); 

は、どのように私は言葉を止め格納するハッシュマップのようなものを作成することができますか?この地図では、キーはストップワードそのものになり、値は重要ではありません。その後、フィルタリングは単語がストップワードマップに存在しないかどうかをチェックする。このようなマップを作成するために使用するデータ構造は何ですか?

+0

関連性:[John Resig's Series by Dictionary Lookups](http://ejohn.org/blog/revised-javascript-dictionary-search/) –

+0

これらの「ストップワード」はいくつありますか?答えは重要である可能性があります。 – ChaosPandion

+0

[JavaScriptコードを使用して一般的な単語をフィルタリングする](http://stackoverflow.com/questions/6686718/javascript-code-to-filter-out-common-words-in-a-string)ソリューションが文字列から辞書を構築することに注意してください。辞書を使い始めると、その部分をスキップすることができます。 – outis

答えて

1

この種の仕事で正規表現に勝るものはありません。しかし、それらには2つの問題があります。あなたのポストで指摘したものと維持管理が難しいものと、非常に大きな問題を伴うパフォーマンスの問題です。私は1つの正規表現がどれだけ扱うことができるかを知ることはできませんが、20-30まではどんな場合でもうまくいくと思います。

したがって、いくつかのデータ構造から動的に正規表現を構築するコードが必要です。配列は配列でも文字列でも構いません。私は個人的には、それを維持するのが最も簡単なので、刺すのが好きです。

// taken from http://www.ranks.nl/resources/stopwords.html 
stops = "" 
+"a about above after again against all am an and any are aren't as " 
+"at be because been before being below between both but by can't " 
+"cannot could couldn't did didn't do does doesn't doing don't down " 
+"during each few for from further had hadn't has hasn't have  " 
+"haven't having he he'd he'll he's her here here's hers herself  " 
+"him himself his how how's i i'd i'll i'm i've if in into is isn't " 
+"it it's its itself let's me more most mustn't my myself no nor  " 
+"not of off on once only or other ought our ours ourselves out  " 
+"over own same shan't she she'd she'll she's should shouldn't so " 
+"some such than that that's the their theirs them themselves then " 
+"there there's these they they'd they'll they're they've this  " 
+"those through to too under until up very was wasn't we we'd we'll " 
+"we're we've were weren't what what's when when's where where's  " 
+"which while who who's whom why why's with won't would wouldn't  " 
+"you you'd you'll you're you've your yours yourself yourselves  " 

// how many to replace at a time 
reSize = 20 

// build regexps 
regexes = [] 
stops = stops.match(/\S+/g).sort(function(a, b) { return b.length - a.length }) 
for (var n = 0; n < stops.length; n += reSize) 
    regexes.push(new RegExp("\\b(" + stops.slice(n, n + reSize).join("|") + ")\\b", "gi")); 

あなたがこれを持ってたら、残りは明白です:

regexes.forEach(function(r) { 
    text = text.replace(r, '') 
}) 

あなたは正規表現の長さや正規表現の合計数との間の最適なバランスを見つけるためにreSize値で実験する必要があります。パフォーマンスが重要な場合は、生成部分を一度実行してから、結果(つまり生成された正規表現)をどこかにキャッシュすることもできます。

+0

ハッシュマップの単語を検索すると、正規表現を使用すると遅くなりますか?プレーンなJSオブジェクトをハッシュマップと見なすことができます。プロパティ名はマップキー、プロパティ値はマップ値です。 – dokondr

+0

正確に何をしているかによって異なりますが、ほとんどの場合、20単語を一度に置き換えると、1つずつ検索/削除するよりも速くなります。はい、javascriptオブジェクトはハッシュマップです。 – georg

+0

でも、JSの実行時に20単語の正規表現置換がどのように実装されるのでしょうか? IMOには、 'sentence'と 'stopWords'という2つの配列があります。とにかく文中のすべての単語はstopWordsのすべての単語と比較されます。私にひどく効率的に見えません。 – dokondr

関連する問題