我有一些文本,其中可能包含一个国家的名称。例如:
“尼日利亚:热点网络有限公司农村电话可行性研究”
这是我从中提取国家名称的方法,在第一次尝试中:
findcountry("Nigeria: Hotspot Network LTD Rural Telephony Feasibility Study")
def findCountry(stringText):
for country in pycountry.countries:
if country.name.lower() in stringText.lower():
return country.name
return None
很遗憾,它给了我错误的输出,显示为
[尼日尔]
,而正确的国家是尼日利亚。请注意,尼日尔和尼日利亚是世界上两个不同的现有国家。在第二次尝试中:
def findCountry(stringText):
full_list =[]
for country in pycountry.countries:
if country.name.lower() in stringText.lower():
full_list.append(country)
if len(full_list) > 0:
return full_list
return None
我得到了['Niger', 'Nigeria']
作为输出,但我找不到一种方法来获取尼日利亚作为我的最终输出。如何实现此目标。
注意:这里我知道尼日利亚是正确答案,但后面我会将其放入代码中,以选择文本中出现的最终国家名称,并且它应该具有非常高的检测精度。