首页 > 解决方案 > Geotext 在某些情况下检测国家,而不是在不同情况下检测同一个国家

问题描述

我正在尝试使用 Geotext 提取字符串中的所有国家/地区

它适用于一些句子,但不适用于某些句子。

我尝试在 Python 3.6 中做到这一点。

s="India Vs Ireland T20 Series"
s=GeoText(s)
s.countries

预期成绩:

['India','Ireland']

实际结果:

['Ireland']

标签: pythongeotext

解决方案


您可以将 pycountry 用于您的任务(它也适用于 python 3):

点安装 pycountry

import pycountry
text = "United States (New York), United Kingdom (London)"
for country in pycountry.countries:
    # Handle both the cases(Uppercase/Lowercase)
    if str(country.name).lower() in str(text).lower():
        print country.name

推荐阅读