forked from sequitur-g2p/sequitur-g2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_LanguageModel.py
188 lines (154 loc) · 6.31 KB
/
test_LanguageModel.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
__author__ = "Maximilian Bisani"
__version__ = "$LastChangedRevision: 1667 $"
__date__ = "$LastChangedDate: 2007-06-02 16:32:35 +0200 (Sat, 02 Jun 2007) $"
__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 Franklin 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 difflib
import itertools
import misc
import StringIO
import unittest
import sys
from LanguageModel import *
from mGramCounts import *
TestCase = unittest.TestCase
class EqualFile(StringIO.StringIO):
def __init__(self, fname):
StringIO.StringIO.__init__(self)
self.reference = gOpenIn(fname).read()
self.data = None
def close(self):
self.data = self.getvalue()
StringIO.StringIO.close(self)
def __nonzero__(self):
if self.data is None:
self.data = self.getvalue()
if self.data == self.reference:
return True
else:
self.diff()
return False
def diff(self):
if self.data is None:
self.data = self.getvalue()
diff = difflib.unified_diff(self.reference.split("\n"), self.data.split("\n"))
for line in diff:
print(line, file=sys.stderr)
class MGramCountTestCase(TestCase):
"""
regenerate reference data:
mGramCounts.py --text tests/nab-mini-corpus.txt.gz
--write tests/nab-mini-corpus.raw-counts.gz
--counts-of-counts tests/nab-mini-corpus.raw-coc
mGramCounts.py --text tests/nab-mini-corpus.txt.gz
--vocab tests/nab-5k-vocabulary.txt.gz
--write tests/nab-mini-corpus.mapped-counts.gz
"""
def testLoadVocabulary(self):
vocabulary = loadVocabulary("tests/nab-5k-vocabulary.txt.gz")
self.assertEqual(vocabulary.size(), 4990)
order = 2
def templateTestRawCounts(self, StorageClass):
text = misc.gOpenIn("tests/nab-mini-corpus.txt.gz")
sentences = itertools.imap(str.split, text)
grams = mGramsChainCount(sentences, self.order)
counts = StorageClass()
counts.addIter(grams)
f = EqualFile("tests/nab-mini-corpus.raw-counts.gz")
TextStorage.write(f, counts)
self.assertTrue(f)
def templateTestMappedCounts(self, StorageClass):
vocabulary = loadVocabulary("tests/nab-5k-vocabulary.txt.gz")
text = misc.gOpenIn("tests/nab-mini-corpus.txt.gz")
sentences = itertools.imap(str.split, text)
sentences = itertools.imap(lambda s: map(vocabulary.map, s), sentences)
grams = mGramsChainCount(sentences, self.order)
counts = StorageClass()
counts.addIter(grams)
f = EqualFile("tests/nab-mini-corpus.mapped-counts.gz")
TextStorage.write(f, counts)
self.assertTrue(f)
def testCoutsOfCounts(self):
counts = TextStorage("tests/nab-mini-corpus.raw-counts.gz")
coc = [
mGramCounts.countsOfCounts(mGramReduceToOrder(counts, order))
for order in range(self.order)
]
reference = eval(open("tests/nab-mini-corpus.raw-coc").read())
for order in range(self.order):
self.assertEqual(coc[order], reference[order])
for StorageClass in [
DictStorage,
ListStorage,
SimpleMultifileStorage,
BiHeapMultifileStorage,
]:
def testRawCounts(self, StorageClass=StorageClass):
return self.templateTestRawCounts(StorageClass)
def testMappedCounts(self, StorageClass=StorageClass):
return self.templateTestMappedCounts(StorageClass)
setattr(
MGramCountTestCase, "testRawCountsWith" + StorageClass.__name__, testRawCounts
)
setattr(
MGramCountTestCase,
"testMappedCountsWith" + StorageClass.__name__,
testMappedCounts,
)
class LanguageModelTestCase(TestCase):
"""
regenerate reference data:
LanguageModel.py --read tests/nab-mini-corpus.mapped-counts.gz
--vocab tests/nab-5k-vocabulary.txt.gz
--counts-of-counts tests/nab-mini-corpus.raw-coc
--order 1
--lm tests/nab-mini-corpus.unigram.lm.gz
LanguageModel.py --read tests/nab-mini-corpus.mapped-counts.gz
--vocabulary tests/nab-5k-vocabulary.txt.gz
--counts-of-counts tests/nab-mini-corpus.raw-coc
--order 3
--lm tests/nab-mini-corpus.trigram.lm.gz
"""
order = 3
def setUp(self):
self.vocabulary = loadVocabulary("tests/nab-5k-vocabulary.txt.gz")
self.counts = loadCounts(
"tests/nab-mini-corpus.mapped-counts.gz", self.vocabulary
)
self.coc = eval(open("tests/nab-mini-corpus.raw-coc").read())
self.builder = LanguageModelBuilder()
self.builder.setVocabulary(self.vocabulary)
def testUnigram(self):
self.builder.setHighestOrder(0)
self.builder.estimateDiscounts(self.coc)
f = EqualFile("tests/nab-mini-corpus.unigram.lm.gz")
lm = LmArpaWriter(f, 0)
self.builder.build(self.counts, lm)
self.assertTrue(f)
def testTrigram(self):
self.builder.setHighestOrder(2)
self.builder.estimateDiscounts(self.coc)
f = EqualFile("tests/nab-mini-corpus.trigram.lm.gz")
lm = LmArpaWriter(f, 2)
self.builder.build(self.counts, lm)
self.assertTrue(f)
if __name__ == "__main__":
unittest.main()