forked from PacktPublishing/Python-Deep-Learning-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfidf_bot.py
More file actions
48 lines (27 loc) · 1.39 KB
/
Copy pathtfidf_bot.py
File metadata and controls
48 lines (27 loc) · 1.39 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import operator ,os
from sklearn.feature_extraction.text import TfidfVectorizer
filepath = './tfidf_version/sample_data.csv'
def bot_engine(query= ''):
resp = ""
print os.getcwd()
csv_reader=pd.read_csv(filepath)
question_list = csv_reader[csv_reader.columns[0]].values.tolist()
answers_list = csv_reader[csv_reader.columns[1]].values.tolist()
vectorizer = TfidfVectorizer(min_df=0, ngram_range=(2, 4), strip_accents='unicode',norm='l2' , encoding='ISO-8859-1')
X_train = vectorizer.fit_transform(np.array([''.join(que) for que in question_list]))
X_query=vectorizer.transform([query])
XX_similarity=np.dot(X_train.todense(), X_query.transpose().todense())
XX_sim_scores= np.array(XX_similarity).flatten().tolist()
dict_sim= dict(enumerate(XX_sim_scores))
sorted_dict_sim = sorted(dict_sim.items(), key=operator.itemgetter(1), reverse =True)
if sorted_dict_sim[0][1]==0:
print("Sorry I have no answer, please try asking again in a nicer way :)")
resp = "Sorry I have no answer, please try asking again in a nicer way :)"
elif sorted_dict_sim[0][1]>0:
print answers_list [sorted_dict_sim[0][0]]
resp = answers_list [sorted_dict_sim[0][0]]
return resp