如何从文本中提取国家?

Mar*_*kus 6 python geography nltk python-3.x

我使用Python 3(我也安装了Python 2),我想从短文本中提取国家或城市.例如,text = "I live in Spain"text = "United States (New York), United Kingdom (London)".

各国答案:

  1. 西班牙
  2. [美国,英国]

我试图安装,geography但我无法运行pip install geography.我收到此错误:

收集地理位置找不到满足要求地理位置的版本(来自版本:)没有找到地理位置的匹配分布

它看起来geography只适用于Python 2.

我也有geopandas,但我不知道如何使用geopandas从文本中提取所需的信息.

mat*_*yas 11

你可以使用pycountry来完成你的任务(它也适用于python 3):

pip install pycountry

import pycountry
text = "United States (New York), United Kingdom (London)"
for country in pycountry.countries:
    if country.name in text:
        print(country.name)
Run Code Online (Sandbox Code Playgroud)