forked from cliveverghese/proj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalb.py
189 lines (143 loc) · 4.83 KB
/
finalb.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
#!/usr/bin/python
import nltk.data
from nltk.corpus import stopwords
import re
from nltk.stem.wordnet import WordNetLemmatizer
from classes import *
import sys,getopt
def usage():
''' Print the command line usage of the program'''
print "Usage: " + sys.argv[0] + " [OPTIONS] FILE..."
print "See " + sys.argv[0] + " -h for more details"
# TODO(cliveverghese@gmail.com): Remove this function from this file and seperate it into a module.
def removeStopwords(sentence):
'''Remove Stop words and stem the sentence. It also splits the sentences into words before stemming. '''
# TODO(cliveverghese@gmail.com) : Add part of speach to each word hence produceds
ret = []
orig = []
stmr = WordNetLemmatizer()
sen = [ stmr.lemmatize(word.lower(),'v') for word in re.sub("[^\w]"," ",sentence).split() if word.lower() not in stopwords.words('english') ]
return sen
# TODO(cliveverghese@gmail.com): Add more command line options
args = sys.argv[1:]
try:
arg,opt = getopt.getopt(args,"h")
except getopt.GetoptError:
usage()
sys.exit(1)
if len(opt) == 0:
usage()
sys.exit(1)
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentence = []
doc_vec = [];
file_names ={}
j=0
total_sentences = 0
for tempfile in opt:
fp = open(tempfile)
file_names[tempfile] = j;
data = fp.read()
data = tokenizer.tokenize(data)
i = 0
tl = []
for sen in data:
#print "(" + str(i) + ")" + sen
bog = removeStopwords(sen)
tl.append(bog);
sentence.append(sentenceRepresentation(bog,0,sen,tempfile,i))
i = i + 1
fp.close()
doc_vec.append(tl)
total_sentences += i
j += 1
bag_of_words = []
for sen in sentence:
for word in sen.sentence:
if word not in bag_of_words:
bag_of_words.append( word )
i = 0
global_vector = [0 for x in range(len(bag_of_words)) ]
sentence_temp = []
for sen in sentence:
v = [ 0 for x in range(len(bag_of_words)) ]
for word in sen.sentence:
v[bag_of_words.index(word)] += 1
global_vector[bag_of_words.index(word)] += 1
sen.words = Vector(v)
i = i + 1
temp_global_vector = Vector(global_vector)
global_vector = Vector(global_vector)
for sen in sentence:
sen.weight = global_vector.cosine(sen.words)
sentence = sorted(sentence,key= lambda x: x.weight)
print "How many sentences : "
n = int(raw_input())
#for i in range(n):
# print "\rChecking sentence (" + str(i) + ")",
# summary.append(sentence[0])
# summary_vector = summary_vector + sentence[0].words
# for word in sentence[0].sentence:
# temp_global_vector[bag_of_words.index(word)] = 0;
# sentence.remove(sentence[0])
#
# for sen in sentence:
# sen.score = temp_global_vector.cosine(sen.words)
# sen.relevance = sen.score
# sentence = sorted(sentence,key = lambda x: x.relevance)
# sentence.reverse()
prev_len = len(sentence) + 1
fact = 0
while len(sentence) > n :
prev_len = len(sentence)
while sentence[0].weight < fact + 0.10:
print "Removing sentence with weight " + str(sentence[0].weight)
temp_global_vector = temp_global_vector - sentence[0].words
sentence.remove(sentence[0])
for sen in sentence:
flag = 0
for sen1 in sentence:
temp = sen1.words.cosine(sen.words)
if temp > 0.40 - fact and sentence.index(sen) != sentence.index(sen1):
flag = 1
if flag == 1:
print "Removing redundant sentence with " + str(temp)
#temp_global_vector = temp_global_vector - sen.words
sentence.remove(sen)
for sen in sentence:
sen.weight = temp_global_vector.cosine(sen.words)
fact += 0.01
print "\rSummary Of the given text"
"""i = max(global_vector.data)
printed = 0
while printed < 3:
for t in range(len(global_vector.data)):
if global_vector[t] == i:
print bag_of_words[t] + " ",
printed += 1
i -= 1
"""
print "\n"
for sen in sentence:
print sen.original + "(" + sen.original_file + "," + str(sen.file_position) +"," + str(sen.length) + "," + str(sen.weight) + ")"
#Ordering by file position
sentence = sorted(sentence,key = lambda x: x.file_position)
print "\n"
for sen in sentence:
print sen.original + "(" + sen.original_file + "," + str(sen.file_position) +"," + str(sen.length) + "," + str(sen.weight) + ")"
for i in range(0,sen.file_position) :
for word in doc_vec[file_names[sen.original_file]][i]:
v = [0 for x in range(len(bag_of_words)) ]
if word in bag_of_words:
v[bag_of_words.index(word)] += 1
print Vector(v).cosine(sen.words)
for sen in sentence:
print sen.original + "(" + sen.original_file + "," + str(sen.file_position) +"," + str(sen.length) + "," + str(sen.weight) + ")"
for i in range(sen.file_position+1,len(doc_vec[file_names[sen.original_file]])) :
for word in doc_vec[file_names[sen.original_file]][i]:
v = [0 for x in range(len(bag_of_words)) ]
if word in bag_of_words:
v[bag_of_words.index(word)] += 1
print Vector(v).cosine(sen.words)
# TODO(balan1.618@gmail.com): Add the sentence reordering
# TODO: Document all functions used within our code including the once that we created