この種の仕事で正規表現に勝るものはありません。しかし、それらには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
値で実験する必要があります。パフォーマンスが重要な場合は、生成部分を一度実行してから、結果(つまり生成された正規表現)をどこかにキャッシュすることもできます。
関連性:[John Resig's Series by Dictionary Lookups](http://ejohn.org/blog/revised-javascript-dictionary-search/) –
これらの「ストップワード」はいくつありますか?答えは重要である可能性があります。 – ChaosPandion
[JavaScriptコードを使用して一般的な単語をフィルタリングする](http://stackoverflow.com/questions/6686718/javascript-code-to-filter-out-common-words-in-a-string)ソリューションが文字列から辞書を構築することに注意してください。辞書を使い始めると、その部分をスキップすることができます。 – outis