-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
178 lines (134 loc) · 5.03 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
177
# -*- coding: utf-8 -*-
import spacy
import gensim
import logging
import os
from nltk.stem import SnowballStemmer
import codecs
nlp = spacy.load('es')
stemmer = SnowballStemmer('spanish')
prohibited_characters = [u'\xe1', u'\xe9', u'\xed', u'\xf3', u'\xfa', u'\xf1']
punctuation = [u'\xa1', u'\u2013', u'\u2009', u'\xfc', u'\xbf', ',', '?', u'\xc2\xa1', ':', '.', ';', '!', u'\xc2\xbf',
'"', '\'', '(', ')', u'\u2026', u'\u201c', u'\u201d']
fix_prohibited = ['a', 'e', 'i', 'o', 'u', 'n']
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
def get_unicode_sentence(s):
final_string = ""
for c in s:
is_prohibited = False
for i in range(0, len(prohibited_characters)):
if c == prohibited_characters[i]:
final_string += fix_prohibited[i]
is_prohibited = True
break
if not is_prohibited:
for i in range(0, len(punctuation)):
if punctuation[i] == c:
is_prohibited = True
break
if not is_prohibited:
final_string += c
return final_string
class Document:
sentences = []
normalizedSentences = []
path = ""
numSentences = 0
scores = []
porcentaje = 0.5
def __init__(self, path, porcentaje = 0.5):
self.porcentaje = porcentaje
self.path = path
model = gensim.models.Word2Vec.load("model")
self.word_vector = model.wv
text = codecs.open(path, "r", encoding='utf-8').read()
text = text.split('.')
for line in text:
self.scores += [0.0]
self.numSentences += 1
self.sentences += [line]
temporal = Sentence(get_unicode_sentence(line.lower()))
self.normalizedSentences += [temporal]
self.numSentences -= 1
self.get_important_sentences()
self.get_summary()
def get_score(self, s1, s2):
score1 = 0.0
if len(s1.sujeto) == 0:
if len(s2.sujeto) == 0:
score1 = 1.0
elif len(s2.sujeto) > 0:
for s in s1.sujeto:
for ss in s2.sujeto:
try:
score1 += self.word_vector.similarity(s, ss)
except:
score1 += 0
score1 = score1 / float(len(s1.sujeto) * len(s1.sujeto))
score2 = 0.0
if len(s1.verbos) == 0:
if len(s2.verbos) == 0:
score2 = 1.0
elif len(s2.verbos) > 0:
for v in s1.verbos:
for vv in s2.verbos:
try:
score2 += self.word_vector.similarity(v, vv)
except:
score2 += 0
score2 = score2 / float(len(s1.verbos) * len(s2.verbos))
score3 = 0.0
if len(s1.adjetivos) == 0:
if len(s2.adjetivos) == 0:
score3 = 1.0
else:
score3 = 0
elif len(s2.adjetivos) > 0:
for a in s1.adjetivos:
for aa in s2.adjetivos:
try:
score3 += self.word_vector.similarity(a, aa)
except:
score3 += 0
score3 = score3 / float(len(s1.adjetivos) * len(s2.adjetivos))
return (score1 + score2 + score3)/3.0
def get_important_sentences(self):
for i in range(0, len(self.normalizedSentences)):
for j in range(i+1, len(self.normalizedSentences)):
temp = self.get_score(self.normalizedSentences[i], self.normalizedSentences[j])
self.scores[i] += temp
self.scores[j] += temp
def get_summary(self):
document = codecs.open("summary.txt", "w", encoding='utf-8')
for i in range(0, len(self.scores)):
if (self.scores[i] / float(self.numSentences - 1)) > self.porcentaje:
document.write(self.sentences[i] + '.')
class Sentence(object):
sujeto = []
verbos = []
adjetivos = []
def __init__(self, s):
doc = nlp(unicode(s))
self.sujeto = []
self.verbos = []
self.adjetivos = []
for token in doc:
if token.pos_ == u'PROPN' or token.pos_ == u'NOUN':
self.sujeto += [stemmer.stem(token.text)]
elif token.pos_ == u'ADJ':
self.adjetivos += [stemmer.stem(token.text)]
elif token.pos_ == u'VERB':
self.verbos += [stemmer.stem(token.text)]
class FriendlyDocument(object):
def __init__(self, dirname):
self.dirname = dirname
def __iter__(self):
for fname in os.listdir(self.dirname):
for line in open(os.path.join(self.dirname, fname)):
yield line.split()
# sentences = FriendlyDocument("normalizedDocs")
# model = gensim.models.Word2Vec(sentences, workers=2)
# model.save("model")
d = Document("test.txt", 0.3)
for i in range(0, len(d.scores)):
print d.scores[i]/float(d.numSentences -1)