-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
52 lines (40 loc) · 1.42 KB
/
Copy pathrun.py
File metadata and controls
52 lines (40 loc) · 1.42 KB
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
import json
import os
import sys
# Ensure src package is importable when running from project root
ROOT = os.path.dirname(__file__)
SRC = os.path.join(ROOT, 'src')
if SRC not in sys.path:
sys.path.insert(0, SRC)
from controllers.chat_controller import ChatController
from strategies.nltk_preprocessor import NLTKPreprocessor
from strategies.rule_faq import RuleFAQStrategy
from strategies.chatterbot_adapter import ChatterBotStrategy
from strategies.rasa_adapter import RasaStrategy
def load_json(path):
with open(path, 'r', encoding='utf-8') as f:
return json.load(f)
def main():
data_dir = os.path.join(ROOT, 'src', 'data')
faq_path = os.path.join(data_dir, 'faqs.json')
corpus_path = os.path.join(data_dir, 'chatterbot_corpus.json')
pre = NLTKPreprocessor()
faq = RuleFAQStrategy(faq_path)
corpus = load_json(corpus_path)
chatter = ChatterBotStrategy(corpus=corpus)
rasa = RasaStrategy() # expects Rasa on localhost:5005 if available
controller = ChatController(pre, faq, chatter, rasa)
samples = [
"How do I reset my password?",
"Hi",
"Tell me a joke",
"I want to book an appointment for tomorrow at 3pm",
]
for s in samples:
out = controller.route(s)
print('> INPUT:', s)
print(' -> SOURCE:', out['source'])
print(' -> RESPONSE:', out['response'])
print()
if __name__ == '__main__':
main()