-
Notifications
You must be signed in to change notification settings - Fork 1
/
phrasebasedShallowSummary.py
216 lines (163 loc) · 7.87 KB
/
phrasebasedShallowSummary.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
208
209
210
211
212
213
214
215
216
import sys
import re
import fio
import xml.etree.ElementTree as ET
from collections import defaultdict
from Survey import *
import random
import NLTKWrapper
import SennaParser
import porter
import tfidf
import shallowSummary
import phraseClusteringKmedoid
stopwords = [line.lower().strip() for line in fio.ReadFile(stopwordfilename)]
print "stopwords:", len(stopwords)
stopwords = stopwords + ['.', '?', '-', ',', '[', ']', '-', ';', '\'', '"', '+', '&', '!', '/', '>', '<', ')', '(', '#', '=']
def getOverlap(dict1, dict2):
count = 0
for key in dict1.keys():
if key in stopwords:
continue
if key in dict2:
count = count + 1
return count
def getStemDict(words):
dict = {}
stemed = porter.getStemming(words)
for token in stemed.split():
dict[token] = 1
return dict
def getKeyNgram(student_summaryList, sennafile, save2file=None, soft = False):
np_phrase = defaultdict(float)
#read senna file
sentences = SennaParser.SennaParse(sennafile)
stemdict = {}
#get NP phrases
for s in sentences:
NPs = s.getNPrases()
for NP in NPs:
NP = NP.lower()
if soft:
#cache the stem dictionary
if NP not in stemdict:
stemdict[NP] = getStemDict(NP)
print "----------------------------------"
print "current dict:"
fio.PrintDict(np_phrase)
print "new phrase:" + NP
#update count
duplicateFlag = False
for key in np_phrase.keys():
overlap_count = getOverlap(stemdict[key], stemdict[NP])
if overlap_count >= 1:
duplicateFlag = True
if NP != key:
np_phrase[NP] = np_phrase[NP] + overlap_count
np_phrase[key] = np_phrase[key] + overlap_count
else:
np_phrase[key] = np_phrase[key] + overlap_count
if not duplicateFlag:
np_phrase[NP] = np_phrase[NP] + 1
else:
np_phrase[NP] = np_phrase[NP] + 1
if save2file != None:
fio.SaveDict(np_phrase, save2file, SortbyValueflag = True)
return np_phrase
def getKeyPhrases(student_summaryList, sennafile, save2file=None):
return getKeyNgram(student_summaryList, sennafile, save2file=save2file)
def getShallowSummary(excelfile, folder, sennadatadir, tfidfdir, np, method, K=30):
#K is the number of words per points
header = ['ID', 'Gender', 'Point of Interest', 'Muddiest Point', 'Learning Point']
summarykey = "Top Answers"
#sheets = range(0,25)
sheets = range(0,12)
for i, sheet in enumerate(sheets):
week = i + 1
orig = prData(excelfile, sheet)
for type in ['POI', 'MP', 'LP']:
print excelfile, sheet, type
student_summaryList = getStudentResponseList(orig, header, summarykey, type)
path = folder + str(week)+ '/'
fio.newPath(path)
filename = path + type + '.summary'
sennafile = sennadatadir + "senna." + str(week) + "." + type + '.output'
Summary = []
if method == 'tfidf':
dict = fio.LoadDict(tfidfdir + str(week)+ '/' + type + '.' + np + '.tfidf.dict')
if method.startswith('lexrank'):
dict = fio.LoadDict(tfidfdir + str(week)+ '/' + type + '.' + np + '.'+method+'.dict')
else:
dict = getKeyPhrases(student_summaryList, sennafile, save2file=filename + ".keys")
keys = sorted(dict, key=dict.get, reverse = True)
total_word = 0
word_count = 0
for key in keys:
skip = False
for s in Summary:
if getOverlap(getStemDict(s), getStemDict(key)) > 0: #duplicate removing
skip = True
continue
if skip: continue
word_count = len(key.split())
total_word = total_word + word_count
if len(Summary) + 1 <= K:
#if total_word <= K:
Summary.append(key)
fio.SaveList(Summary, filename)
def ShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np, method, K=30):
getShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np, method, K)
WriteTASummary(excelfile, datadir)
def computeTFIDF(excelfile, datadir, sennadatadir, np):
header = ['ID', 'Gender', 'Point of Interest', 'Muddiest Point', 'Learning Point']
summarykey = "Top Answers"
for type in ['POI', 'MP', 'LP']:
my_tfidf = tfidf.TfIdf(stopword_filename=stopwordfilename)
sheets = range(0,12)
for i, sheet in enumerate(sheets):
week = i + 1
orig = prData(excelfile, sheet)
student_summaryList = getStudentResponseList(orig, header, summarykey, type, withSource=True)
ids = [summary[1] for summary in student_summaryList]
summaries = [summary[0] for summary in student_summaryList]
sennafile = sennadatadir + "senna." + str(week) + "." + type + '.output'
NPs, sources = phraseClusteringKmedoid.getNPs(sennafile, MalformedFlilter = True, source=ids, np=np)
my_tfidf.add_input_document_withterms(NPs)
for i, sheet in enumerate(sheets):
week = i + 1
orig = prData(excelfile, sheet)
student_summaryList = getStudentResponseList(orig, header, summarykey, type, withSource=True)
ids = [summary[1] for summary in student_summaryList]
summaries = [summary[0] for summary in student_summaryList]
sennafile = sennadatadir + "senna." + str(week) + "." + type + '.output'
NPs, sources = phraseClusteringKmedoid.getNPs(sennafile, MalformedFlilter = True, source=ids, np=np)
dict = my_tfidf.get_doc_keywords_withterms(NPs)
path = datadir + str(week)+ '/'
fio.newPath(path)
fio.SaveDict(dict, path + type + '.' + np + '.tfidf.dict', SortbyValueflag = True)
def getTFIDF(excelfile, datadir, sennadatadir, np):
computeTFIDF(excelfile, datadir, sennadatadir, np)
if __name__ == '__main__':
excelfile = "../data/2011Spring.xls"
sennadatadir = "../data/senna/"
tfidfdir = "../data/np/"
datadir = "../../mead/data/C4_ShallowSummary_bigram/"
ShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np=None, method="bigram", K=4)
# fio.newPath(tfidfdir)
#
# for np in ['chunk', 'syntax']:
# getTFIDF(excelfile, tfidfdir, sennadatadir, np)
#datadir = "../../mead/data/ShallowSummary_NPhraseSoft/"
# for np in ['chunk', 'syntax']:
# datadir = "../../mead/data/ShallowSummary_NPhrase_"+np+"_TFIDF/"
# fio.deleteFolder(datadir)
# ShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np, method="tfidf", K=30)
for np in ['chunk', 'syntax']:
datadir = "../../mead/data/Phrase_"+np+"_lexrank/"
fio.deleteFolder(datadir)
ShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np, method="lexrankmax", K=4)
# for np in ['chunk', 'syntax']:
# datadir = "../../mead/data/C4_Phrase_"+np+"_lexrank/"
# fio.deleteFolder(datadir)
# ShallowSummary(excelfile, datadir, sennadatadir, tfidfdir, np, method="lexrankmax", K=4)
print "done"