HK Chun
commited on
Commit
•
d3a22aa
1
Parent(s):
c3fbd10
Readme.md
Browse filesThis chatbot1 can handle more complex conversations and tasks. For example, the code can respond to special commands, such as "repeat" and "generate." Additionally, the code can now generate more realistic and natural-sounding text. In this version of the code, the respond_to method takes an input text and generates a response based on it. It first checks for any special commands, such as repeat or generate, and responds accordingly. If there are no special commands, it finds the most similar sentence in the training data using the Jaccard distance metric, and generates a response.
README.md
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- chatbot
|
4 |
+
---
|
5 |
+
import nltk
|
6 |
+
import numpy as np
|
7 |
+
import random
|
8 |
+
|
9 |
+
class Chatbot:
|
10 |
+
|
11 |
+
def __init__(self, train_data):
|
12 |
+
self.train_data = train_data
|
13 |
+
self.vocabulary = set()
|
14 |
+
self.word_to_index = {}
|
15 |
+
self.index_to_word = {}
|
16 |
+
self.create_vocabulary()
|
17 |
+
self.build_model()
|
18 |
+
|
19 |
+
def create_vocabulary(self):
|
20 |
+
for sentence in self.train_data:
|
21 |
+
for word in sentence:
|
22 |
+
self.vocabulary.add(word)
|
23 |
+
self.vocabulary = sorted(self.vocabulary)
|
24 |
+
self.word_to_index = {word: i for i, word in enumerate(self.vocabulary)}
|
25 |
+
self.index_to_word = {i: word for i, word in enumerate(self.vocabulary)}
|
26 |
+
|
27 |
+
def build_model(self):
|
28 |
+
self.num_words = len(self.vocabulary)
|
29 |
+
self.W = np.random.randn(self.num_words, self.num_words)
|
30 |
+
self.b = np.random.randn(self.num_words)
|
31 |
+
|
32 |
+
def predict(self, sentence):
|
33 |
+
# Convert the sentence to a sequence of indices.
|
34 |
+
indices = []
|
35 |
+
for word in sentence:
|
36 |
+
indices.append(self.word_to_index[word])
|
37 |
+
|
38 |
+
# Calculate the probability of each possible next word.
|
39 |
+
probabilities = np.dot(indices, self.W) + self.b
|
40 |
+
|
41 |
+
# Choose the word with the highest probability.
|
42 |
+
next_word = self.index_to_word[np.argmax(probabilities)]
|
43 |
+
|
44 |
+
return next_word
|
45 |
+
|
46 |
+
def generate_text(self, start_text, max_length=100):
|
47 |
+
sentence = start_text
|
48 |
+
for _ in range(max_length):
|
49 |
+
next_word = self.predict(sentence)
|
50 |
+
sentence += " " + next_word
|
51 |
+
return sentence
|
52 |
+
|
53 |
+
def respond_to(self, input_text):
|
54 |
+
input_words = nltk.word_tokenize(input_text.lower())
|
55 |
+
# Check for special commands
|
56 |
+
if input_words[0] == "repeat":
|
57 |
+
return " ".join(input_words[1:])
|
58 |
+
elif input_words[0] == "generate":
|
59 |
+
start_text = " ".join(input_words[1:])
|
60 |
+
return self.generate_text(start_text)
|
61 |
+
else:
|
62 |
+
# Find the most similar sentence in the training data.
|
63 |
+
similarity_scores = []
|
64 |
+
for sentence in self.train_data:
|
65 |
+
similarity_score = nltk.jaccard_distance(set(sentence), set(input_words))
|
66 |
+
similarity_scores.append(similarity_score)
|
67 |
+
most_similar_index = np.argmin(similarity_scores)
|
68 |
+
most_similar_sentence = self.train_data[most_similar_index]
|
69 |
+
# Generate a response based on the most similar sentence.
|
70 |
+
response = ""
|
71 |
+
for word in most_similar_sentence:
|
72 |
+
response += self.predict([word]) + " "
|
73 |
+
return response.strip()
|
74 |
+
|
75 |
+
def main():
|
76 |
+
# Load the training data.
|
77 |
+
train_data = nltk.corpus.reuters.sents()
|
78 |
+
|
79 |
+
# Create the chatbot.
|
80 |
+
chatbot = Chatbot(train_data)
|
81 |
+
|
82 |
+
# Start a conversation.
|
83 |
+
print("Chatbot: Hi, I'm a chatbot. What can I help you with?")
|
84 |
+
while True:
|
85 |
+
user_input = input("User: ")
|
86 |
+
if user_input.lower() in ["bye", "goodbye", "exit", "quit"]:
|
87 |
+
print("Chatbot: Goodbye!")
|
88 |
+
break
|
89 |
+
response = chatbot.respond_to(user_input)
|
90 |
+
print("Chatbot:", response)
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
main()
|