如何从文本中提取国家?

问题描述 投票:4回答:1

我使用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从文本中提取所需的信息。

python python-3.x nltk geography
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.