forked from kothiyayogesh/RestaurantBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.py
53 lines (38 loc) · 1.7 KB
/
location.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
from rasa.nlu.components import Component
class LocationExtractor(Component):
"""An API based location extractor component"""
name = "location"
provides = ["entities"]
requires = []
defaults = {}
language_list = ["en"]
print('initialised the class')
def __init__(self, component_config=None):
self.api_token = "c596f13460364db9915e6f3f98ffdac2"
self.base_url = "https://api.dandelion.eu/datatxt/nex/v1/"
self.min_confidence = 0.6
super(LocationExtractor, self).__init__(component_config)
def train(self, training_data, cfg, **kwargs):
"""Not needed, because it's done through API"""
pass
def convert_to_rasa(self, value):
"""Convert model output into the Rasa NLU compatible output format."""
entity = {"value": value,
"entity": "location",
"extractor": "location_extractor"}
return entity
def process(self, message, **kwargs):
"""Retrieve the text message, pass it to the classifier
and append the prediction results to the message class."""
import requests
list_cities = []
params = {"token": self.api_token, "min_confidence": self.min_confidence}
r = requests.get(self.base_url + "?text=" + message.text + "&include=types%2Cabstract%2Ccategories", params)
all_locations = r.json()
for x in all_locations["annotations"]:
list_cities.append(x["label"])
entity = self.convert_to_rasa(list_cities)
message.set("entities", [entity], add_to_output=True)
def persist(self, file_name, model_dir):
"""Pass because a pre-trained model is already persisted"""
pass