-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
54 lines (50 loc) · 1.81 KB
/
Copy pathdemo.py
File metadata and controls
54 lines (50 loc) · 1.81 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
53
54
from baker import Chatbot
bot = Chatbot("Baker", "data.json", backend='tfidf', memory=True)
bot.add_intent(
"greeting",
["Hello", "Hi", "Hey", "Howdy", "Good morning", "Good evening"],
["Hey there!", "Hi {name}!", "Hello! How are you?"]
)
bot.add_intent(
"farewell",
["Bye", "Goodbye", "See you later", "Talk later"],
["Goodbye {name}!", "See you later!", "Take care!"]
)
print("Baker Chatbot (ML backend: TF-IDF + char n-gram)")
print("New: Intents, Templates, Smart Response Selection")
print("=" * 50)
print("Examples:")
print(" Hello / Howdy / Good morning")
print(" My name is Alice")
print(" How are you / what is your name")
print(" Bye / Goodbye")
print("Commands: 'exit' to quit, 'teach' to train, 'stats' for context")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
break
if user_input.lower() == "teach":
print("Enter: question | response")
while True:
line = input("Teach: ")
if line.lower() == "done":
break
if "|" in line:
q, r = line.split("|", 1)
bot.train(q.strip(), r.strip())
print(f" Learned: '{q.strip()}'")
continue
if user_input.lower() == "stats":
ctx = bot.get_context()
print(f" Exchanges: {ctx.get('exchange_count', 0)}")
print(f" Entities: {ctx.get('entities', {})}")
print(f" Intents: {bot.list_intents()}")
continue
details = bot.respond_detailed(user_input)
parts = [f"Bot: {details['response']}"]
parts.append(f"(confidence: {details['confidence']})")
if details.get('intents'):
parts.append(f"intents: {details['intents']}")
if details.get('entities'):
parts.append(f"entities: {details['entities']}")
print(" ".join(parts))