-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop.py
185 lines (147 loc) · 5.58 KB
/
op.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
177
178
179
180
181
182
183
184
185
# importeer libraries
import re
import glob
import math
# vind txt bestanden in de directory
bestanden = glob.glob("bestanden/*.txt")
# zet veel gebruikte woorden in lijst
veelGebruikteWoorden = list(open("dutch.txt", "r"))
veelGebruikteWoorden = map(lambda s: s.strip(), veelGebruikteWoorden)
# maak dictionary met teller
wordcounts = {}
# loop door elk bestand en voeg woorden toe aan de wordcounts dictionary
for bestand in bestanden:
with open(bestand, 'r') as f:
text = f.read()
woorden = text.split()
leestekens = ".`(),-':;_"
woordfreq = {}
for term in woorden:
woord = term.strip(leestekens)
woord = woord.lower()
if woord in veelGebruikteWoorden:
pass
elif woord in woordfreq:
woordfreq[woord] += 1
else:
woordfreq[woord] = 1
wordcounts[bestand] = woordfreq
# lege set voor de woorden
unieke_woorden = set()
# unieke woorden toevoegen aan set
for documenten, dictionary in wordcounts.items():
for key, waarde in dictionary.items():
unieke_woorden.add(key)
# woordfrequentie matrix maken en document-titels toevoegen
frequentieMatrix = [[''] + list(wordcounts.keys())]
# waardes van unieke woorden toevoegen per document
for term in set(doc for freq in wordcounts.values() for doc in freq.keys()):
frequentieMatrix.append([term] + [wordcounts[k].get(term, 0) for k in frequentieMatrix[0][1:]])
# sorteer matrix
frequentieMatrix = sorted(frequentieMatrix, key=lambda t: t[0])
# maak een lijst voor de df
df_lijst = []
teller = 0
# tel het aantal bestanden
for bestand in bestanden:
teller += 1
# tel de df per term
for term in frequentieMatrix:
df = 0
for i in term[1:]:
if i == 0:
continue
else:
df += 1
df_lijst.append(df)
# verwijder eerste rij
df_lijst.remove(df_lijst[0])
# bereken idf per term
df_lijst = [teller / term for term in df_lijst]
idf_lijst = [math.log2(term) for term in df_lijst]
# verwijder strings voor de verdere berekening
documentenRij = frequentieMatrix[0]
termen = []
for rij in frequentieMatrix:
termen.append(rij[0])
rij.remove(rij[0])
frequentieMatrix.remove(frequentieMatrix[0])
# term weight matrix maken
twMatrix = [[i * f for i in g] for g, f in zip(frequentieMatrix, idf_lijst)]
twMatrix.insert(0, documentenRij)
twMatrix = [[termen[i]] + twMatrix[i] for i in range(len(termen))]
# bereken kwadraat en de sum-weigths
rijen = [rij[1:] for rij in twMatrix[1:]]
cijfersPerDoc = zip(*rijen)
kwadraat = [sum(cijfer ** 2 for cijfer in docs) for docs in cijfersPerDoc]
# vector-lijst en vector-matrix maken
vectorLijst = []
vectorMatrix = []
# doc-titels toevoegen
vectorMatrix.append(twMatrix[0])
# bereken vierkantswortel en voeg het resultaat toe aan de vector lijst
for getal in kwadraat:
vector = math.sqrt(getal)
vectorLijst.append(vector)
# voeg de lijst met vectoren toe aan de matrix
vectorLijst.insert(0, 0.0)
vectorMatrix.append(vectorLijst)
# functie cosinus similariteit berekenen
def zoeken(queries):
count = 0
# tel de queries
for query in queries:
count += 1
# maak matrix en voeg titels van docs toe
accumulateMatrix = []
accumulateMatrix.append(twMatrix[0])
termWaarde = []
rows = [row[0:] for row in twMatrix[0]]
columns = zip(*rows)
# kijken of de query bestaat
for row in twMatrix:
for term in row:
for query in queries:
if query in row[0]:
if term != 0.0:
termWaarde.append(term)
else:
termWaarde.append(0.0)
# voeg data toe aan matrix
accumulateMatrix.append(termWaarde)
# bereken de cosinus similariteit
rowAcc = [row[1:] for row in accumulateMatrix[1:]]
rowAcc = rowAcc[0]
rowVec = [row[1:] for row in vectorMatrix[1:]]
rowVec = rowVec[0]
vectorLengte = [(vector * math.sqrt(count)) for vector in rowVec]
cosinus = [innerProduct / vecLengte for innerProduct, vecLengte in zip(rowAcc, vectorLengte)]
cosinus.insert(0, 0.0)
# maak een matrix waarbij documenten niet gerangschikt zijn
ongerangschikteMatrix = []
# for loop om titels en data toe te voegen aan ongerangschikte matrix
for i in range(len(twMatrix[0])):
lijst = []
lijst.append(twMatrix[0][i])
lijst.append(cosinus[i])
if lijst[1] != 0.0: # verwijder artikelen zonder cosinus similariteit
ongerangschikteMatrix.append(lijst)
# rangschik de documenten
gerangschikteMatrix = sorted(ongerangschikteMatrix, key=lambda score: score[1], reverse=True)
return gerangschikteMatrix
def artikelInformatie(queries):
gerangschikteMatrix = zoeken(queries)
artikelInfo = [] # lege lijst voor artikel informatie
for artikel in gerangschikteMatrix: # voeg per artikel de informatie toe aan de lijst
with open(str(artikel[0]), 'r') as f:
text = f.read()
titel = artikel[0].strip('bestanden/').strip('.txt')
woorden = re.findall(r"\w+|\W+", text)
if queries[0] in woorden:
index = woorden.index(queries[0])
preview = '...' + text[(index-10):(index+200)] + '...'
else:
preview = text[0:200] + '...'
link = re.search('((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*', text).group()
artikelInfo.append({'titel': titel, 'preview': preview, 'link': link, 'cosinus': artikel[1]})
return artikelInfo