For this Code, I am provided a text file that contain several cities. I am suppose to identify the cities mentioned and print their state and country.
Requirements: If a mentioned city is in two or more countries, I ask the user to mention which city they are talking about. In addition if there is a slight typo, I ask the user if they meant a certain city instead. For example if they type 'Dalls' instead of 'Dallas' I need to provide the user options such as 'Do you mean Dallas instead of Dalls'.
Problem: My code has been working pretty well, but I feel it can be significantly simplified. Please let me know what concepts I can use to further simplify my code.
File Explanations: I am using a text called 'world-cities.csv', 'TEXT.txt' and 'usa.txt'. 'world-cities.csv' is a file that contains a lot of cities in the world. 'TEXT.txt' is a file that contains the sentences that I will be analyzing for cities. 'usa.txt' contains common words in the english language. I used it to compare to 'TEXT.txt' to remove common words. I had a problem with words such as 'and' showing up as typos. So this was a bootleg method to get rid of them.
I am trying not to use imports like geotext, and instead focus more on finding the most logically efficient way to program. If I am wrong about this, let me know.
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
mentioned in the paragraph
final = [] #final array
all_cities = [] #holds all the cities mentioned, 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 = []
altered2 = []
ban = []
temp = []
#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
#finding two worded cities
for count,y in enumerate(city):
if y in altered:
if y not in all_cities:
if len(y.split()) >1:
zcount +=1
templist.append([city[count], state[count],
country[count]])
all_cities.append(city[count])
temp.append(city[count])
#removing one city words identified from two worded cities (York vs New
York)
#splitting words
split = []
for x in temp:
for y in x.split():
split.append(y)
#removing from all_cities
for x in all_cities:
for y in split:
if y in all_cities:
all_cities.remove(y)
ban.append(y)
#removing from templist
for x in ban:
for y in templist:
if x == y[0]:
templist.remove(y)
#removing form repeat
#print(repeat)
for x in list(repeat):
if x in ban:
repeat.pop(x)
#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('\n 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)