【问题标题】:How to get the location names(only) using Named Entity Recognition?如何使用命名实体识别(仅)获取位置名称?
【发布时间】: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)

【问题讨论】:

标签: python location named-entity-recognition


【解决方案1】:

您需要训练命名实体识别 (NER) 来执行此操作。 NLTK 工具包会给你演讲的部分内容,而不是它的名词类型

如果您正在寻找更快的解决方案。我会推荐geotext

from geotext import GeoText
sentence = "my foreigner New York Canberra Sydney Australia, Japan, Fujimoto Godfather Avatar"
places = GeoText(sentence)
print places.countries
print places.cities

【讨论】:

    猜你喜欢
    • 2014-03-17
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2011-07-31
    • 2018-03-08
    • 2020-07-02
    • 1970-01-01
    • 2014-01-14
    相关资源
    最近更新 更多