-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp.py
347 lines (282 loc) · 11.9 KB
/
nlp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# importing libraries
import io
import librosa
from time import time
import numpy as np
import IPython.display as ipd
import grpc
import riva_api.riva_nlp_pb2 as rnlp
import riva_api.riva_nlp_pb2_grpc as rnlp_srv
import requests
from bs4 import BeautifulSoup as bs
#def answer_question(question):
# '''
# This function takes in a question, embeds it and looks for paragraphs that would be semantically
# most similar. It then takes top 10 of such paragraphs and concatenates them to create a context.
#
# We then ship the context over to Riva, to the Triton inference server and print the output
# '''
# #query_embedding = model.encode(question)
# #similarities = util.pytorch_cos_sim(query_embedding, paragraph_embeddings)
##
# #context = ''
# #for idx in similarities.argsort()[0].flip(0)[:10]:
# # context += paragraphs[idx]
#
# channel = grpc.insecure_channel('localhost:50051')
# riva_nlp = rnlp_srv.RivaLanguageUnderstandingStub(channel)
# req = rnlp.NaturalQueryRequest()
# req.query = question
# req.context = context
# resp = riva_nlp.NaturalQuery(req)
#
# print(f"Query: {question}")
# print(f"Answer: {resp.results[0].answer}")
#
#answer_question("How are you doing today?")
query = {
"intent" : "",
"domain" : "",
"O" : "",
"weathertime" : "today",
"weatherplace" : "ankara",
"temperatureunit" : "",
"current_location" : "",
"wind_speed_unit" : "",
"rainfallunit" : "",
"snowunit" : "",
"alert_type" : "",
"weatherforecastdaily" : ""
}
def analyze_intent(text):
channel = grpc.insecure_channel('localhost:50051')
riva_nlp = rnlp_srv.RivaLanguageUnderstandingStub(channel)
req = rnlp.AnalyzeIntentRequest()
req.query = str(text)
req.options.domain = "weather"
# The <domain_name> is appended to "riva_intent_" to look for a model "riva_intent_<domain_name>"
# So the model "riva_intent_<domain_name>" needs to be preloaded in riva server.
# In this case the domain is weather and the model being used is "riva_intent_weather-misc".
resp = riva_nlp.AnalyzeIntent(req)
#try:
# req = rnlp.AnalyzeIntentRequest()
# req.query = str(text)
# # The <domain_name> is appended to "riva_intent_" to look for a model "riva_intent_<domain_name>"
# # So the model "riva_intent_<domain_name>" needs to be preloaded in riva server.
# # In this case the domain is weather and the model being used is "riva_intent_weather-misc".
# req.options.domain = "weather"
# resp = riva_nlp.AnalyzeIntent(req)
#except Exception as inst:
# # An exception occurred
# print("[Riva NLU] Error during NLU request")
# return {'riva_error': 'riva_error'}
string_resp = ""
string_resp = str(resp)
#print(type(string_resp))
#print(string_resp)
#line.replace("class_name: ", "")
#line.replace('""', "")
#print(line)
main = ""
#for line in string_resp.splitlines():
# if line == "intent {":
# main = "intent"
# for x in range(3):
# if "class_name:" in line:
smth = string_resp.split("}")
for x in smth:
main = ""
token = ""
class_name = ""
if x.find(' token: "?"') != -1:
continue
for line in x.splitlines():
if line == "":
continue
elif line == "slots {":
main = "slot"
elif line == "intent {":
main = "intent"
elif line == 'domain_str: "weather"':
main = "domain"
else:
if line.find("token") != -1:
line = line.replace("token: ", "")
line = line.replace('"', "")
line = line.replace(' ', "")
line = line.replace(' ', "")
if line == "?":
continue
else:
token = line
elif line.find("class_name:") != -1:
line = line.replace("class_name: ", "")
line = line.replace('"', "")
line = line.replace(' ', "")
line = line.replace(' ', "")
class_name = line
else:
continue
if main == "slot":
query[class_name] = token
elif main == "intent":
query['intent'] = class_name
elif main == "domain":
query['domain'] = class_name
#for x in query:
# print(x , " -> ", query[x], "\n")
print(query.items())
def weather_request_response1():
# enter city name
city = query["weatherplace"]
time = query["weathertime"]
# creating url and requests instance
url = "https://www.google.com/search?q="+"weather"+city+time
html = requests.get(url).content
# getting raw data
soup = bs(html, 'html.parser')
temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
# formatting data
data = str.split('\n')
time = data[0]
sky = data[1]
# getting all div tag
listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
strd = listdiv[5].text
# getting other required data
pos = strd.find('Wind')
other_data = strd[pos:]
# printing all data
print("Temperature is", temp)
print("Time: ", time)
print("Sky Description: ", sky)
print(other_data)
def weather_request_response2():
soup = bs(requests.get("https://www.google.com/search?q=weather+london").content)
soup.find("div", attrs={'id': 'wob_loc'}).text
soup.find("div", attrs={"id": "wob_dts"}).text
soup.find("span", attrs={"id": "wob_dc"}).text
def get_weather_data(url):
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
# US english
LANGUAGE = "en-US,en;q=0.5"
session = requests.Session()
session.headers['User-Agent'] = USER_AGENT
session.headers['Accept-Language'] = LANGUAGE
session.headers['Content-Language'] = LANGUAGE
html = session.get(url)
# create a new soup
soup = bs(html.text, "html.parser")
# store all results on this dictionary
result = {}
# extract region
result['region'] = soup.find("div", attrs={"id": "wob_loc"}).text
# extract temperature now
result['temp_now'] = soup.find("span", attrs={"id": "wob_tm"}).text
# get the day and hour now
result['dayhour'] = soup.find("div", attrs={"id": "wob_dts"}).text
# get the actual weather
result['weather_now'] = soup.find("span", attrs={"id": "wob_dc"}).text
# get the precipitation
result['precipitation'] = soup.find("span", attrs={"id": "wob_pp"}).text
# get the % of humidity
result['humidity'] = soup.find("span", attrs={"id": "wob_hm"}).text
# extract the wind
result['wind'] = soup.find("span", attrs={"id": "wob_ws"}).text
# get next few days' weather
next_days = []
days = soup.find("div", attrs={"id": "wob_dp"})
for day in days.findAll("div", attrs={"class": "wob_df"}):
# extract the name of the day
day_name = day.findAll("div")[0].attrs['aria-label']
# get weather status for that day
weather = day.find("img").attrs["alt"]
temp = day.findAll("span", {"class": "wob_t"})
# maximum temparature in Celsius, use temp[1].text if you want fahrenheit
max_temp = temp[0].text
# minimum temparature in Celsius, use temp[3].text if you want fahrenheit
min_temp = temp[2].text
next_days.append({"name": day_name, "weather": weather, "max_temp": max_temp, "min_temp": min_temp})
# append to result
result['next_days'] = next_days
return result
def analyze_entities():
channel = grpc.insecure_channel('localhost:50051')
riva_nlp = rnlp_srv.RivaLanguageUnderstandingStub(channel)
text = "Have you ever heard of 'Among Us' Gregory?"
req = rnlp.AnalyzeEntitiesRequest()
req.query = str(text)
resp = riva_nlp.AnalyzeEntities(req)
print(resp)
def classify_text():
channel = grpc.insecure_channel('localhost:50051')
riva_nlp = rnlp_srv.RivaLanguageUnderstandingStub(channel)
query = "Have you ever heard of 'Among Us' Gregory?"
req = rnlp.TextClassRequest()
req.text(query)
resp = riva_nlp.ClassifyText(req)
print(resp)
def return_response(user_input):
resp = "I couldn't understand what you mean."
if (user_input == ""):
return resp
analyze_intent(user_input)
URL = "https://www.google.com/search?lr=lang_en&ie=UTF-8&q=weather"
#import argparse
#parser = argparse.ArgumentParser(description="Quick Script for Extracting Weather data using Google Weather")
#parser.add_argument("region", nargs="?", help="""Region to get weather for, must be available region.
# Default is your current location determined by your IP Address""", default="")
## parse arguments
#args = parser.parse_args()
#region = args.region
URL += query["weatherplace"] + query["weatherforecastdaily"] + query["weathertime"]
# get data
data = get_weather_data(URL)
if query["intent"] == "weather.weather":
resp = "Weather for: " + data["region"] + ". " + data['weather_now'] + ". " + "Temperature now is " + data['temp_now'] + " celcius. " + "Precipitation " + data["precipitation"] + ". Humidity " + data["humidity"] + " and Wind is "+ data["wind"]
elif query["intent"] == "weather.temperature":
resp = "Temperature of " + data["region"] + "is " + data['temp_now'] + "celcius."
elif query["intent"] == "weather.Temperature_yes_no":
resp = "I'm sorry I didn't understand what you mean."
elif query["intent"] == "weather.rainfall":
resp = "Precipitation is " + data["precipitation"]
elif query["intent"] == "weather.rainfall_yes_no":
resp = "Precipitation is " + data["precipitation"]
elif query["intent"] == "weather.snow":
resp = "Sorry. I cannot look for snow right now"
elif query["intent"] == "weather.snow_yes_no":
resp = "Sorry. I cannot look for snow right now"
elif query["intent"] == "weather.humidity":
resp = "Humidity is " + data["humidity"]
elif query["intent"] == "weather.humidity_yes_no":
resp = "Humidity is " + data["humidity"]
elif query["intent"] == "weather.windspeed":
resp = "Wind is "+ data["wind"]
elif query["intent"] == "weather.sunny":
if data["weather_now"] == "Bulutlu":
resp = "No it is cloudy."
elif data["weather_now"] == "Güneşli":
resp = "Yes it is sunny."
elif query["intent"] == "weather.cloudy":
if data["weather_now"] == "Güneşli":
resp = "No it is sunny."
elif data["weather_now"] == "Bulutlu":
resp = "Yes it is cloudy."
elif query["intent"] == "weather.context":
resp = "Weather for: " + data["region"] + ". " + data['weather_now'] + ". " + "Temperature now is " + data['temp_now'] + " celcius. " + "Precipitation " + data["precipitation"] + ". Humidity " + data["humidity"]+ " and Wind is "+ data["wind"]
# print data
#print("Weather for:", data["region"])
#print("Now:", data["dayhour"])
#print(f"Temperature now: {data['temp_now']}°C")
#print("Description:", data['weather_now'])
#print("Precipitation:", data["precipitation"])
#print("Humidity:", data["humidity"])
#print("Wind:", data["wind"])
#print("Next days:")
#for dayweather in data["next_days"]:
# print("="*40, dayweather["name"], "="*40)
# print("Description:", dayweather["weather"])
# print(f"Max temperature: {dayweather['max_temp']}°C")
# print(f"Min temperature: {dayweather['min_temp']}°C")
return resp