Zlo*_*Zlo 14 python nlp named-entity-recognition nltk
我使用NLTK ne_chunk从文本中提取命名实体:
my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."
nltk.ne_chunk(my_sent, binary=True)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何将这些实体保存到列表中?例如 -
print Entity_list
('WASHINGTON', 'New York', 'Loretta', 'Brooklyn', 'African')
Run Code Online (Sandbox Code Playgroud)
谢谢.
alv*_*vas 30
nltk.ne_chunk返回一个嵌套nltk.tree.Tree对象,因此您必须遍历该Tree对象才能到达NE.
>>> from nltk import ne_chunk, pos_tag, word_tokenize
>>> from nltk.tree import Tree
>>>
>>> def get_continuous_chunks(text):
... chunked = ne_chunk(pos_tag(word_tokenize(text)))
... continuous_chunk = []
... current_chunk = []
... for i in chunked:
... if type(i) == Tree:
... current_chunk.append(" ".join([token for token, pos in i.leaves()]))
... elif current_chunk:
... named_entity = " ".join(current_chunk)
... if named_entity not in continuous_chunk:
... continuous_chunk.append(named_entity)
... current_chunk = []
... else:
... continue
... return continuous_chunk
...
>>> my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."
>>> get_continuous_chunks(my_sent)
['WASHINGTON', 'New York', 'Loretta E. Lynch', 'Brooklyn']
Run Code Online (Sandbox Code Playgroud)
ima*_*bet 10
您还可以label使用以下代码提取文本中每个名称实体的内容:
import nltk
for sent in nltk.sent_tokenize(sentence):
for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent))):
if hasattr(chunk, 'label'):
print(chunk.label(), ' '.join(c[0] for c in chunk))
Run Code Online (Sandbox Code Playgroud)
输出:
GPE WASHINGTON
GPE New York
PERSON Loretta E. Lynch
GPE Brooklyn
Run Code Online (Sandbox Code Playgroud)
你可以看到Washington,New York并且Brooklyn是GPE指地缘政治实体
而且Loretta E. Lynch是一个PERSON
当你得到 atree作为返回值时,我猜你想选择那些标有NE
这是一个简单的示例,用于收集列表中的所有内容:
import nltk
my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."
parse_tree = nltk.ne_chunk(nltk.tag.pos_tag(my_sent.split()), binary=True) # POS tagging before chunking!
named_entities = []
for t in parse_tree.subtrees():
if t.label() == 'NE':
named_entities.append(t)
# named_entities.append(list(t)) # if you want to save a list of tagged words instead of a tree
print named_entities
Run Code Online (Sandbox Code Playgroud)
这给出:
[Tree('NE', [('WASHINGTON', 'NNP')]), Tree('NE', [('New', 'NNP'), ('York', 'NNP')])]
Run Code Online (Sandbox Code Playgroud)
或作为列表列表:
[[('WASHINGTON', 'NNP')], [('New', 'NNP'), ('York', 'NNP')]]
Run Code Online (Sandbox Code Playgroud)
另请参阅:如何导航 nltk.tree.Tree?
小智 5
使用 nltk.chunk 中的 tree2conlltags。ne_chunk 还需要 pos 标记来标记单词标记(因此需要 word_tokenize)。
from nltk import word_tokenize, pos_tag, ne_chunk
from nltk.chunk import tree2conlltags
sentence = "Mark and John are working at Google."
print(tree2conlltags(ne_chunk(pos_tag(word_tokenize(sentence))
"""[('Mark', 'NNP', 'B-PERSON'),
('and', 'CC', 'O'), ('John', 'NNP', 'B-PERSON'),
('are', 'VBP', 'O'), ('working', 'VBG', 'O'),
('at', 'IN', 'O'), ('Google', 'NNP', 'B-ORGANIZATION'),
('.', '.', 'O')] """
Run Code Online (Sandbox Code Playgroud)
这会给你一个元组列表: [(token, pos_tag, name_entity_tag)] 如果这个列表不是你想要的,那么从这个列表中解析你想要的列表肯定更容易,然后是 nltk 树。
此链接中的代码和详细信息;查看更多信息
您也可以通过仅提取单词来继续,使用以下功能:
def wordextractor(tuple1):
#bring the tuple back to lists to work with it
words, tags, pos = zip(*tuple1)
words = list(words)
pos = list(pos)
c = list()
i=0
while i<= len(tuple1)-1:
#get words with have pos B-PERSON or I-PERSON
if pos[i] == 'B-PERSON':
c = c+[words[i]]
elif pos[i] == 'I-PERSON':
c = c+[words[i]]
i=i+1
return c
print(wordextractor(tree2conlltags(nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sentence))))
Run Code Online (Sandbox Code Playgroud)
编辑添加输出文档字符串 **编辑* 仅为 B-Person 添加输出
| 归档时间: |
|
| 查看次数: |
37877 次 |
| 最近记录: |