问题
I have about 100,000 unique city names and many of them have spelling mistakes (bad scanning, bad ocr, many european names with special characters, etc...). Can I write a loop in python to check cities one by one with google maps, to see if the spelling is correct? E.g. if I send "nev york", I want to receive something like "Did you mean: New York". I've already done lots of things such as matching with a list and then calculating the levenshtein distance, etc.
回答1:
i just found out about difflib
its pretty cool stuff.
its ALMOST like a spell check
>>> import difflib
>>> x = 'smoke'
>>> y = ['choke','poke','loc','joke','mediocre', 'folk']
>>>
>>> difflib.get_close_matches(x,y)
['poke', 'joke', 'choke']
>>> x = 'nev york'
>>> y = ['New York', 'Compton', ' Phoenix']
>>> difflib.get_close_matches(x,y)
['New York']
The only other part, is to get all your cities correctly spelled into a list.. or find someone with a "correctly spelled city" word file
回答2:
Ahem...well then. here is a different approch
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def setup():
driver = webdriver.Chrome()
driver.get("http://maps.google.com")
return driver
def spelledCorrectly(driver, maybeMisspelled):
searchBox = driver.find_element_by_name('gbqfq')
searchBox.send_keys(maybeMisspelled)
ref = driver.find_element_by_id('refsec')
if ref.text == u'':
print "Spelled Correctly"
else:
print ref.text
if __name__ == "__main__":
driver = setup() #pass this object into spelledCorrectly
spelledCorrectly(driver,"schenekctity")
run setup()
once, and then run spelledCorrectly()
on whatever word.
for example......
driver = setup()
for item in giant_misspelled_list_of_cities:
spelledCorrectly(driver, item)
来源:https://stackoverflow.com/questions/16972564/python-check-spelling-of-city-names-with-google-maps-api