ValadisCERTH commited on
Commit
40cca03
·
1 Parent(s): ce611b0

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +181 -0
main.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ print("\n\n ==== THE NATURAL LANGUAGE MODULE IS BEING LOADED. PLEASE WAIT ==== \n\n")
4
+ start_time_load = time.time()
5
+
6
+ from transformers import logging
7
+ logging.set_verbosity_error()
8
+
9
+ import warnings
10
+ warnings.filterwarnings("ignore", category=UserWarning)
11
+
12
+ import sys
13
+
14
+ import requests
15
+
16
+ from countriesIdentification import identify_locations
17
+ from datesIdentification import dates_binding
18
+ from magnitudeIdentification import magnitude_binding
19
+ from comparativesIdentification import comparatives_binding
20
+ from earthquaqeIdentification import identify_earthquake_event
21
+
22
+
23
+ def process_final_dict(final_dictionary):
24
+ """
25
+ Function to convert each one of the error codes from each component into a relevant code number to be handled by the SF
26
+ """
27
+
28
+ # convert all tuple error messages into dictionary error messages
29
+ for i, elem in enumerate(final_dictionary):
30
+ if isinstance(elem, tuple):
31
+
32
+ if elem == (0, "MAGNITUDE", "no_magnitude"):
33
+ final_dictionary[i] = {"Number": 9999911}
34
+
35
+ elif elem == (0, "MAGNITUDE", "more_magnitude"):
36
+ final_dictionary[i] = {"Number": 9999912}
37
+
38
+ elif elem == (0, "MAGNITUDE", "format_error"):
39
+ final_dictionary[i] = {"Number": 9999914}
40
+
41
+ elif elem == (0, "MAGNITUDE", "unknown_error"):
42
+ final_dictionary[i] = {"Number": 9999913}
43
+
44
+ elif elem == (0, "EARTHQUAKE_EVENT", "no_earthquake_reference"):
45
+ final_dictionary[i] = {"event":9999921}
46
+
47
+ elif elem == (0, "EARTHQUAKE_EVENT", "unknown_error"):
48
+ final_dictionary[i] = {"event": 9999922}
49
+
50
+ elif elem == (0,'DATES', 'wrong_date_format'):
51
+ final_dictionary[i] = {"date": {"day": 9999931, "month": 9999931, "year": 9999931}}
52
+
53
+ elif elem == (0,'DATES', 'no_date'):
54
+ final_dictionary[i] = {"date": {"day": 9999932, "month": 9999932, "year": 9999932}}
55
+
56
+ elif elem == (0,'DATES', 'more_dates'):
57
+ final_dictionary[i] = {"date": {"day": 9999933, "month": 9999933, "year": 9999933}}
58
+
59
+ elif elem == (0,'DATES', 'unknown_error'):
60
+ final_dictionary[i] = {"date": {"day": 9999934, "month": 9999934, "year": 9999934}}
61
+
62
+ elif elem == (0, "LOCATION", "no_country"):
63
+ final_dictionary[i] = {"city":[9999941], "country":[9999941]}
64
+
65
+ elif elem == (0, "LOCATION", "more_city_or_country"):
66
+ final_dictionary[i] = {"city": [9999942], "country": [9999942]}
67
+
68
+ elif elem == (0, "LOCATION", "more_country"):
69
+ final_dictionary[i] = {"city": [9999943], "country": [9999943]}
70
+
71
+ elif elem == (0, "LOCATION", "unknown_error"):
72
+ final_dictionary[i] = {"city": [9999944], "country": [9999944]}
73
+
74
+ elif elem == (0, "COMPARATIVES", "more_comparatives_mentions"):
75
+ final_dictionary[i] = {"comparative": 9999951}
76
+
77
+ elif elem == (0, "COMPARATIVES", "no_comparatives"):
78
+ final_dictionary[i] = {"comparative": 9999952}
79
+
80
+ elif elem == (0, "COMPARATIVES", "more_symbol_comparatives"):
81
+ final_dictionary[i] = {"comparative": 9999953}
82
+
83
+ elif elem == (0, "COMPARATIVES", "unknown_error"):
84
+ final_dictionary[i] = {"comparative": 9999954}
85
+
86
+ return final_dictionary
87
+
88
+
89
+
90
+ def natural_language_module(sentence):
91
+ """
92
+ Function to execute the complete natural language module pipeline
93
+ """
94
+ try:
95
+ final_dictionary = []
96
+
97
+ # identify whether the sentence is referred on earthquake events
98
+ earth_event = identify_earthquake_event(sentence)
99
+
100
+ if earth_event:
101
+ final_dictionary.append(earth_event)
102
+
103
+ # identify the target country and city in the sentence
104
+ location = identify_locations(sentence)
105
+
106
+ if location:
107
+ final_dictionary.append(location)
108
+
109
+ # identify the target comparative in the sentence
110
+ comparative = comparatives_binding(sentence)
111
+
112
+ if comparative:
113
+ final_dictionary.append(comparative)
114
+
115
+ # identify the target date in the sentence
116
+ date = dates_binding(sentence)
117
+
118
+ if isinstance(date, list):
119
+
120
+ date_dict = date[0]
121
+ date_replc = date[1]
122
+
123
+ if date_dict:
124
+ final_dictionary.append(date_dict[0])
125
+
126
+ # we also delete the date reference from the sentence so that there will
127
+ # not be any confusion with it for the magnitude identification module
128
+ if len(date_replc) == 1:
129
+ sentence = sentence.replace(date_replc[0], " ")
130
+
131
+ # in case it is a tuple we add it as it is and we do not substitute something in the sentence
132
+ elif isinstance(date, tuple):
133
+ final_dictionary.append(date)
134
+
135
+ # identify the target magnitude number in the sentence
136
+ magnitude = magnitude_binding(sentence)
137
+
138
+ if magnitude:
139
+ final_dictionary.append(magnitude)
140
+
141
+ clean_final_dictionary = process_final_dict(final_dictionary)
142
+
143
+ result = {}
144
+ for d in clean_final_dictionary:
145
+ result.update(d)
146
+
147
+ return result
148
+
149
+ except:
150
+ return "\n\n=== AN UNEXPECTED ERROR HAS OCCURED. PLEASE EXECUTE AGAIN THE SCRIPT OR COMMUNICATE WITH THE DEVELOPER TEAM === \n\n"
151
+
152
+
153
+
154
+ def process_json_sf(nl_json, sentence):
155
+ """
156
+ Function to conver the captured information an a relevant json format
157
+ """
158
+ try:
159
+ sf_json_format = {
160
+ "text": sentence,
161
+ "page": "1",
162
+ "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']),
163
+ "day": int(nl_json['date']['day']), "magnitude": nl_json['Number'], "comparative": nl_json['comparative'], "point": False, "latitude": None,"lognitude": None}
164
+ }
165
+
166
+ return sf_json_format
167
+
168
+ except:
169
+ return "\n\n=== AN UNEXPECTED ERROR HAS OCCURED. PLEASE EXECUTE AGAIN THE SCRIPT OR COMMUNICATE WITH THE DEVELOPER TEAM === \n\n"
170
+
171
+
172
+
173
+ def main(sentence):
174
+ """
175
+ Function to bind together all the info and be executed
176
+ """
177
+
178
+ nl_data = natural_language_module(sentence)
179
+ nl_json = process_json_sf(nl_data, sentence)
180
+
181
+ return nl_json