-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
177 lines (127 loc) · 4.45 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# coding: utf-8
# ## All functions for search engines
#
# In[117]:
# The first search engine
def query1():
a = input("Enter a Query: ")
a = f_clean(a) #we need to clean the input so we have the match between the word
l = []
for i in a:
try:
l1 = list(voca[i].keys())
l += [l1]
except Exception as e:
print("Not in any films:"+str(a))
return
e = list(reduce(set.intersection, [set(item) for item in l ])) # we find the intersection from all the lis
p = df_film[df_film['film_id'].isin(e)] #we select only the film that are in the intesection
p.reset_index(inplace=True)
return p[['title','intro','url']].head(10)
# In[118]:
query1()
# In[38]:
# Now we define a function to calculates the tf idf
def invertx_voc(voc):
new_voc = {}
for k in voc.keys():
repetition = len(voc[k])
IDF = math.log(30000/repetition)
for elem in voc[k].keys():
val = voc[k][elem]
length = list(df_film[df_film['film_id'] == int(elem)]['len_text'])[0]
tf = val/length
if k not in new_voc:
new_voc[k] = {elem : tf*IDF}
else:
new_voc[k][elem] = tf*IDF
return new_voc
# In[79]:
tfldf = invertx_voc(voca)
json.dump(tfldf, open("tfidf.txt",'w'))
# In[39]:
tfldf = json.load(open("tfidf.txt"))
# In[119]:
# Second search engine with cosine similarity
def query2():
def cosSim(row):
film_vector = []
for elem in cleaned_query:
if(elem in tfldf):
if(str(row['film_id']) in tfldf[elem]):
film_vector.append(tfldf[elem][str(row['film_id'])])
else:
film_vector.append(0)
else:
film_vector.append(0)
query_vector_idf = tfidf_query(cleaned_query)
cos_sim = cosine_similarity([film_vector], [query_vector_idf])[0][0]
return cos_sim
def tfidf_query(q):
tfidf_q = []
for elem in q:
tfidf_q.append(1)
return tfidf_q
query = input("Enter a Query: ")
tfldf = json.load(open("tfidf.txt"))
cleaned_query = f_clean(query)
query_vector = tfidf_query(cleaned_query)
df_film['Similarity'] = df_film.apply(cosSim, axis = 1)
ndf = df_film[['title', 'intro','url', 'Similarity']]
result = ndf[ndf['Similarity'] > 0.7].sort_values('Similarity', ascending = False).head(10)
return result
# In[120]:
query2()
# In[121]:
# Third search engine with the most similarity of desired runtime and the film's runtime
def query3():
q_min = input("DESIRE RUNTIME(in mins): ")
inp = input("Enter The Query: ")
inp = f_clean(inp) #we need to clean the input
l = []
for i in inp:
try:
l1 = list(voca[i].keys())
l += [l1]
except Exception as e:
print("Not in any films:"+str(inp))
return
e = list(reduce(set.intersection, [set(item) for item in l ])) # we find the intersection from all the lis
p = df_film[df_film['film_id'].isin(e)] #we select only the film that are in the intesection
p.reset_index(inplace=True)
p = p[['title','intro','url','runtime']]
sim = []
# here we calculate sort-scoring
for i in range(len(p.runtime)):
try:
resrun = int(p.runtime[i].split('minutes')[0].strip())
qrun = int(q_min.strip())
calc = abs(resrun - qrun)
sim.append(calc)
except:
# in some fields that we don't have any data for runtimes we replace it by a very large number
sim.append(int('1000000'))
p['Differrence'] = sim
p.sort_values('Differrence',ascending = True,inplace = True)
return p.head(10)
# In[129]:
query3()
# In[130]:
# The total search engines
def search_engines():
q = input("Choose which engine to search[1,2 or 3](ex: 1): \n")
if q == '1':
print('** THE SIMPLE QUERY **')
return query1()
if q == '2':
print('** THE QUERY WITH HIGHEST SIMILARITY **')
return query2()
if q == '3':
print('** THE QUERY WITH HIGHEST SIMILARITY With DESIRED RUNTIME **')
print('** The lower is the Differrence, the highest is the similarity **')
return query3()
else:
print('** PLEASE TRY AGAIN WITH CHOOSING A SEARCH ENGINE **')
# In[ ]:
search_engines()