Skip to main content
 首页 » 编程设计

python之识别文本中的双词城市 ('New York' )

2024年11月24日9emanlee

对于此代码,我获得了一个包含多个城市的文本文件。我想确定提到的城市并打印他们的州和国家。

要求: 如果提到的城市位于两个或多个国家/地区,我会要求用户提及他们正在谈论的城市。此外,如果有轻微的打字错误,我会询问用户是否指的是某个城市。例如,如果他们键入“Dalls”而不是“Dallas”,我需要为用户提供选项,例如“你是说 Dallas 而不是 Dalls”。

问题: 到目前为止,我已经成功地满足了这些条件,但是当涉及到识别两个措辞城市,如“纽约”或“旧金山”时,我的程序无法做到。这是因为我正在逐字逐句地阅读全文。如果您对如何更好地阅读全文有任何建议,请告诉我。

P.S.(我知道可以使用更高级的 Python 方法来简化代码,但我对 Python 的了解还没有达到那个水平。但是请告诉我我还能如何简化我的代码程序,因为我觉得现在没有必要冗长。谢谢!)

文件说明: 我正在使用名为“world-cities.csv”、“TEXT.txt”和“usa.txt”的文本。 'world-cities.csv' 是一个包含世界上许多城市的文件。 “TEXT.txt”是一个文件,其中包含我将针对城市分析的句子。 'usa.txt' 包含英语中的常用词。我用它与“TEXT.txt”进行比较以删除常用词。我对诸如“和”之类的单词显示为拼写错误有疑问。所以这是一种摆脱它们的盗版方法。

文本文件: 今天我去了海得拉巴,然后去了美国的金奈和纽约。现在我要去东京旅行,明天要回到罗彻斯特。达尔斯和悉尼是我的下一个目的地。

我使用了 Geotext,它可以工作,但在读取“纽约”等城市时会出现问题。我在程序中没有地理文本的部分在“约克”中读取,当我添加地理文本时,它显示为“纽约”。结果,我的城市列表中有“约克”和“纽约”。有人告诉我可以使用 NLTK 包,但我仍在寻找一种有效的方法来做到这一点。

在此输入验证码

import pandas as pd 
import re 
 
 
#imported dataset 
dataset = pd.read_csv('world-cities.csv') 
 
#assigned certain parts of data set to variable 
data = dataset.iloc[:,:-1] 
city = dataset.iloc[:,0] 
state = dataset.iloc[:,2] 
country = dataset.iloc[:,1] 
 
 
#opened and imported textfile 
txtfile = open('TEXT.txt','r') 
txtfile = txtfile.read() 
words = open('usa.txt','r') 
words = words.read() 
 
 
#getting rid of punctation 
altered = re.sub("[.,:]",'',txtfile) 
templist = [] #holds the cities(state and country) info of the places  
final = [] #final array 
all_cities = [] #used to check for repeating cities 
repeat = {} #contains only city names 
repeatinfo = [] #contain all infor about repeating cities 
stupid = 0 
close = 0 
typo = [] 
typodict = {} 
typecount = 0 
finaltypo = [] 
 
#finding out where the talked about cities are  
for x in altered.split(): 
    count = 0 
    zcount = 0 
    for y in city: 
        if x == y: 
            zcount +=1 
            templist.append([city[count], state[count], country[count]]) 
            all_cities.append(city[count]) 
        count+=1 
     if zcount > 1: 
        repeat[x] = zcount 
 
#put in all assumed Typos 
for x in altered.split(): 
    if x not in all_cities: 
        x = x.lower() 
        if x not in words: 
            typo.append(x) 
 
 
#narrow down options of typos 
many = 0 
for a in typo: 
    for b in city: 
        b = b.lower() 
        if len(a) >= (len(b)-1) and len(a) <= (len(b)+1): 
            if a[0] == b[0] or a[-1::] == b[-1::]: 
                if a[0:3] == b[0:3] or a[-3::] == b[-3::]: 
                    #print(f'{a} vs {b}') 
                    many = 0 
                    for x in a: 
                        if x in b: 
                            many+=1 
                        if many >= (len(b)-1) and many <= (len(b)+1): 
                            typodict[b] = a 
 
#let user choose if it is a typo or not 
print('TYPO Checking') 
for a in typo: 
    p =0 
    q = 0 
    while(p < len(typo) and q == 0): 
        for x,y in typodict.items(): 
            go2 = True 
            while(go2 and q==0): 
                if y == a: 
                    user2 = input(f" Did you mean to type '{x}' instead of  
'{y}'? Enter 'y' or 'n': ") 
                    user2 = user2.lower() 
                    if user2 == 'y': 
                        go2 = False 
                        finaltypo.append(x) 
                        p+=1 
                        q+=1 
                    elif user2 == 'n': 
                        go2 = False 
                    else:  
                        print('You have entered a invalid value') 
                else: 
                    go2 = False 
 
 
 
#adding typoed cities into list 
for x in finaltypo: 
    x = x.capitalize() 
    count = 0 
    zcount = 0 
    for y in city: 
        if x == y: 
            zcount +=1 
            templist.append([city[count], state[count], country[count]]) 
            all_cities.append(city[count]) 
        count+=1 
    if zcount > 1: 
        repeat[x] = zcount 
 
#finding out what cities repeat and adding all their information to repeat  
info 
for x in repeat: 
    rcount = 0 
    for y in city: 
        if x == y:  
            repeatinfo.append([city[rcount], state[rcount],  
country[rcount]]) 
        rcount +=1 
 
#determining which country they mean when they mentioned repeating cities 
print('Which City?') 
for x,y in repeat.items(): 
    i = 0 
    e = 0 
    while(i < y and e == 0): 
        go = True 
        for c in repeatinfo:  
            go = True 
            while(go and e == 0): 
                if x == c[0]: 
                    user = input(f'Do you mean {x} in {c[1]},{c[2]} enter y  
or n: ') 
                    user = user.lower() 
                    i +=1 
                    if user == 'y': 
                        final.append(f' {x} in {c[1]}, {c[2]}') 
                        go = False 
                        i +=1 
                        e +=1 
                    elif user == 'n': 
                        go = False 
                        i+=1 
                    else: 
                        print('You have entered a invalid input') 
                else:  
                    go = False 
 
 
#removing repeating cities from templist 
for y in list(templist): 
    if y[0] in list(repeat): 
        templist.remove(y) 
 
#adding remaining elements of templist to final list 
for y in list(templist): 
    final.append(f' {y[0]} in {y[1]}, {y[2]}') 
 
#printing final output 
print('\n You have entered the following cities:')                
for x in final: 
    print(x) 

这里的输出是针对不同的文本文件(一个不包含两个单词的城市):

enter code here 
 
 You have entered the following cities: 
 Hyderabad in Telangana, India 
 Rochester in New York, United States 
 Sydney in Nova Scotia, Canada 
 Chennai in Tamil Nadu, India 
 Tokyo in Tokyo, Japan 
 Dallas in Texas, United States 

请您参考如下方法:

猜测“纽约”id 在您的 city 列表中。

我认为你可以这样搜索:

#finding out where the talked about cities are 
for count,y in enumerate(city): 
    if y in altered: 
        zcount +=1 
        templist.append([city[count], state[count], country[count]]) 
        all_cities.append(city[count])  
 

我希望这有助于为您提供基本概念。如果您需要更多帮助,请告诉我。