Python字符串中提取国家名称
从文本中抽取国家名称,是一个命名实体识别的问题。但是因为国家个数有限,用机器学习大材小用,用直接匹配的方法看起来是快速且准确的选择。Python中也有第三方包可以调用,在这里整理一下相关资源。
Python从字符串串中如何提取国家、地区或者城市信息?中列出了两个方法。一个是Python工具包geotext,另一个是使用数据库自己匹配的方法。
geotext(0.4.0)
from geotext import GeoTextplaces = GeoText("London is a great city")
places.cities
# "London"# filter by country code
result = GeoText('I loved Rio de Janeiro and Havana', 'BR').cities
# 'Rio de Janeiro'GeoText('New York, Texas, and also China').country_mentions
# OrderedDict([(u'US', 2), (u'CN', 1)])
这个包的正确率很高,但是召回率不太行。识别出来的国家基本都是正确的,但是对于一些相对模糊的国家缩写识别不出来,比如USA和America都识别不出来,国家名称的变体比如Chinese也识别不出来。
GeoText('USA, America').countries
# []
geograpy3(0.2.2)
问题
1. sqlite3.OperationalError: no such table: countries
这应该是这个包的一个bug,在github的问题中有提到[BUG]OperationalError: no such table: countries #59,也有看到其它同样的提问geograpy3: sqlite3.OperationalError: no such table。是因为某个需要读取数据的数据库是空的,所以从中读取数据表失败。
解决方法:
- 下载locations.db
- 解压后放在
$HOME/.geograpy3
的文件夹下,并把文件夹中的db文件删除。我用的是windows系统,是在C:\Users\<Username>\.geograpy3
这个路径。
2. nltk下载
诸如‘chunkers/maxent_ne_chunker/english_ace_multiclass.pickle’ not found
此类。
因为我nltk下载总是失败,所以需要手动下载。在nltk_data网站,下载maxent_ne_chunker
压缩包,解压后放在提示缺失的Searched in
任意一个文件夹中就可以了。
3. UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa6 in position …
跳转到出错的源代码中,在open
的时候加个encoding=“utf8”
就解决了。
以上就是在初次使用过程中遇到的问题了。
使用方法
还是搬运一下github给出的示例。
方法一
geograpy.get_geoPlace_context
方法可以传入字符串(text参数),也可以传入需要提取信息的网址(url参数)。
- 国家、地区、城市等信息:使用
places.countries
查看国家,类似地可以查看地区places.regions
、查看城市places.cities
和其它places.other
。 - 地点之间关系的信息:使用
places.country_regions
查看按国家划分的地区,类似地可以查看按国家划分的城市places.country_cities
和与地理编码相关的字符串places.address_strings
。不过直观感受这个不太准。 - 国家、地区、城市及频次:使用
places.country_mentions
查看国家出现的次数,类似地还可以获取地区出现次数places.region_mentions
和城市出现次数places.city_mentions
。
这种方法可以识别出“xx国家的”这种所有格形式,比如“Chinese”,但是缩写识别不出来,比如“USA”。
import geograpytext = "China borders 14 nations and extends across much of East Asia, bordering Vietnam, Laos, and Myanmar (Burma) in Southeast Asia; India, Bhutan, Nepal, Afghanistan, and Pakistan in South Asia; Tajikistan, Kyrgyzstan and Kazakhstan in Central Asia; and Russia, Mongolia, and North Korea in Inner Asia and Northeast Asia. Additionally, China shares maritime boundaries with South Korea, Japan, Vietnam, and the Philippines."places = geograpy.get_geoPlace_context(text=text)places.countries
# ['Pakistan', 'Laos', 'Bhutan', 'Kyrgyzstan', 'Tajikistan', 'India', 'Russia', 'Japan', 'Afghanistan', 'South Korea', 'Mongolia', 'North Korea', 'Vietnam', 'Myanmar', 'Philippines', 'Kazakhstan', "People's Republic of China", 'United States of America', 'Indonesia', 'Mexico', 'Georgia', 'Moldova', 'Italy']places.country_regions
# {'Pakistan': [], 'Laos': [], 'Bhutan': [], 'Kyrgyzstan': [], 'Tajikistan': [], 'India': ['Asia', 'China'], 'Russia': [], 'Japan': [], 'Afghanistan': ['Pakistan'], 'South Korea': ['North Korea', 'South Asia', 'South Korea', 'Bhutan'], 'Mongolia': [], 'North Korea': ['North Korea', 'South Korea'], 'Vietnam': [], 'Myanmar': ['China'], 'Philippines': ['East Asia', 'South Asia', 'South Korea', 'North Korea'], 'Kazakhstan': [], "People's Republic of China": ['Mongolia', 'Inner Asia']}places.country_mentions
# [("People's Republic of China", 2), ('Myanmar', 2), ('Laos', 1), ('India', 1), ('Bhutan', 1), ('Afghanistan', 1), ('Pakistan', 1), ('Tajikistan', 1), ('Kyrgyzstan', 1), ('Kazakhstan', 1), ('Russia', 1), ('Mongolia', 1), ('North Korea', 1), ('South Korea', 1), ('Japan', 1), ('Vietnam', 1), ('Philippines', 1), ('United States of America', 1), ('Indonesia', 1), ('Mexico', 1), ('Georgia', 1), ('Moldova', 1), ('Italy', 1)]
方法二
先用extraction.Extractor
中的find_geoEntities()
方法找到地理实体,再利用places.PlaceContext
中的set_countries()
方法获得国家,可以pc.countries
获得国家列表,也可以统计出现的次数pc.country_mentions
,这种方法得到的地名更准一点。地区pc.set_regions()
、城市pc.set_cities()
和其它pc.set_other()
也可以查看,值得注意的是,这种方法可以识别一些缩写,比如“USA”,会在其它而非国家的列表中。
from geograpy import extraction, placestext = "China borders 14 nations and extends across much of East Asia, bordering Vietnam, Laos, and Myanmar (Burma) in Southeast Asia; India, Bhutan, Nepal, Afghanistan, and Pakistan in South Asia; Tajikistan, Kyrgyzstan and Kazakhstan in Central Asia; and Russia, Mongolia, and North Korea in Inner Asia and Northeast Asia. Additionally, China shares maritime boundaries with South Korea, Japan, Vietnam, and the Philippines."e = extraction.Extractor(text=text)
e.find_geoEntities()pc = places.PlaceContext(e.places)pc.set_countries()pc.countries
# ['Kyrgyzstan', 'Nepal', "People's Republic of China", 'South Korea', 'India', 'Myanmar', 'Russia', 'North Korea', 'Kazakhstan', 'Japan', 'Laos', 'Mongolia', 'Bhutan', 'Pakistan', 'Vietnam', 'Afghanistan', 'Tajikistan', 'Philippines']pc.country_mentions
# [("People's Republic of China", 2), ('Vietnam', 2), ('Myanmar', 2), ('Laos', 1), ('India', 1), ('Bhutan', 1), ('Nepal', 1), ('Afghanistan', 1), ('Pakistan', 1), ('Tajikistan', 1), ('Kyrgyzstan', 1), ('Kazakhstan', 1), ('Russia', 1), ('Mongolia', 1), ('North Korea', 1), ('South Korea', 1), ('Japan', 1), ('Philippines', 1)]
Python字符串中提取国家名称相关推荐
- Python中的函数及Python字符串中提取字母、数字
一.函数:就是对功能或动作的封装,一段有规律.重复使用的代码. 函数的定义: def 函数名(形参): 函数体 返回值(return) 函数的调用: ret=函数名(实参) 函数的返回值 return ...
- python关键词提取_如何从Python格式字符串中提取关键字? - python
我想在API中提供自动字符串格式,例如: my_api("path/to/{self.category}/{self.name}", ...) 可以替换为格式化字符串中标注的属性值 ...
- python正则表达式提取数字比较好_python正则表达式从字符串中提取数字的思路详解...
python从字符串中提取数字 使用正则表达式,用法如下: ## 总结 ## ^ 匹配字符串的开始. ## $ 匹配字符串的结尾. ## \b 匹配一个单词的边界. ## \d 匹配任意数字. ## ...
- python 从字符串中提取数字 re.findall()
以前老用(.*?)提取数字,今天发现不对了,比如一行数字为: 0 0.248438 0.255556 0.128125 0.194444 用: re.findall('(.*?) (.*?) (.*? ...
- python电话号码对应的字符组合_Python3 在字符串中提取字母+数字组合微信账号、电话等 - pytorch中文网...
今天处理数据要提取字符串中的微信,字符串中包含中文英文Emoji,标点符号等. python 提取字符串中的电话 提取电话相对简单,多个电话也可以提取 import re desstr = " ...
- python文本分析 提取数据含义_从文本字符串中提取数据进行分析
需求 在进行数据分析的时候,有时候会碰到需要从文本字符串中提取需要的数据来进行分析的情况,这种需求在网络爬虫数据分析非常常见. 比如,需要下列表格"基础薪资规则"字段中提取阶梯单量 ...
- 正则匹配——python用一个正则表达式从字符串中提取数字(包括整数、小数、正负数)
import re# 从字符串中提取数字 totalCount = '-100,abc2.4-123s,d-1ds-0.234as123.2s1.3bb.24' count = re.findall( ...
- Python:如何从字符串中提取字母或数字?
从字符串中提取字母 s = 'cn中国520' print(''.join([i for i in s if i.encode('UTF-8').isalpha()]))# 输出:cn 注意:中文的汉 ...
- 文本字符串中提取数据进行分析
需求 在进行数据分析的时候,有时候会碰到需要从文本字符串中提取需要的数据来进行分析的情况,这种需求在网络爬虫数据分析非常常见. 比如,需要下列表格"基础薪资规则"字段中提取阶梯单量 ...
最新文章
- 刚毕业就20w年薪,这个项目太牛逼了!
- 不吹牛,中国车主已经实现了「停车自由」
- 车仪表台上的装饰_仪表台放这个东西,相当于定时炸弹,很多人都忽略了
- SPICE:过程改进的又一种选择
- 刚构桥的优缺点_连续刚构桥相对于连续梁来说有优点吗
- html文本显示状态代码中,HTML文本显示状态代码中,表示?
- vue (可读写)全局变量的 定义、任意调用、值的修改
- 【踩坑经历】一次Asp.NET小网站部署踩坑和解决经历
- 前端同时访问后端同一接口两次,但只有后面那次请求有返回数据
- dsn服务器显示问题,DNS 服务器出现4004错误解决方法
- c语言入门视频教程-
- gartner 服务器虚拟化 市场份额 2013,Gartner:VMware成虚拟化魔力象限领导者
- 关键字查询地理经纬度及省份-市
- 三相同步电机怎么接线图_实用!41例电动机接线方法-清晰大图(上)
- fbx模型压缩成gltf格式
- Win2000 Server***监测
- php中关于js保存文件至本地的问题
- 安卓逆向_6 --- Dalvik 字节码、Smali 详解
- java安卓字体_Android中添加外部字体库和竖直排列字体
- 计算机不显示磁盘阵列,识别不到硬盘?bios中怎么改raid为ahci硬盘模式。