-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.py
38 lines (28 loc) · 1.29 KB
/
action.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
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from rasa_core.actions.action import Action
from rasa_core.events import SlotSet
import pandas as pd
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
class GetAnswer(Action):
def __init__(self):
print("ici2")
self.faq_data = pd.read_csv('./data/faq_data.csv')
def name(self):
return 'action_get_answer'
def run(self, dispatcher, tracker, domain):
query = tracker.latest_message.text
questions = self.faq_data['question'].values.tolist()
mathed_question, score = process.extractOne(query, questions, scorer=fuzz.token_set_ratio) # use process.extract(.. limits = 3) to get multiple close matches
if score > 50: # arbitrarily chosen 50 to exclude matches not relevant to the query
matched_row = self.faq_data.loc[self.faq_data['question'] == mathed_question,]
document = matched_row['link'].values[0]
page = matched_row['page'].values[0]
match = matched_row['question'].values[0]
answer = matched_row['answers'].values[0]
response = "Here's something I found, \n\n Question: {} \n Answer: {} \n".format(match, answer)
else:
response = "Sorry I couldn't find anything relevant to your query!"
dispatcher.utter_message(response)