-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.py
142 lines (123 loc) · 5.29 KB
/
prediction.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
# Load libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics
from pickle import load
import urllib.parse
from cosineSimilarity2 import get_cosine_similarity
import Classes.urlsSim as urlsim
import Classes.URL as url
import databaseHandler as dbHandler
def getPrediction(query):
db = dbHandler.databaseHandler()
#get data from data base (urls of query)
listURL=db.get_all_Query_withoutAPI(query)
listSim=[]
# extract feature from urls
ListIdentity=[]
for firtUrl in listURL:
ListIdentity.append(firtUrl.id)
for secondUrl in listURL:
if(firtUrl.id != secondUrl.id and secondUrl.id not in ListIdentity):
cosineSim=get_cosine_similarity(firtUrl.contentText,secondUrl.contentText)
length=abs(len(firtUrl.contentText)-len(secondUrl.contentText))
dist=abs(len(firtUrl.contentText.split(" "))-len(secondUrl.contentText.split(" ")))
sameDomain=(urllib.parse.urlparse(firtUrl.url).netloc==urllib.parse.urlparse(secondUrl.url).netloc)
listSim.append(urlsim.urlsSim(firtUrl.url,secondUrl.url,cosineSim,length,dist,sameDomain))
#print(len(listURL))
#print(len(listSim))
# get test set ( all url is test set in this case)
X_test=[]
for url in listSim:
X_test.append(url.getList())
# load the model
model = load(open('model.pkl', 'rb'))
# make predictions
y_pred = model.predict(X_test)
# make predict lable to urls
for i in range(len(listSim)):
listSim[i].setLabel(y_pred[i])
listOfSimilarURl =[]
listOfNotSimilarURl =[ss.url for ss in listURL]
#list of urls similar (object of urlsim)
for i in range(len(listSim)):
if listSim[i].getLabel() == 1:
listOfSimilarURl.append(listSim[i])
#group of urls
# dic=dict()
# for i in range(len(listOfSimilarURl)):
# #print(listOfSimilarURl[i].url1," ",listOfSimilarURl[i].url2)
# listOfNotSimilarURl =[ss for ss in listOfNotSimilarURl if ( ss !=listOfSimilarURl[i].url1 and ss != listOfSimilarURl[i].url2 )]
# if listOfSimilarURl[i].url1.strip() in dic or listOfSimilarURl[i].url2.strip() in dic:
# if listOfSimilarURl[i].url1.strip() in dic:
# dic[listOfSimilarURl[i].url1.strip()].append(listOfSimilarURl[i].url2.strip())
# else:
# dic[listOfSimilarURl[i].url2.strip()].append(listOfSimilarURl[i].url1.strip())
# else:
# dic[listOfSimilarURl[i].url1.strip()]=[listOfSimilarURl[i].url2.strip()]
# new_dic=dict()
# selectedKey=[]
# for key,items in dic.items():
# if key in selectedKey:
# continue
# new_dic[key]=[]
# for item in items:
# new_dic[key].append(item)
# if item in dic:
# selectedKey.append(item)
# for ss in dic[item]:
# dic[key].append(ss)
#end of group of urls
#show urls group
# for key,items in dic.items():
# print("Similal to ", key," : ",items)
# print('-----------------')
listSimilarity=[]
for i in range(len(listOfSimilarURl)):
#print(listOfSimilarURl[i].url1," ",listOfSimilarURl[i].url2)
listOfNotSimilarURl =[ss for ss in listOfNotSimilarURl if ( ss !=listOfSimilarURl[i].url1 and ss != listOfSimilarURl[i].url2 )]
if len(listSimilarity)==0:
listSimilarity.append([listOfSimilarURl[i].url1.strip(),listOfSimilarURl[i].url2.strip()])
else:
count=0
currentItem=[]
for item in listSimilarity:
count1=0
count2=0
if listOfSimilarURl[i].url1.strip() in item:
count1=count1+1
if listOfSimilarURl[i].url2.strip() in item:
count2=count2+1
if count1>0 or count2>0:
count=count+1
if count>1 :
rang=range(len(item))
for v in rang:
x=item.pop()
if x not in currentItem:
currentItem.append(x)
continue
currentItem=item
if count1>0 and count2>0:
continue
elif count1>0:
item.append(listOfSimilarURl[i].url2.strip())
elif count2>0:
item.append(listOfSimilarURl[i].url1.strip())
continue
if count==0:
listSimilarity.append([listOfSimilarURl[i].url1.strip(),listOfSimilarURl[i].url2.strip()])
for item in listSimilarity:
if len(item)==0:
listSimilarity.remove(item)
#show urls group
print('-----------------')
for i in range(len(listSimilarity)):
print("Similal group ",i+1," : ",listSimilarity[i])
print('-----------------')
# print(len(listSimilarity))
# print(len(listOfSimilarURl))
print('-----------------')
print("Other sites : ")
print(listOfNotSimilarURl)