Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
59 lines (45 loc) · 1.41 KB
/
Copy pathmodel.py
File metadata and controls
59 lines (45 loc) · 1.41 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
55
56
57
58
59
'''
Creating a TfidfVectorizer and SVM model for the chatbot
'''
#importing libraries
import json
import warnings
warnings.filterwarnings('ignore')
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
import pickle
with open(r"data\profile_intents.json") as file:
intents = json.load(file)
# print(intents)
#initializing the arrays
patterns = []
responses = []
labels = []
#getting the data from json to arrays
for intent in intents["intents"]:
for pattern in intent["patterns"]:
patterns.append(pattern)
labels.append(intent["tag"])
responses.append({intent['tag']:intent['responses']})
#print(patterns,'\n\n\n',responses,'\n\n\n',lables)
# encode the Lables
label_encoder = LabelEncoder()
encoded_lables = label_encoder.fit_transform(labels)
#print(encoded_lables)
#Creating a pipeline
pipeline = Pipeline([
('tfidf',TfidfVectorizer(stop_words = 'english')),
('svm',SVC(kernel='rbf',probability = True))
])
#Train the pipeline
pipeline.fit(patterns, encoded_lables)
#Saving the pipeline
with open("pickles/chatbot_pipeline.pkl","wb") as file:
pickle.dump(pipeline,file)
with open("pickles/label_encoder.pkl","wb") as file:
pickle.dump(label_encoder,file)
with open("pickles/responses.pkl","wb") as file:
pickle.dump(responses,file)
print("Model trained and Saved")