Spaces:
Running
Running
Update services/location_service.py
Browse files- services/location_service.py +37 -17
services/location_service.py
CHANGED
@@ -1,9 +1,29 @@
|
|
1 |
-
import locationtagger
|
2 |
from geopy.geocoders import Nominatim
|
3 |
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable
|
4 |
from models.location_models import LocationData, Coordinates, ErrorResponse
|
5 |
|
6 |
class LocationService:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
@staticmethod
|
8 |
def get_coordinates(data:dict) -> Coordinates | ErrorResponse:
|
9 |
print("Inside get coordinates")
|
@@ -11,26 +31,26 @@ class LocationService:
|
|
11 |
try:
|
12 |
location = data.get('location')
|
13 |
except:
|
14 |
-
return Coordinates(latitude=None
|
15 |
-
|
16 |
-
city=None
|
17 |
-
state=None
|
18 |
-
country=None
|
|
|
19 |
if location:
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
extracted_countries = list(place_entity.countries)
|
25 |
|
26 |
-
if
|
27 |
-
city =
|
28 |
-
|
29 |
-
|
30 |
-
if extracted_countries:
|
31 |
-
country = extracted_countries[0]
|
32 |
|
33 |
-
#
|
|
|
34 |
|
35 |
if not location_string:
|
36 |
location_string = location
|
|
|
1 |
+
# import locationtagger
|
2 |
from geopy.geocoders import Nominatim
|
3 |
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable
|
4 |
from models.location_models import LocationData, Coordinates, ErrorResponse
|
5 |
|
6 |
class LocationService:
|
7 |
+
@staticmethod
|
8 |
+
def extract_location_entities(ner_results):
|
9 |
+
city, state, country = None, None, None
|
10 |
+
|
11 |
+
for entity in ner_results:
|
12 |
+
word = entity['word'].replace("##", "").strip()
|
13 |
+
|
14 |
+
if entity['entity_group'] == 'CITY' and word.isalpha():
|
15 |
+
city = word
|
16 |
+
elif entity['entity_group'] == 'STATE' and word.isalpha():
|
17 |
+
state = word
|
18 |
+
elif entity['entity_group'] == 'COUNTRY' and word.isalpha():
|
19 |
+
country = word
|
20 |
+
|
21 |
+
if city or state or country:
|
22 |
+
return {k: v for k, v in {"city": city, "state": state, "country": country}.items() if v is not None}
|
23 |
+
else:
|
24 |
+
return None
|
25 |
+
|
26 |
+
|
27 |
@staticmethod
|
28 |
def get_coordinates(data:dict) -> Coordinates | ErrorResponse:
|
29 |
print("Inside get coordinates")
|
|
|
31 |
try:
|
32 |
location = data.get('location')
|
33 |
except:
|
34 |
+
return Coordinates(latitude=None, longitude=None)
|
35 |
+
|
36 |
+
city = None
|
37 |
+
state = None
|
38 |
+
country = None
|
39 |
+
|
40 |
if location:
|
41 |
+
# Assuming `app.nlp` is already initialized elsewhere and accessible
|
42 |
+
ner_results = app.nlp(location)
|
43 |
|
44 |
+
# Extract city, state, and country using the logic from extract_location_entities
|
45 |
+
location_entities = LocationService.extract_location_entities(ner_results)
|
|
|
46 |
|
47 |
+
if location_entities:
|
48 |
+
city = location_entities.get('city')
|
49 |
+
state = location_entities.get('state')
|
50 |
+
country = location_entities.get('country')
|
|
|
|
|
51 |
|
52 |
+
# Create the location string
|
53 |
+
location_string = ' '.join(filter(None, [city, state, country]))
|
54 |
|
55 |
if not location_string:
|
56 |
location_string = location
|