-
Notifications
You must be signed in to change notification settings - Fork 0
/
poilt.py
166 lines (115 loc) · 3.52 KB
/
poilt.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 2 10:10:16 2018
@author: paprasad
"""
import numpy as np
import pandas as pd
import nltk
import re
from math import *
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem import PorterStemmer
ps = PorterStemmer()
w_tokenizer = nltk.tokenize.WhitespaceTokenizer()
lemmatizer = nltk.stem.WordNetLemmatizer()
from scipy import spatial
from math import*
def square_rooted(x):
return round(sqrt(sum([a*a for a in x])),3)
def cosine_similarity(x,y):
x = x[0]
y = y[0]
numerator = sum(a*b for a,b in zip(x,y))
denominator = square_rooted(x)*square_rooted(y)
ret = round(numerator/float(denominator),3)
return ret
def cosine_similarity1(list1,list2):
result = 1 - spatial.distance.cosine(list1[0], list2[0])
return result
def lemmatize_text(text):
'''
This method applies lemmatizeation on the input text value
'''
temp=''
for w in w_tokenizer.tokenize(text):
temp = temp+' '+lemmatizer.lemmatize(w)
temp = temp.lower()
return temp
def text_stemming(text):
'''
This method applies stemming on the input text value
'''
temp=''
#print(text)
for w in w_tokenizer.tokenize(text):
temp = temp+' '+ ps.stem(w)
return temp
def create_list(text):
ret_dict = []
for word in text.split():
if word not in stop_words and word not in ret_dict:
ret_dict.append(word)
return ret_dict
def feature(sentences,feature_set):
temp = []
for sentence in sentences:
temp1= []
for feature in feature_set:
if feature in sentence:
temp1.append(1)
else:
temp1.append(0)
temp.append(temp1)
return temp
def ques_feature(ques,feature_set):
temp = []
for feature in feature_set:
if feature in ques:
temp.append(1)
else:
temp.append(0)
return temp
def clean_text(text):
regex = re.compile('(<!--(.|\\n)*-->)')
text = regex.sub('',text)
regex = re.compile('\n')
clean_text = regex.sub(' ',text)
clean_text = ''.join(x for x in clean_text if( x.isalpha() or (x ==' ')))
return clean_text
file = open('sample.txt','r',encoding="utf8")
file = file.read()
file = file.lower()
#file = lemmatize_text(file)
#file = text_stemming(file)
file = file.strip()
sentences = file.split('.')
sentences.pop()
file = clean_text(file)
for index, sentence in enumerate(sentences):
sentence = clean_text(sentence)
sentences[index] = sentence
stop_words = nltk.corpus.stopwords.words('english')
feature_set = create_list(file)
#for sentence in sentences:
feature_mat = feature(sentences,feature_set)
df_passage = pd.DataFrame(feature_mat)
ques = input("enter your question\n")
ques = lemmatize_text(ques)
ques = text_stemming(ques)
ques = ques_feature(ques,feature_set)
df_ques = pd.DataFrame([ques])
temp = []
for row in range(df_passage.shape[0]):
# res = cosine_similarity(df_ques,df_passage[row])
print(row)
X= [list(df_ques.iloc[0,:])]
#print(type(X))
# X = np.shape(X)
Y = [list(df_passage.iloc[row,:])]
#print(type(Y))
#res = np.dot(X,Y)
res = cosine_similarity(X,Y)
temp.append(res)
res = pd.DataFrame(temp)
print(res[res[0]==np.max(res[0])])