我需要將單詞分類爲他們的詞類。就像一個動詞,名詞,副詞等。 我用在Python中爲NLTK命名實體識別。識別NE
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
我得到上述錯誤。
我想要的是得到輸出爲'NE'類似於以前的'PRP',所以我無法確定哪個單詞是一個命名實體。 是否有任何方式與Python中的NLTK做這個?如果是這樣,請發佈命令。或者在樹庫中有一個函數來做到這一點?我需要的節點值'NE'
看着節點屬性之前,你要檢查isinstance(sent3 [2],樹) (在從nltk.tree導入樹之後)。 – Jacob 2011-04-19 16:00:56
@Jacob感謝隊友,真的很有幫助。我面臨的下一個問題是如何知道一個元素是否是一棵樹。因爲我需要使用for循環遍歷元素。 **如果isinstance(sent3 [2],樹)**是我一直在尋找這一切。再次感謝。當前版本(3.1)中的 – Asl506 2011-04-20 15:22:34
'node'被替換爲'label()' – Vladimir 2016-01-21 14:36:33