-
Notifications
You must be signed in to change notification settings - Fork 2
/
modelVC.py
119 lines (84 loc) · 2.47 KB
/
modelVC.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
import cosine
import nltk
import numpy as np
from threading import Thread
import threading
class summary:
text=""
n=0
weightGraph=[]
def buildGraph(self,sentences,i,tImpWord):
j=0
global n,weightGraph
#print threading.currentThread()
for j in range (i+1,n):
weightGraph[i][j]=cosine.cosineS(sentences[i],sentences[j])
if tImpWord[i]:
#print "Before adding " ,weightGraph[i][j]
weightGraph[i][j] += 0.1*tImpWord[i]
#print "After adding " ,weightGraph[i][j]
weightGraph[i][i]=0.0
def summarize(self,text,reductionRatio,heavywords):
global n,selectedSent
sentences=text.split('.')
#print len(text)
#text1=text
text1=text.replace('\"','')
text1=text1.replace(',',' ')
#text1=text1.replace('...','.')
#text1=text1.replace('....','.')
sentences1=text1.split('.')
n=len(sentences)-1
with open("stopWords.txt") as f:
stopWords=f.read().split()
t=["" for i in range(n)]
tImpWord=[0 for i in range(n)]
heavywords=heavywords.split()
for i in range(n):
t[i]=sentences[i].split(" ")
sentences1[i]=' '.join(word for word in t[i] if word not in stopWords)+'.'
for word in heavywords:
if word in t[i]:
tImpWord[i] += 1
#print tImpWord[i]
#print "heavy Word present, Added value to sent no: ",i
#print "Sentences:\n"
#print sentences
global weightGraph
weightGraph=[[0 for x in range(n)] for y in range(n)]
tCount=[0 for i in range(0,n)]
#Expoliting MultiThreading
for i in range (0,n):
#Sequential:
#self.buildGraph(sentences,i,tImpWord)
t=Thread(target=self.buildGraph, args=(sentences1,i,tImpWord,))
t.start()
t.join()
#print "Matrix of weigh graph:\n"
#print (np.matrix(weightGraph))
gist=self.gist(weightGraph,sentences,reductionRatio)
#print "Summary:\n" ,gist
return gist
def gist(self,weightGraph,sentences,reductionRatio):
gist=""
global n,selectedSent
sum1=[0 for i in range(n)]
selectedSent=[0 for i in range(n)]
for i in range(0,n):
for j in range(0,n):
sum1[i]=sum1[i]+weightGraph[i][j]+weightGraph[j][i]
#print "\n" ,np.matrix(sum1)
rank=sorted(range(n),key=lambda x:sum1[x])[::-1]
#print "\nRank array\n"
#print rank
i=0
#print n*reductionRatio
while i < n * reductionRatio:
selectedSent[rank[i]]=1
i+=1
for i in range(0,n):
if selectedSent[i]==1:
gist=gist+sentences[i]+"."
#print "Selected sentences array:\n"
#print np.matrix(selectedSent)
return gist