2016-12-30 11 views
1

ここに私のpythonコードは次のとおりです。ステミング/字形化を行うためにspacyを使用しても、一貫した結果が得られないのはなぜですか?

import spacy 
nlp = spacy.load('en') 
line = u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...' 
line = line.lower() 
print ' '.join([token.lemma_ for token in nlp(line)]) 

出力は次のとおりです。

algorithm ; deterministic algorithm ; adaptive algorithms ; something... 

はなぜ第三algorithmsは 'アルゴリズム' に変換されませんか? そして、私はlower()機能を削除すると、私はこれを取得:

algorithms ; deterministic algorithms ; adaptive algorithm ; something... 

今回は第一及び第二のalgorithmsは変換できませんでした。 この問題は私を夢中にさせますが、これを解決してすべての単語を字形にする方法はありますか?

答えて

2

どのバージョンをお使いですか? lowerで、それが私のために正しく動作:lowerなし

>>> doc = nlp(u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...'.lower()) 
>>> for word in doc: 
... print(word.text, word.lemma_, word.tag_) 
... 
(u'algorithms', u'algorithm', u'NNS') 
(u';', u';', u':') 
(u'deterministic', u'deterministic', u'JJ') 
(u'algorithms', u'algorithm', u'NNS') 
(u';', u';', u':') 
(u'adaptive', u'adaptive', u'JJ') 
(u'algorithms', u'algorithm', u'NN') 
(u';', u';', u':') 
(u'something', u'something', u'NN') 
(u'...', u'...', u'.') 

、鬼はAlgorithmsタグNNP、すなわち固有名詞を割り当てます。このモデルは、単語が固有名詞であると統計的に推測しているため、これを避けることができます。

tokenizerにspecial caseのルールを設定して、spaCyにAlgorithmsという名前が決して適切な名詞ではないことを伝えることができます。

from spacy.attrs import POS, LEMMA, ORTH, TAG 
nlp = spacy.load('en') 

nlp.tokenizer.add_special_case(u'Algorithms', [{ORTH: u'Algorithms', LEMMA: u'algorithm', TAG: u'NNS', POS: u'NOUN'}]) 
doc = nlp(u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...') 
for word in doc: 
    print(word.text, word.lemma_, word.tag_) 
(u'Algorithms', u'algorithm', u'NNS') 
(u';', u';', u':') 
(u'Deterministic', u'deterministic', u'JJ') 
(u'algorithms', u'algorithm', u'NN') 
(u';', u';', u':') 
(u'Adaptive', u'adaptive', u'JJ') 
(u'algorithms', u'algorithm', u'NNS') 
(u';', u';', u':') 
(u'Something', u'something', u'NN') 
(u'...', u'...', u'.') 

tokenizer.add_special_case機能を使用すると、文字の文字列をトークン化する方法を指定し、サブトークンのそれぞれの属性を設定することができます。

+0

おかげで、私はこの問題はスペイシーのバージョンによって引き起こされるかもしれないと思う、私は1.5.0にスペイシー1.3.0を更新することでこの問題を解決します。 –

0

私はsyllogism_がそれをより良く説明したと思います。しかし、ここでもう一つの方法です:

が出力
from nltk.stem import WordNetLemmatizer 

lemma = WordNetLemmatizer() 
line = u'Algorithms; Deterministic algorithms; Adaptive algorithms; Something...'.lower().split(';') 
line = [a.strip().split(' ') for a in line] 
line = [map(lambda x: lemma.lemmatize(x), l1) for l1 in line ] 
print line 

[[u'algorithm'], [u'deterministic', u'algorithm'], [u'adaptive', u'algorithm'], [u'something...']] 
関連する問題