-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercice_1.py
207 lines (161 loc) · 5.55 KB
/
exercice_1.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
################################################
# Ex1 #
# Project by: #
# Group 13 #
# Sofia Aparicio 81105 #
# Rodrigo Lousada 81115 #
# Rogerio Cabaco #
################################################
################################################
# imports #
################################################
import re, pdb, sys, math, nltk, glob, os, codecs, string
import scipy.sparse as sp
import numpy as np
from collections import Counter
from nltk.tokenize import sent_tokenize, word_tokenize
from collections import defaultdict
from sklearn.feature_extraction.text import TfidfTransformer, TfidfVectorizer, _document_frequency
from sklearn.metrics.pairwise import cosine_similarity
################################################
# constants #
################################################
THRESHOLD = 0.1
RESID_PROB = 0.2 #(d)
MAXITERATIONS = 50
SENT_SUM = 5
################################################
# classes #
################################################
class Graph:
def __init__(self, listVertices):
#print("------- creating graph ----------")
self.Vertices = self.createAllVert(listVertices)
self.Edges = self.createAllEdges()
def createAllVert(self, listVertices):
vertList = []
for sent in listVertices:
if len(sent) > 1:
newVertex = Vertex(sent)
vertList.append(newVertex)
return vertList
def createAllEdges(self):
edgeList = []
lenList = len(self.Vertices)
for index in range(lenList):
for index2 in range(index+1, lenList):
cosSim = self.similarity((self.Vertices[index]).Sentence,(self.Vertices[index2]).Sentence)
if cosSim > THRESHOLD:
newEdge = Edge((self.Vertices[index]),(self.Vertices[index2]))
edgeList.append(newEdge)
(self.Vertices[index]).addEdge(newEdge)
(self.Vertices[index2]).addEdge(newEdge)
return edgeList
def numbEdgesForVertex(self):
for vertex in self.Vertices:
print(vertex.numberEdges())
def numbVertices(self):
return len(self.Vertices)
def get_cosine(self,vec1, vec2):
intersection = set(vec1.keys()) & set(vec2.keys())
numerator = sum([vec1[x] * vec2[x] for x in intersection])
sum1 = sum([vec1[x]**2 for x in vec1.keys()])
sum2 = sum([vec2[x]**2 for x in vec2.keys()])
denominator = math.sqrt(sum1) * math.sqrt(sum2)
if not denominator:
return 0.0
else:
return float(numerator) / denominator
def similarity(self, first_sent, sec_sent):
sent1_words = word_tokenize(first_sent)
sent2_words = word_tokenize(sec_sent)
vector1 = Counter(sent1_words)
vector2 = Counter(sent2_words)
cosineSim = self.get_cosine(vector1,vector2)
return cosineSim
def pageRank(self):
totalNumb = self.numbVertices()
damping_value = RESID_PROB / totalNumb
dontlink = (1 - RESID_PROB)
#iteration 0
for vertex in self.Vertices:
vertex.pageRank = damping_value
for iteration in range(MAXITERATIONS):
for vertex in self.Vertices:
vertex.pageRankNew = damping_value
sigma = 0
#Calculating sum sigma
for edge in vertex.Edges:
if edge.Vertex1 == vertex:
#print(edge.Vertex2.Sentence)
sigma += (edge.Vertex2).pageRank / (edge.Vertex2).numberEdges()
elif edge.Vertex2 == vertex:
#print(edge.Vertex1.Sentence)
sigma += (edge.Vertex1).pageRank / (edge.Vertex1).numberEdges()
#getting pageRankNew
vertex.pageRankNew += dontlink * sigma
#updating pageRanks
for vertex in self.Vertices:
vertex.pageRank = vertex.pageRankNew
"""scoresSent = {}
for vertex in self.Vertices:
scoresSent.update({vertex.pageRank:vertex.Sentence})
print(scoresSent)
sentSort = sorted(scoresSent, key= , reverse=True)
print(sentSort)
return sorted(sentSort)"""
def getSummary(self,sentSum):
summarylist = []
self.pageRank()
bestSent = (sorted(self.Vertices, key=lambda x: x.pageRank, reverse = True))[:sentSum]
orderedVertex = sorted(bestSent, key = lambda x : self.Vertices.index(x))
for x in orderedVertex:
#print(x.Sentence)
summarylist.append(x.Sentence)
return summarylist
class Vertex:
def __init__(self, sent):
self.Sentence = sent
self.Edges = []
self.pageRank = float
self.pageRankNew = float
def addEdge(self,edge):
(self.Edges).append(edge)
def numberEdges(self):
return len(self.Edges)
class Edge:
def __init__(self, vert1, vert2):
self.Vertex1 = vert1
self.Vertex2 = vert2
# self.Weight = 0
################################################
# functions #
################################################
def fileRead(filename):
with codecs.open(filename, "r", "latin-1") as file:
lines = (file.read())#.split('\n')#.decode('utf-8')
file.close()
return lines.lower()
def exercise_1_main(dir, file):
fpath = os.path.join(dir, file)
lines = fileRead(fpath)
doc=(lines.replace('\n', ' '))
sentences = []
fileSent = []
paragraphs = [p for p in lines.split('\n') if p]
for paragraph in paragraphs:
sentences += sent_tokenize(paragraph)
for sentence in sentences:
if sentence.strip(" ") != "(...)":
fileSent.append(sentence.strip(" "))
graph = Graph(fileSent)
return graph.getSummary(SENT_SUM)
def exercise_1_getGraph(fileSent):
graph = Graph(fileSent)
graph.pageRank()
return graph
################################################
# run #
################################################
if __name__ == '__main__':
mainS = exercise_1_main("TeMario/Textos-fonte", "ce94jl10-a.txt")