Omkar008 commited on
Commit
9ea563b
·
verified ·
1 Parent(s): 0c1ad60

Update services/location_service.py

Browse files
Files changed (1) hide show
  1. 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 , longitude=None)
15
- location_string=None
16
- city=None
17
- state=None
18
- country=None
 
19
  if location:
20
- place_entity = locationtagger.find_locations(text=location)
 
21
 
22
- extracted_cities = list(place_entity.cities)
23
- extracted_regions = list(place_entity.regions)
24
- extracted_countries = list(place_entity.countries)
25
 
26
- if extracted_cities:
27
- city = extracted_cities[0]
28
- if extracted_regions:
29
- state = extracted_regions[0]
30
- if extracted_countries:
31
- country = extracted_countries[0]
32
 
33
- # location_string = ' '.join(filter(None, [city, state, country]))
 
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