在Spacy Ner中区分国家和城市 [英] Differentiate between countries and cities in spacy NER

查看:13
本文介绍了在Spacy Ner中区分国家和城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spacy NER从组织地址中提取国家/地区,然而,它使用相同的标签GPE标记国家/地区和城市。有什么方法可以区分它们吗?

例如:

nlp = en_core_web_sm.load()

doc= nlp('Resilience Engineering Institute, Tempe, AZ, United States; Naval Postgraduate School, Department of Operations Research, Monterey, CA, United States; Arizona State University, School of Sustainable Engineering and the Built Environment, Tempe, AZ, United States; Arizona State University, School for the Future of Innovation in Society, Tempe, AZ, United States')

for ent in doc.ents:
    if ent.label_ == 'GPE':
        print(ent.text)

回馈

Tempe
AZ
United States
United States
Tempe
AZ
United States
Tempe
AZ
United States

推荐答案

如其他答案所述,预先培训的Spacy模式的GPE适用于国家/地区、城市和州。但是,有一种解决办法,我相信可以使用几种方法。

一种方法:您可以向模型添加自定义标记。Towards Data Science上有一篇很好的文章可以帮助你做到这一点。为此收集培训数据可能很麻烦,因为您需要在句子中根据城市/国家/地区的各自位置来标记它们。我引用Stack Overflow的答案:

Spacy Ner模型培训包括提取其他&Quot;隐式&Quot;特征,如词性和周围单词。

当您尝试针对单个单词进行训练时,无法获得足够的通用特征来检测这些实体。

以下是解决此问题的一个更简单的解决方法:

安装geonamescache

pip install geonamescache

然后使用以下代码获取国家和城市列表

import geonamescache

gc = geonamescache.GeonamesCache()

# gets nested dictionary for countries
countries = gc.get_countries()

# gets nested dictionary for cities
cities = gc.get_cities()

文档说明您还可以获得许多其他位置选项。

使用以下函数从嵌套词典(从该answer获取)中获取某个名称的键的所有值

def gen_dict_extract(var, key):
    if isinstance(var, dict):
        for k, v in var.items():
            if k == key:
                yield v
            if isinstance(v, (dict, list)):
                yield from gen_dict_extract(v, key)
    elif isinstance(var, list):
        for d in var:
            yield from gen_dict_extract(d, key)

分别加载citiescountries的两个列表。

cities = [*gen_dict_extract(cities, 'name')]
countries = [*gen_dict_extract(countries, 'name')]

然后使用以下代码进行区分:

nlp = spacy.load("en_core_web_sm")

doc= nlp('Resilience Engineering Institute, Tempe, AZ, United States; Naval Postgraduate School, Department of Operations Research, Monterey, CA, United States; Arizona State University, School of Sustainable Engineering and the Built Environment, Tempe, AZ, United States; Arizona State University, School for the Future of Innovation in Society, Tempe, AZ, United States')

for ent in doc.ents:
    if ent.label_ == 'GPE':
        if ent.text in countries:
            print(f"Country : {ent.text}")
        elif ent.text in cities:
            print(f"City : {ent.text}")
        else:
            print(f"Other GPE : {ent.text}")

输出:

City : Tempe
Other GPE : AZ
Country : United States
Country : United States
City : Tempe
Other GPE : AZ
Country : United States
City : Tempe
Other GPE : AZ
Country : United States

这篇关于在Spacy Ner中区分国家和城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆