首页 > 解决方案 > Python - Geotext - 无法识别“里约热内卢”等城市

问题描述

我正在尝试使用 GeoText 生成国家/地区提及,但无法识别里约热内卢、里约达斯奥斯特拉斯等城市。我查了字典,那里的城市还可以。

text = "Rio de Janeiro, Las Vegas, New York" geo = GeoText(text) print(geo.cities)

使用 python 3.x 和 geotext 0.3.0

标签: pythonpython-3.xmoduledata-mininggeotext

解决方案


GitHub repo 上的正则表达式和最新的 pip 安装版本 ( 0.3.0) 是不同的。

In[2]: import re
In[3]: text = "Rio de Janeiro, Las Vegas, New York"

# old regex (pip installed)
In[4]: city_regex = r"[A-Z]+[a-zà-ú]*(?:[ '-][A-Z]+[a-zà-ú]*)*"
In[5]: re.findall(city_regex, text)
Out[5]: ['Rio', 'Janeiro', 'Las Vegas', 'New York']

# new regex (GitHub)
In[6]: city_regex = r"[A-ZÀ-Ú]+[a-zà-ú]+[ \-]?(?:d[a-u].)?(?:[A-ZÀ-Ú]+[a-zà-ú]+)*"
In[7]: re.findall(city_regex, text)
Out[7]: ['Rio de Janeiro', 'Las Vegas', 'New York']

GitHub repos 正则表达式似乎即使对于三个单词的城市也能正常工作,但它并没有在 PyPI 的最新版本中使用。


推荐阅读