Spaces:
Runtime error
Runtime error
| from countriesIdentification import identify_locations | |
| from datesIdentification import dates_binding | |
| from magnitudeIdentification import magnitude_binding | |
| from comparativesIdentification import comparatives_binding | |
| from earthquaqeIdentification import identify_earthquake_event | |
| def process_final_dict(final_dictionary): | |
| """ | |
| Function to convert each one of the error codes from each component into a relevant code number to be handled by the SF | |
| """ | |
| # convert all tuple error messages into dictionary error messages | |
| for i, elem in enumerate(final_dictionary): | |
| if isinstance(elem, tuple): | |
| if elem == (0, "MAGNITUDE", "no_magnitude"): | |
| final_dictionary[i] = {"Number": 9999911} | |
| elif elem == (0, "MAGNITUDE", "more_magnitude"): | |
| final_dictionary[i] = {"Number": 9999912} | |
| elif elem == (0, "MAGNITUDE", "format_error"): | |
| final_dictionary[i] = {"Number": 9999914} | |
| elif elem == (0, "MAGNITUDE", "unknown_error"): | |
| final_dictionary[i] = {"Number": 9999913} | |
| elif elem == (0, "EARTHQUAKE_EVENT", "no_earthquake_reference"): | |
| final_dictionary[i] = {"event":9999921} | |
| elif elem == (0, "EARTHQUAKE_EVENT", "unknown_error"): | |
| final_dictionary[i] = {"event": 9999922} | |
| elif elem == (0,'DATES', 'wrong_date_format'): | |
| final_dictionary[i] = {"date": {"day": 9999931, "month": 9999931, "year": 9999931}} | |
| elif elem == (0,'DATES', 'no_date'): | |
| final_dictionary[i] = {"date": {"day": 9999932, "month": 9999932, "year": 9999932}} | |
| elif elem == (0,'DATES', 'more_dates'): | |
| final_dictionary[i] = {"date": {"day": 9999933, "month": 9999933, "year": 9999933}} | |
| elif elem == (0,'DATES', 'unknown_error'): | |
| final_dictionary[i] = {"date": {"day": 9999934, "month": 9999934, "year": 9999934}} | |
| elif elem == (0, "LOCATION", "no_country"): | |
| final_dictionary[i] = {"city":[9999941], "country":[9999941]} | |
| elif elem == (0, "LOCATION", "more_city_or_country"): | |
| final_dictionary[i] = {"city": [9999942], "country": [9999942]} | |
| elif elem == (0, "LOCATION", "more_country"): | |
| final_dictionary[i] = {"city": [9999943], "country": [9999943]} | |
| elif elem == (0, "LOCATION", "unknown_error"): | |
| final_dictionary[i] = {"city": [9999944], "country": [9999944]} | |
| elif elem == (0, "COMPARATIVES", "more_comparatives_mentions"): | |
| final_dictionary[i] = {"comparative": 9999951} | |
| elif elem == (0, "COMPARATIVES", "no_comparatives"): | |
| final_dictionary[i] = {"comparative": 9999952} | |
| elif elem == (0, "COMPARATIVES", "more_symbol_comparatives"): | |
| final_dictionary[i] = {"comparative": 9999953} | |
| elif elem == (0, "COMPARATIVES", "unknown_error"): | |
| final_dictionary[i] = {"comparative": 9999954} | |
| return final_dictionary | |
| def natural_language_module(sentence): | |
| """ | |
| Function to execute the complete natural language module pipeline | |
| """ | |
| try: | |
| final_dictionary = [] | |
| # identify whether the sentence is referred on earthquake events | |
| earth_event = identify_earthquake_event(sentence) | |
| if earth_event: | |
| final_dictionary.append(earth_event) | |
| # identify the target country and city in the sentence | |
| location = identify_locations(sentence) | |
| if location: | |
| final_dictionary.append(location) | |
| # identify the target comparative in the sentence | |
| comparative = comparatives_binding(sentence) | |
| if comparative: | |
| final_dictionary.append(comparative) | |
| # identify the target date in the sentence | |
| date = dates_binding(sentence) | |
| if isinstance(date, list): | |
| date_dict = date[0] | |
| date_replc = date[1] | |
| if date_dict: | |
| final_dictionary.append(date_dict[0]) | |
| # we also delete the date reference from the sentence so that there will | |
| # not be any confusion with it for the magnitude identification module | |
| if len(date_replc) == 1: | |
| sentence = sentence.replace(date_replc[0], " ") | |
| # in case it is a tuple we add it as it is and we do not substitute something in the sentence | |
| elif isinstance(date, tuple): | |
| final_dictionary.append(date) | |
| # identify the target magnitude number in the sentence | |
| magnitude = magnitude_binding(sentence) | |
| if magnitude: | |
| final_dictionary.append(magnitude) | |
| clean_final_dictionary = process_final_dict(final_dictionary) | |
| result = {} | |
| for d in clean_final_dictionary: | |
| result.update(d) | |
| return result | |
| except: | |
| return "\n\n=== AN UNEXPECTED ERROR HAS OCCURED. PLEASE EXECUTE AGAIN THE SCRIPT OR COMMUNICATE WITH THE DEVELOPER TEAM === \n\n" | |
| def replace_zero_with_null(d): | |
| """ | |
| This is a small helper function to convert the 0 references on the final json to be sent, into None, as needed by the SF | |
| """ | |
| for k, v in d.items(): | |
| if isinstance(v, dict): | |
| replace_zero_with_null(v) | |
| elif v == 0 and k != "point": | |
| d[k] = "null" | |
| return d | |
| def process_json_sf(nl_json, sentence): | |
| """ | |
| Function to convert the captured information an a relevant json format | |
| """ | |
| try: | |
| sf_json_format = { | |
| "text": sentence, | |
| "page": "1", | |
| "nlp": {"event": nl_json['event'], "city": nl_json['city'][0], "country": nl_json['country'][0], "year": int(nl_json['date']['year']), "month": int(nl_json['date']['month']), | |
| "day": int(nl_json['date']['day']), "magnitude": nl_json['Number'], "comparative": nl_json['comparative'], "point": False, "latitude": "null", "lognitude": "null"} | |
| } | |
| return sf_json_format | |
| except: | |
| return "\n\n=== AN UNEXPECTED ERROR HAS OCCURED. PLEASE EXECUTE AGAIN THE SCRIPT OR COMMUNICATE WITH THE DEVELOPER TEAM === \n\n" | |
| def main(sentence): | |
| """ | |
| Function to bind together all the info and be executed | |
| """ | |
| nl_data = natural_language_module(sentence) | |
| nl_json = process_json_sf(nl_data, sentence) | |
| nl_json_with_null = replace_zero_with_null(nl_json) | |
| return nl_json_with_null |