-
Notifications
You must be signed in to change notification settings - Fork 1
/
ILP_UniBigram.py
147 lines (117 loc) · 4.88 KB
/
ILP_UniBigram.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
import fio
import json
import sys
import porter
import NLTKWrapper
import os
import numpy
import ILP_baseline as ILP
#Stemming
phraseext = ".key" #a list
studentext = ".keys.source" #json
countext = ".dict" #a dictionary
lpext = ".lp"
lpsolext = ".sol"
def formulate_problem(Lambda, StudentGamma, StudentPhrase, BigramTheta, PhraseBeta, BigramPhrase, PhraseBigram, L, lpfileprefix):
SavedStdOut = sys.stdout
sys.stdout = open(lpfileprefix + lpext, 'w')
#write objective
print "Maximize"
objective = []
for bigram, theta in BigramTheta.items():
objective.append(" ".join([str(theta*Lambda), bigram]))
for student, grama in StudentGamma.items():
objective.append(" ".join([str(grama*(1-Lambda)), student]))
print " ", " + ".join(objective)
#write constraints
print "Subject To"
ILP.WriteConstraint1(PhraseBeta, L)
ILP.WriteConstraint2(BigramPhrase)
ILP.WriteConstraint3(PhraseBigram)
ILP.WriteConstraint4(StudentPhrase)
indicators = []
for bigram in BigramTheta.keys():
indicators.append(bigram)
for phrase in PhraseBeta.keys():
indicators.append(phrase)
for student in StudentGamma.keys():
indicators.append(student)
#write Bounds
print "Bounds"
for indicator in indicators:
print " ", indicator, "<=", 1
#write Integers
print "Integers"
print " ", " ".join(indicators)
#write End
print "End"
sys.stdout = SavedStdOut
def ILP2(prefix, L, Lambda, ngram, MalformedFlilter):
# get each stemmed bigram, sequence the bigram and the phrase
# bigrams: {index:bigram}, a dictionary of bigram index, X
# phrases: {index:phrase}, is a dictionary of phrase index, Y
#PhraseBigram: {phrase, [bigram]}
phrases, bigrams, PhraseBigram = ILP.getPhraseBigram(prefix + phraseext, Ngram=ngram, MalformedFlilter=MalformedFlilter)
fio.SaveDict(phrases, prefix + ".phrase_index.dict")
fio.SaveDict(bigrams, prefix + ".bigram_index.dict")
#get weight of bigrams {bigram:weigth}
BigramTheta = ILP.getBigramWeight_TF(PhraseBigram, phrases, prefix + countext) # return a dictionary
#get word count of phrases
PhraseBeta = ILP.getWordCounts(phrases)
#get {bigram:[phrase]} dictionary
BigramPhrase = ILP.getBigramPhrase(PhraseBigram)
#get {student:phrase}
#sequence students, students = {index:student}
students, StudentPhrase = ILP.getStudentPhrase(phrases, prefix + studentext)
fio.SaveDict(students, prefix + ".student_index.dict")
#get {student:weight0}
StudentGamma = ILP.getStudentWeight_One(StudentPhrase)
lpfile = prefix
formulate_problem(Lambda, StudentGamma, StudentPhrase, BigramTheta, PhraseBeta, BigramPhrase, PhraseBigram, L, lpfile)
m = ILP.SloveILP(lpfile)
output = lpfile + '.L' + str(L) + "." + str(Lambda) + ".summary"
ILP.ExtractSummaryfromILP(lpfile, phrases, output)
def ILP_Summarizer(ilpdir, np, L, Lambda, ngram, MalformedFlilter):
sheets = range(0,12)
for i, sheet in enumerate(sheets):
week = i + 1
dir = ilpdir + str(week) + '/'
for type in ['POI', 'MP', 'LP']:
prefix = dir + type + "." + np
ILP2(prefix, L, Lambda, ngram, MalformedFlilter)
if __name__ == '__main__':
# ilpdir = "../../data/ILP2/"
#
# #for Lambda in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
# for Lambda in [0.8]:
# #for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for L in [30]:
# for np in ['syntax', ]: #'chunk'
# ILP_Summarizer(ilpdir, np, L, Lambda, ngram=[2], MalformedFlilter=False)
#
# ilpdir = "../../data/ILP_Unibigram/"
#
# #for Lambda in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
# for Lambda in [0.8]:
# #for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for L in [30]:
# for np in ['syntax', ]: #'chunk'
# ILP_Summarizer(ilpdir, np, L, Lambda, ngram=[1,2], MalformedFlilter=False)
#
#
# ilpdir = "../../data/ILP_UnibigramMalformedFilter/"
#
# #for Lambda in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
# for Lambda in [0.8]:
# #for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for L in [30]:
# for np in ['syntax', ]: #'chunk'
# ILP_Summarizer(ilpdir, np, L, Lambda, ngram=[1,2], MalformedFlilter=True)
ilpdir = "../../data/ILP2_MalformedFilter/"
#for Lambda in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]:
for Lambda in [0.8]:
#for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
for L in [30]:
for np in ['syntax', ]: #'chunk'
ILP_Summarizer(ilpdir, np, L, Lambda, ngram=[2], MalformedFlilter=True)
print "done"