2015-11-12 18 views
5

私はnltkを使用して、指定されたストップワードを最初に削除して文からnグラムを生成しています。しかし、nltk.pos_tag()は、CPU(Intel i7)で0.6秒という非常に遅いです。POS-Taggerは非常に遅いです

出力:

['The first time I went, and was completely taken by the live jazz band and atmosphere, I ordered the Lobster Cobb Salad.'] 
0.620481014252 
["It's simply the best meal in NYC."] 
0.640982151031 
['You cannot go wrong at the Red Eye Grill.'] 
0.644664049149 

コード:

for sentence in source: 

    nltk_ngrams = None 

    if stop_words is not None: 
     start = time.time() 
     sentence_pos = nltk.pos_tag(word_tokenize(sentence)) 
     print time.time() - start 

     filtered_words = [word for (word, pos) in sentence_pos if pos not in stop_words] 
    else: 
     filtered_words = ngrams(sentence.split(), n) 

が、これは本当に遅いですか、私はここで何か間違ったことをやっていますか?複数の文をタグ付けするための

+0

あなたはそのテキストを投稿することができますあなたは入力がありますか?あなたのマシンの仕様(CPUの速度とRAM)は何ですか?あなたはクラウドに接続していますか、どのように機能をタイミングしていますか?また、http://stackoverflow.com/questions/33558836/pos-tagging-using-nltk-takes-time – alvas

+1

@alvasを参照してくださいそれは、Intel i7(質問に記載)です。 16GBのRAM。いいえ、それは雲の中にローカルではありません。私のコード例で、私がどのように時間を計測しているかを見ることができます。 – displayname

+0

巨大なデータセットがある場合は、ソリューションを並列化する必要があります。それ以外の場合(タグ付きの文章をRAMに保存することができれば)、すべてのタグ付き文章を収集してから、後でフィルタを実行するだけです。 – alvas

答えて

5

使用pos_tag_sents:あなたがPythonで高速なパフォーマンスを持つ別のPOSタガーを探しているなら

>>> import time 
>>> from nltk.corpus import brown 
>>> from nltk import pos_tag 
>>> from nltk import pos_tag_sents 
>>> sents = brown.sents()[:10] 
>>> start = time.time(); pos_tag(sents[0]); print time.time() - start 
0.934092998505 
>>> start = time.time(); [pos_tag(s) for s in sents]; print time.time() - start 
9.5061340332 
>>> start = time.time(); pos_tag_sents(sents); print time.time() - start 
0.939551115036 
+0

非常に良い! 'pos_tag_sents()'を使うと、パフォーマンスが大幅に向上します。なぜこれが当てはまるのか不思議です。 'pos_tag()'は何らかの初期化を繰り返すようです。 – displayname

+0

これはかなり長い回答の質問ですが、どうやってそれがなぜ速いのかという別の質問をすると、誰かが答えるかもしれません。もし誰も答えなければ、私は自由になるだろう - 私は明日の夕方に答えるでしょう=) – alvas

+0

[そこに行く](http://stackoverflow.com/questions/33829160/why-is-pos-tag-so-painfully -low-and-can-this-be-avoided);) – displayname

0

、あなたがRDRPOSTaggerをしようとする場合があります。たとえば、英語のPOSタグでは、Core 2Duo 2.4GHzのコンピュータを使用して、Pythonのシングルスレッド実装の場合、タギング速度は8Kワード/秒です。マルチスレッドモードを使用するだけで、タグ付けの速度を向上させることができます。 RDRPOSTaggerは、最先端のタガーと比較して非常に競争力のある精度を獲得し、現在は40の言語の事前訓練モデルをサポートしています。実験結果はthis paperです。

4
nltk pos_tag is defined as: 
from nltk.tag.perceptron import PerceptronTagger 
def pos_tag(tokens, tagset=None): 
    tagger = PerceptronTagger() 
    return _pos_tag(tokens, tagset, tagger) 

のでpos_tagへの各呼び出しは、計算time.Youの多くを取るperceptrontaggerモジュールが直接として自分自身をtagger.tag呼び出すことで、この時間を節約することができインスタンス化します。

from nltk.tag.perceptron import PerceptronTagger 
tagger=PerceptronTagger() 
sentence_pos = tagger.tag(word_tokenize(sentence)) 
+0

ありがとう、これはずっと速いです! – liang