Skip to content

Commit

Permalink
black fix for location_extraction.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tkalir committed Sep 15, 2024
1 parent 6817221 commit 429c395
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions anyway/parsers/location_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_road_segment_by_name_and_road(road_segment_name: str, road: int) -> Road
segments = db.session.query(RoadSegments).filter(RoadSegments.road == road).all()
for segment in segments:
if road_segment_name.startswith(segment.from_name) and road_segment_name.endswith(
segment.to_name
segment.to_name
):
return segment
err_msg = f"get_road_segment_by_name_and_road:{road_segment_name},{road}: not found"
Expand Down Expand Up @@ -198,9 +198,9 @@ def get_db_matching_location(db, latitude, longitude, resolution, road_no=None):
markers_orig = markers.copy()
if resolution != "אחר":
if (
road_no is not None
and road_no > 0
and ("road1" in relevant_fields or "road2" in relevant_fields)
road_no is not None
and road_no > 0
and ("road1" in relevant_fields or "road2" in relevant_fields)
):
markers = markers.loc[(markers["road1"] == road_no) | (markers["road2"] == road_no)]
for field in relevant_fields:
Expand Down Expand Up @@ -452,30 +452,30 @@ def extract_location_text(text):
punc_after_ind = text.find(punc_to_try, forbid_ind)
if punc_before_ind != -1 or punc_after_ind != -1:
if punc_before_ind == -1:
text = text[(punc_after_ind + 1):]
text = text[(punc_after_ind + 1) :]
elif punc_after_ind == -1:
text = text[:punc_before_ind]
else:
text = text[:punc_before_ind] + " " + text[(punc_after_ind + 1):]
text = text[:punc_before_ind] + " " + text[(punc_after_ind + 1) :]
removed_punc = True
break
if (not removed_punc) and (forbid_word in hospital_words):
for hospital_name in hospital_names:
hospital_ind = text.find(hospital_name)
if (
hospital_ind == forbid_ind + len(forbid_word) + 1
or hospital_ind == forbid_ind + len(forbid_word) + 2
hospital_ind == forbid_ind + len(forbid_word) + 1
or hospital_ind == forbid_ind + len(forbid_word) + 2
):
text = (
text[:hospital_ind] + text[hospital_ind + len(hospital_name) + 1:]
text[:hospital_ind] + text[hospital_ind + len(hospital_name) + 1 :]
)
forbid_ind = text.find(forbid_word)
text = text[:forbid_ind] + text[forbid_ind + len(forbid_word) + 1:]
text = text[:forbid_ind] + text[forbid_ind + len(forbid_word) + 1 :]
found_hospital = True
if (not found_hospital) and (not removed_punc):
text = (
text[:forbid_ind]
+ text[text.find(" ", forbid_ind + len(forbid_word) + 2):]
text[:forbid_ind]
+ text[text.find(" ", forbid_ind + len(forbid_word) + 2) :]
)

except Exception as _:
Expand Down Expand Up @@ -511,7 +511,7 @@ def extract_location_text(text):
for token in near_tokens:
i = text.find(token)
if i >= 0:
text = text[:i] + token + text[i + len(token):]
text = text[:i] + token + text[i + len(token) :]
return text


Expand All @@ -532,8 +532,10 @@ def extract_geo_features(db, newsflash: NewsFlash, use_existing_coordinates_only
if not use_existing_coordinates_only:
update_coordinates_and_resolution_using_location_text(newsflash)

if newsflash.resolution in [BE_CONST.ResolutionCategories.STREET,
BE_CONST.ResolutionCategories.SUBURBAN_ROAD]:
if newsflash.resolution in [
BE_CONST.ResolutionCategories.STREET,
BE_CONST.ResolutionCategories.SUBURBAN_ROAD,
]:
location_from_db = get_db_matching_location(
db, newsflash.lat, newsflash.lon, newsflash.resolution, newsflash.road1
)
Expand All @@ -551,9 +553,11 @@ def update_location_fields(newsflash, location_from_db):


def try_find_segment_id(newsflash):
if (newsflash.road_segment_id is None
and newsflash.road_segment_name is not None
and newsflash.road1 is not None):
if (
newsflash.road_segment_id is None
and newsflash.road_segment_name is not None
and newsflash.road1 is not None
):
try:
seg = get_road_segment_by_name_and_road(newsflash.road_segment_name, newsflash.road1)
newsflash.road_segment_id = seg.segment_id
Expand Down

0 comments on commit 429c395

Please sign in to comment.