単語を品詞に分類する必要があります。動詞、名詞、副詞などのように。私は
nltk.word_tokenize() #to identify word in a sentence
nltk.pos_tag() #to identify the parts of speech
nltk.ne_chunk() #to identify Named entities.
これの出力は木です。例えば
>>> sentence = "I am Jhon from America"
>>> sent1 = nltk.word_tokenize(sentence )
>>> sent2 = nltk.pos_tag(sent1)
>>> sent3 = nltk.ne_chunk(sent2, binary=True)
>>> sent3
Tree('S', [('I', 'PRP'), ('am', 'VBP'), Tree('NE', [('Jhon', 'NNP')]), ('from', 'IN'), Tree('NE', [('America', 'NNP')])])
このツリーの要素にアクセスするとき、私は次のようにアクセスしました。
>>> sent3[0]
('I', 'PRP')
>>> sent3[0][0]
'I'
>>> sent3[0][1]
'PRP'
ただし、名前付きエンティティにアクセスする場合:
>>> sent3[2]
Tree('NE', [('Jhon', 'NNP')])
>>> sent3[2][0]
('Jhon', 'NNP')
>>> sent3[2][1]
Traceback (most recent call last):
File "<pyshell#121>", line 1, in <module>
sent3[2][1]
File "C:\Python26\lib\site-packages\nltk\tree.py", line 139, in __getitem__
return list.__getitem__(self, index)
IndexError: list index out of range
上記のエラーが発生しました。
私が欲しいのは、前の「PRP」と同様の「NE」として出力を取得することです。そのため、どの単語が名前付きエンティティであるかを識別できません。PythonのNLTKでこれを行う方法はありますか?もしそうなら、コマンドを投稿してください。または、これを行うための関数がツリーライブラリにありますか?ノード値「NE」が必要です