import time print("\n\n ==== THE NATURAL LANGUAGE MODULE IS BEING LOADED. PLEASE WAIT ==== \n\n") start_time_load = time.time() from transformers import logging logging.set_verbosity_error() import warnings warnings.filterwarnings("ignore", category=UserWarning) import sys import requests 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) print(final_dictionary) # 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 process_json_sf(nl_json, sentence): """ Function to conver 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": None,"lognitude": None} } 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 send_json_to_endpoint(json_output, username, password): """ Function to send the produced json to a target endpoint """ endpoint_url = "http://160.40.54.158:8087/SemanticFramework/api/retrieve" headers = {'Content-type': 'application/json'} auth = (username, password) print(" === THE FINAL OUTPUT THAT IS SENT TO THE SF === \n") print(json_output) response = requests.post(endpoint_url, json=json_output, headers=headers, auth=auth) if response.status_code == 200: print("\n\n === THE RESPONSE RETRIEVED BY THE SF ===") print("\n") print(response.content) print("\n\n") else: print("Error sending JSON to endpoint") print("\n\n") def main(sentence): """ Function to bind together all the info and be executed """ end_time_load = time.time() elapsed_time_load = end_time_load - start_time_load print(f"=== THE NATURAL LANGUAGE MODULE HAS BEEN LOADED. THE LOADING TIME WAS : {elapsed_time_load} seconds === \n\n") print(" === THE NATURAL LANGUAGE MODULE HAS JUST STARTED PROCESSING THE INPUT SENTENCE === \n") start_time = time.time() nl_data = natural_language_module(sentence) print("\n") print(" === FINAL OUTPUT === \n") print(nl_data) end_time = time.time() elapsed_time = end_time - start_time print("\n\n\n ==== THE NATURAL LANGUAGE MODULE HAS FINISHED ITS EXECUTION ==== \n\n\n") print(f" ==== THE TIME OF SENTENCE PROCESSING WAS: {elapsed_time} seconds ==== \n\n") nl_json = process_json_sf(nl_data, sentence) return nl_json