-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sequitur.py
167 lines (144 loc) · 5.52 KB
/
test_sequitur.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
__author__ = 'Maximilian Bisani'
__version__ = '$LastChangedRevision: 1691 $'
__date__ = '$LastChangedDate: 2011-08-03 15:38:08 +0200 (Wed, 03 Aug 2011) $'
__copyright__ = 'Copyright (c) 2004-2005 RWTH Aachen University'
__license__ = """
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 2 (June
1991) as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, you will find it at
http://www.gnu.org/licenses/gpl.html, or write to the Free Software
Foundation, Inc., 51 Franlin Street, Fifth Floor, Boston, MA 02110,
USA.
Should a provision of no. 9 and 10 of the GNU General Public License
be invalid or become invalid, a valid provision is deemed to have been
agreed upon which comes closest to what the parties intended
commercially. In any case guarantee/warranty shall be limited to gross
negligent actions or intended actions or fraudulent concealment.
"""
import unittest
import math
from sequitur import *
class SequenceModelTestCase(unittest.TestCase):
def testEmpty(self):
sm = SequenceModel.SequenceModel()
h = sm.initial()
for t in range(10):
self.failUnlessEqual(sm.advanced(h, t), h)
self.failUnlessEqual(sm.probability(t, h), 0.0)
def testZerogram(self):
p = 0.1
data = [((), None, - math.log(p))]
sm = SequenceModel.SequenceModel()
sm.setInitAndTerm(0, 0)
sm.set(data)
h = sm.initial()
for t in range(10):
self.failUnlessEqual(sm.advanced(h, t), h)
self.failUnlessAlmostEqual(sm.probability(t, h), p)
def testUnigram(self):
probs = [ 0.2, 0.3, 0.5 ]
data = [((), t+1, - math.log(p)) for t, p in enumerate(probs) ]
sm = SequenceModel.SequenceModel()
sm.setInitAndTerm(0, 0)
sm.set(data)
h = sm.initial()
for t in range(1, 4):
self.failUnlessEqual(sm.advanced(h, t), h)
self.failUnlessAlmostEqual(sm.probability(t, h), probs[t-1])
def testBigram(self):
probs = [ 0.2, 0.3, 0.5 ]
data = [((), t+1, - math.log(p)) for t, p in enumerate(probs) ]
probs2 = [ 0.4, 0.1, 0.5 ]
data += [((2,), t+1, - math.log(p)) for t, p in enumerate(probs2) ]
sm = SequenceModel.SequenceModel()
sm.setInitAndTerm(0, 0)
sm.set(data)
h = sm.initial()
h2 = sm.advanced(h, 2)
for t in range(1, 4):
if t == 2:
self.failUnlessEqual(sm.advanced(h, t), h2)
self.failUnlessEqual(sm.advanced(h2, t), h2)
else:
self.failUnlessEqual(sm.advanced(h, t), h)
self.failUnlessEqual(sm.advanced(h2, t), h)
self.failUnlessAlmostEqual(sm.probability(t, h), probs[t-1])
self.failUnlessAlmostEqual(sm.probability(t, h2), probs2[t-1])
class EstimatorTestCase(unittest.TestCase):
def setUp(self):
self.sequitur = Sequitur()
def tearDown(self):
del self.sequitur
def obliviousModel(self, Q):
result = SequenceModel.SequenceModel()
result.setInitAndTerm(self.sequitur.term, self.sequitur.term)
result.setZerogram(Q);
return result
def testNoData(self):
sizeTemplates = [(1,1), (1,0), (0,1)]
model = self.obliviousModel(1)
sample = Sample(self.sequitur, sizeTemplates, EstimationGraphBuilder.emergeNewMultigrams, [], model)
evidence, logLik = sample.evidence(model, useMaximumApproximation=False)
evidence = evidence.asList()
self.failUnlessEqual(evidence, [])
def testMonograms(self):
sizeTemplates = [(1,1), (1,0), (0,1)]
model = self.obliviousModel(3)
sample = [ ((c,), (c,)) for c in list('abc') ]
sample = self.sequitur.compileSample(sample)
sample = Sample(self.sequitur, sizeTemplates, EstimationGraphBuilder.emergeNewMultigrams, sample, model)
evidence, logLik = sample.evidence(model, useMaximumApproximation=False)
evidence = evidence.asList()
for hist, seg, p in evidence:
l, r = self.sequitur.symbol(seg)
self.failUnless(len(l) in range(2))
self.failUnless(len(r) in range(2))
if l == ('__term__',) and r == ('__term__',):
self.failUnlessAlmostEqual(p, 3.0)
elif len(l) == 1 and len(r) == 1:
self.failUnlessAlmostEqual(p, 0.6)
else:
self.failUnlessAlmostEqual(p, 0.4)
def testAbcMonoGrams(self):
return
estm = self.makeEstimator(1.0/16.0)
estm.setLengthConstraints(0, 1, 0, 1)
estm.addSample(['a', 'b', 'c'], ['A', 'B', 'C'])
evidence = estm.estimate()
self.failUnlessEqual(len(evidence), (1+3)**2)
for hist, (l, r), p in evidence:
self.failUnless(len(l) in range(2))
self.failUnless(len(r) in range(2))
def testAbcDiGrams(self):
return
estm = self.makeEstimator(1.0/36.0)
estm.setLengthConstraints(0, 2, 0, 2)
estm.addSample(['a', 'b', 'c'], ['A', 'B', 'C'])
evidence = estm.estimate()
self.failUnlessEqual(len(evidence), (1+3+2)**2)
for hist, (l, r), p in evidence:
self.failUnless(len(l) in range(3))
self.failUnless(len(r) in range(3))
def testAbcTriGrams(self):
return
estm = self.makeEstimator(1.0/49.0)
estm.setLengthConstraints(0, 3, 0, 3)
estm.addSample(['a', 'b', 'c'], ['A', 'B', 'C'])
for i in range(5):
print '\n', i
evidence = estm.estimate()
self.failUnlessEqual(len(evidence), (1+3+2+1)**2)
evidence.sort(lambda a, b: cmp(a[-1], b[-1]))
for hist, (l, r), p in evidence:
self.failUnless(len(l) in range(4))
self.failUnless(len(r) in range(4))
print l, r, p
estm.reestimate()
if __name__ == '__main__':
unittest.main()