From 7af4f151966de39335284ba2219f60dc3bc36024 Mon Sep 17 00:00:00 2001 From: Alex Hebing Date: Fri, 24 May 2019 14:49:30 +0200 Subject: [PATCH] Refer #11 and #18. Change Google method to place, requires language --- extract.py | 6 +++--- geocoding.py | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/extract.py b/extract.py index 0880af9..370fe82 100644 --- a/extract.py +++ b/extract.py @@ -85,8 +85,8 @@ def extract_entities(title, text, language): return r.json() -def add_geocodes(entities): - g = Geocoder(entities['text']['entities'], 'alexhebing') +def add_geocodes(args, entities): + g = Geocoder(entities['text']['entities'], args.language) try: g.geocode_locations() @@ -105,7 +105,7 @@ def collect_data(args): entities = extract_entities('test', text, args.language) print("adding geocodes to locations from '{}'".format(filename)) - add_geocodes(entities) + add_geocodes(args, entities) export(args, filename, text, entities) print("results for '{}' exported".format(filename)) diff --git a/geocoding.py b/geocoding.py index cb54759..51df73c 100644 --- a/geocoding.py +++ b/geocoding.py @@ -9,13 +9,14 @@ class Geocoder: is not present as an environment variable. Propagates geocoder's/requests' requests.exceptions.ConnectionError. ''' - def __init__(self, named_entities, geonames_username): + def __init__(self, named_entities, language): if not "GEONAMES_USERNAME" in os.environ: raise EnvironmentError("GEONAMES_USERNAME is not present as an environment variable. Please export it") if not "GOOGLE_API_KEY" in os.environ: raise EnvironmentError("GOOGLE_API_KEY is not present as an environment variable. Please export it") self.named_entities = named_entities + self.language = language self.geonames_username = os.getenv('GEONAMES_USERNAME') def geocode_locations(self): @@ -63,8 +64,8 @@ def set_geocode_from_osm(self, session, named_entity): def set_geocode_from_google(self, session, named_entity): place_name = named_entity['ne'] - g = geocoder.google(place_name, session=session) - + g = geocoder.google(place_name, session=session, method='places', language=self.language) + if (g.ok): named_entity['google_lat'] = g.lat named_entity['google_lng'] = g.lng