Skip to content

Instantly share code, notes, and snippets.

@abiramen
Last active September 17, 2021 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abiramen/20a9cde40cc560443a02a6c4481c05b4 to your computer and use it in GitHub Desktop.
Save abiramen/20a9cde40cc560443a02a6c4481c05b4 to your computer and use it in GitHub Desktop.
Dodgy streetname syllable estimation
import os
import sys
import pickle
import overpy
import syllables
from termcolor import cprint
api = overpy.Overpass()
def fetch_streets(name):
if os.path.isfile(f'cached/{name}.pickle'):
with open(f'cached/{name}.pickle', 'rb') as f:
result = pickle.load(f)
else:
try:
result = api.query(f'''
area
[name="{name}"];
way(area)[highway][name];
out;
''')
except overpy.exception.OverpassGatewayTimeout as e:
print('Seems like Overpass is overloaded and we can\'t fetch data right now. Try again later.')
sys.exit(1)
except overpy.exception.OverpassTooManyRequests as e:
print('Seems like you\'ve made too many requests to Overpass. Try again later.')
sys.exit(1)
with open(f'cached/{name}.pickle', 'wb') as f:
pickle.dump(result, f)
search_syllables = {}
for way in result.ways:
way_syllable = syllables.estimate(way.tags['name'])
if way_syllable is not None:
if way_syllable in search_syllables and way.tags['name'] not in search_syllables[way_syllable]:
search_syllables[way_syllable].append(way.tags['name'])
elif way_syllable not in search_syllables:
search_syllables[way_syllable] = [way.tags['name']]
return search_syllables
name = input('Enter a OSM name: ')
search_syllables = fetch_streets(name)
for syllable_count in sorted(search_syllables):
cprint(f'{syllable_count} syllable streets', 'green', attrs=['bold', 'underline'])
for street in search_syllables[syllable_count]:
print(street)
syllables
overpy
termcolor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment