【发布时间】:2018-01-24 16:32:19
【问题描述】:
我正在做一个项目,我需要在给定的文本文件中提取 locations。 我尝试了给定here 的命名实体识别示例。下面给出了它的代码sn-p。 但在这里它输出所有三个实体; 名称、地点和组织。是否有任何解决方案可以仅使用 python 提取位置?
import nltk
def extract_entity_names(t):
entity_names = []
if hasattr(t, 'label') and t.label:
if t.label() == 'NE':
entity_names.append(' '.join([child[0] for child in t]))
else:
for child in t:
entity_names.extend(extract_entity_names(child))
return entity_names
with open('sample.txt', 'r') as f:
for line in f:
sentences = nltk.sent_tokenize(line)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)
entities = []
for tree in chunked_sentences:
entities.extend(extract_entity_names(tree))
print(entities)
【问题讨论】:
-
spaCy 包非常适合这样的任务。见spacy.io/usage/linguistic-features
标签: python location named-entity-recognition