python - Python - Geotext - 无法识别“里约热内卢”等城市
问题描述
我正在尝试使用 GeoText 生成国家/地区提及,但无法识别里约热内卢、里约达斯奥斯特拉斯等城市。我查了字典,那里的城市还可以。
- 输入:
text = "Rio de Janeiro, Las Vegas, New York"
geo = GeoText(text)
print(geo.cities)
输出:
- ['拉斯维加斯','纽约']
预期输出:
- ['里约热内卢','拉斯维加斯','纽约']
使用 python 3.x 和 geotext 0.3.0
解决方案
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 的最新版本中使用。
推荐阅读
- azure - Azure AD 应用程序需要管理员批准错误:应用程序需要访问组织中只有管理员才能授予的资源的权限
- python - Finding the row wise mean
- c# - 如何在 C# 中格式化字符串
- list - 这是什么数据?
- swift - 如何将(hstack)多个视频与 AVMutableVideoComposition 并排组合?
- vb.net - 整数值后的尾随百分比字符
- kotlin - Kotlin 错误“索引超出范围异常”
- netlogo - 基于代理和基于个体的建模(Railsback 和 Grimm,2019)NetLogo 商业模式
- fpdf - 我在第 526 行的 FPDF 文件中不断出现致命错误
- javascript - 使用 javascript 将 html 页面转换为 pdf 时,我无法看到图像