-
Notifications
You must be signed in to change notification settings - Fork 1
/
ILP_MatrixFramework.py
134 lines (101 loc) · 3.96 KB
/
ILP_MatrixFramework.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
import fio
import json
import sys
import porter
import NLTKWrapper
import os
import json
import ILP_baseline as ILP
ngramTag = "___"
#Stemming
phraseext = ".key" #a list
studentext = ".keys.source" #json
countext = ".dict" #a dictionary
lpext = ".lp"
lpsolext = ".sol"
def WriteConstraint1(PhraseBeta, L):
#$\sum_{j=1}^P y_j \beta _j \le L$
constraint = []
for phrase, beta in PhraseBeta.items():
constraint.append(" ".join([str(beta), phrase]))
print " ", " + ".join(constraint), "<=", L
def WriteConstraint2(BigramPhrase):
#$\sum_{j=1} {y_j Occ_{ij}} \ge x_i$
for bigram, phrases in BigramPhrase.items():
print " ", " + ".join(phrases), "-", bigram, ">=", 0
def WriteConstraint3(PhraseBigram):
#$y_j Occ_{ij} \le x_i$
for phrase, bigrams in PhraseBigram.items():
for bigram in bigrams:
print " ", phrase, "-", bigram, "<=", 0
def WriteConstraint4(StudentPhrase):
#$\sum_{j=1}^P {y_j Occ_{jk}} \ge z_k$
for student, phrases in StudentPhrase.items():
print " ", " + ".join(phrases), "-", student, ">=", 0
def formulate_problem(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), bigram]))
print " ", " + ".join(objective)
#write constraints
print "Subject To"
WriteConstraint1(PhraseBeta, L)
WriteConstraint2(BigramPhrase)
WriteConstraint3(PhraseBigram)
indicators = []
for bigram in BigramTheta.keys():
indicators.append(bigram)
for phrase in PhraseBeta.keys():
indicators.append(phrase)
#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 ILP1(prefix, L):
# 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]}
IndexPhrase, IndexBigram, PhraseBigram = ILP.getPhraseBigram(prefix + phraseext)
fio.SaveDict(IndexPhrase, prefix + ".phrase_index.dict")
fio.SaveDict(IndexBigram, prefix + ".bigram_index.dict")
#get weight of bigrams
BigramTheta = ILP.getBigramWeight_TF(PhraseBigram, IndexPhrase, prefix + countext) # return a dictionary
fio.SaveDict(BigramTheta, prefix + ".bigram_theta.dict")
#get word count of phrases
PhraseBeta = ILP.getWordCounts(IndexPhrase)
#get {bigram:[phrase]} dictionary
BigramPhrase = ILP.getBigramPhrase(PhraseBigram)
lpfile = prefix
formulate_problem(BigramTheta, PhraseBeta, BigramPhrase, PhraseBigram, L, lpfile)
m = ILP.SloveILP(lpfile)
output = lpfile + '.L' + str(L) + ".summary"
ILP.ExtractSummaryfromILP(lpfile, IndexPhrase, output)
def ILP_Summarizer(ilpdir, np, L):
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
ILP1(prefix, L)
if __name__ == '__main__':
ilpdir = "../../data/ILP1_Sentence/"
#ILP1(ilpdir + "test/MP.syntax", 10)
# for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for np in ['syntax', 'chunk']:
# ILP_Summarizer(ilpdir, np, L)
for L in [30]:
for np in ['sentence']:
ILP_Summarizer(ilpdir, np, L)
print "done"