2017-11-26 14 views
0

見つける:私がやろうとしていますスペイシーバージョン2.0でトークン化された文章から名付けられたエンティティ

    テキスト
  • 計算から
  • トークン化文の文章

に存在する各単語の名前付きエンティティのこれがされ何私がこれまで行ってきた:

nlp = spacy.load('en') 
sentence = "Germany and U.S.A are popular countries. I am going to gym tonight" 
sentence = nlp(sentence) 
tokenized_sentences = [] 
for sent in sentence.sents: 
     tokenized_sentences.append(sent) 
for s in tokenized_sentences: 
     labels = [ent.label_ for ent in s.ents] 
     entities = [ent.text for ent in s.ents] 

エラー:

labels = [ent.label_ for ent in s.ents] 
    AttributeError: 'spacy.tokens.span.Span' object has no attribute 'ents' 

トークン化された文の名前付きエンティティを見つける方法はありますか?

ありがとうございます。

答えて

1

米国とドイツの2つのエンティティのみが存在します。

簡易版:

sentence = nlp("Germany and U.S.A are popular countries. I am going to gym tonight")  
for ent in sentence.ents: 
     print(ent.text, ent.label_) 

私はあなたがする抱き合わせていると思うもの:

sentence = nlp("Germany and U.S.A are popular countries. I am going to gym tonight") 
for sent in sentence.sents: 
    tmp = nlp(str(sent)) 
    for ent in tmp.ents: 
     print(ent.text, ent.label_) 
+2

一つの小さな改善:代わりに 'STR(送られた)'で、それが明示的に '送っを使用するクリーナーです.text_'は、文のテキストを与えます。オブジェクト上で 'str'を呼び出すと、望ましくない副作用が発生しやすくなります。たとえば、Python 2では、これはUnicode文字列ではなくバイト文字列を返します。 –

関連する問題