20

長いテキスト文字列を解析して、レシピのタグ (キーワード) を見つけようとしています。テキストには、レシピの材料、指示、および短い宣伝文句が含まれています。

タグリストから一般的な単語を削除する最も効率的な方法は何だと思いますか?

一般的な言葉とは、「the」、「at」、「there」、「their」などの言葉を意味します。

使用できる方法が 2 つあります。どちらが速度の点でより効率的だと思いますか?また、これを行うためのより効率的な方法を知っていますか?

方法 1:
- 各単語が出現する回数を決定します (ライブラリ Collections を使用)
- 一般的な単語のリストを用意し、コレクション オブジェクトからそのキーを削除しようとすることで、コレクション オブジェクトからすべての「一般的な単語」を削除します (存在する場合)。
- したがって、速度は変数 delims の長さによって決まります

import collections from Counter
delim     = ['there','there\'s','theres','they','they\'re'] 
# the above will end up being a really long list!
word_freq = Counter(recipe_str.lower().split())
for delim in set(delims):
    del word_freq[delim]
return freq.most_common()

方法 2:
- 複数形の可能性がある一般的な単語については、レシピ文字列の各単語を調べて、一般的な単語の複数形でないバージョンが部分的に含まれているかどうかを確認します。例えば; 文字列「There's a test」について、各単語に「there」が含まれているかどうかを確認し、含まれている場合は削除します。

delim         = ['this','at','them'] # words that cant be plural
partial_delim = ['there','they',] # words that could occur in many forms
word_freq     = Counter(recipe_str.lower().split())
for delim in set(delims):
    del word_freq[delim]
# really slow 
for delim in set(partial_delims):
    for word in word_freq:
        if word.find(delim) != -1:
           del word_freq[delim]
return freq.most_common()
4

3 に答える 3

33

私はちょうどこのようなことをしたい:

from nltk.corpus import stopwords
s=set(stopwords.words('english'))

txt="a long string of text about him and her"
print filter(lambda w: not w in s,txt.split())

印刷する

['long', 'string', 'text']

複雑さの点では、ハッシュされたセットのルックアップが O(1) であると思われる場合、文字列内の単語数は O(n) である必要があります。

FWIW、NLTK の私のバージョンでは 127 個のストップワードが定義されています

'all', 'just', 'being', 'over', 'both', 'through', 'yourselves', 'its', 'before', 'herself', 'had', 'should', 'to', 'only', 'under', 'ours', 'has', 'do', 'them', 'his', 'very', 'they', 'not', 'during', 'now', 'him', 'nor', 'did', 'this', 'she', 'each', 'further', 'where', 'few', 'because', 'doing', 'some', 'are', 'our', 'ourselves', 'out', 'what', 'for', 'while', 'does', 'above', 'between', 't', 'be', 'we', 'who', 'were', 'here', 'hers', 'by', 'on', 'about', 'of', 'against', 's', 'or', 'own', 'into', 'yourself', 'down', 'your', 'from', 'her', 'their', 'there', 'been', 'whom', 'too', 'themselves', 'was', 'until', 'more', 'himself', 'that', 'but', 'don', 'with', 'than', 'those', 'he', 'me', 'myself', 'these', 'up', 'will', 'below', 'can', 'theirs', 'my', 'and', 'then', 'is', 'am', 'it', 'an', 'as', 'itself', 'at', 'have', 'in', 'any', 'if', 'again', 'no', 'when', 'same', 'how', 'other', 'which', 'you', 'after', 'most', 'such', 'why', 'a', 'off', 'i', 'yours', 'so', 'the', 'having', 'once'

明らかに、独自のセットを提供できます。これよりも多くの単語を削除したい場合を除き、削除したいすべてのバリエーションを前もって提供するのがおそらく最も簡単(かつ最速)であるというあなたの質問に対するコメントに同意しますが、それはより多くの質問になります偽のものを排除するよりも、興味深いものを見つけることです。

于 2012-04-07T23:45:04.510 に答える
1

あなたは速度について尋ねますが、正確さをもっと気にするべきです。どちらの提案も多くの間違いを犯し、削除が多すぎたり少なすぎたりします (たとえば、部分文字列 "at" を含む単語がたくさんあります)。nltk モジュールを調べるという提案に賛成です。実際、NLTK 本の初期の例の 1 つは、最も一般的な残りの単語がそのジャンルについて何かを明らかにするまで、一般的な単語を削除することです。ツールだけでなく、その方法についても説明します。

いずれにせよ、コンピュータがプログラムを実行するよりもはるかに長い時間をプログラムの作成に費やすことになるので、プログラムをうまく作成することに集中してください。

于 2012-04-07T23:24:27.687 に答える