-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_extractTextFromTex.py
213 lines (173 loc) · 10.7 KB
/
test_extractTextFromTex.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
from arXivExtracter.extractTextFromTex import extractText, compileRegex, sortArgs
from xml.dom import minidom
import unittest
import os
class TestSortArgs(unittest.TestCase):
def testDefault(self):
self.assertEqual(sortArgs([]),
(False,[],["word","hword"],False,False,False,False),
'sortArgs Default not working')
def testUsedArgs(self):
self.assertEqual(sortArgs(["--replace-inline-math","inline__math","--output",
"/Volumes/Corpus/Test06","Volumes/Corpus/Test03"]),
("inline__math",[],["word","hword"],False,False,
"/Volumes/Corpus/Test06",False))
def testNoPunc(self):
self.assertEqual(sortArgs(["--no-punctuation"]),
(False,[],["word","hword"],True,False,False,False))
def testIgnoreDefault(self):
self.assertEqual(sortArgs(["--ignore-default-extraction"]),
(False,[],[],False,False,False,False))
def testReplaceInlineMath(self):
self.assertEqual(sortArgs(["--replace-inline-math","inline__math"]),
("inline__math",[],["word","hword"],False,False,False,False))
def testReplaceInlineMathIncorrectUsage(self):
self.assertEqual(sortArgs(['--replace-inline-math','--output', 'file.txt']),
("--output",[],["word","hword"],False,False,'file.txt',False))
def testReplaceClass(self):
self.assertEqual(sortArgs(["--replace-class","math", "maths"]),
(False,[["math","maths"]],["word","hword"],False,False,False,False))
def testReplaceClasses(self):
self.assertEqual(sortArgs(["--replace-class", "math", "maths",
"--replace-class", "mathgroup", "more_maths"]),
(False, [["math","maths"],["mathgroup","more_maths"]],
["word","hword"],False,False,False,False))
def testExtractClass(self):
self.assertEqual(sortArgs(["--extract-class", "math"]),
(False,[],["word","hword","math"],False,False,False,False))
def testPuncFile(self):
self.assertEqual(sortArgs(["--punctuation-file", "/Users/paolo/Desktop/puncFile.txt"]),
(False,[],["word","hword"],False,"/Users/paolo/Desktop/puncFile.txt",
False,False))
def testOutputFile(self):
self.assertEqual(sortArgs(["--output", "/Users/paolo/Desktop/output.txt"]),
(False,[],["word","hword"],False,False,
"/Users/paolo/Desktop/output.txt", False))
def testOutputFileIncorrectUsage(self):
self.assertEqual(sortArgs(["--output", "/Users/paolo/Desktop/output.tex"]),
(False,[],["word","hword"],False,False,
"/Users/paolo/Desktop/output.tex", "/Users/paolo/Desktop/output.tex"))
def testInputFile(self):
self.assertEqual(sortArgs(["/Users/paolo/Desktop/file.tex"]),
(False,[],["word","hword"],False,False,False,
"/Users/paolo/Desktop/file.tex"))
def testErrorInlineMath(self):
with self.assertRaises(IndexError):
sortArgs(["--replace-inline-math"])
def testErrorReplaceClass(self):
with self.assertRaises(IndexError):
sortArgs(["--replace-class"])
def testErrorReplaceClass2(self):
with self.assertRaises(IndexError):
sortArgs(["--replace-class",'word'])
def testErrorReplaceClass3(self):
with self.assertRaises(IndexError):
sortArgs(["--replace-class",'/Users/paolo/Desktop/input.tex'])
def testErrorReplaceClasses(self):
with self.assertRaises(IndexError):
sortArgs(["--replace-class",'word',"hello","--replace-class","T"])
def testErrorExtractClass(self):
with self.assertRaises(IndexError):
sortArgs(["--extract-class"])
def testErrorExtractClass2(self):
with self.assertRaises(IndexError):
sortArgs(["word","--extract-class"])
def testErrorPuncFile(self):
with self.assertRaises(IndexError):
sortArgs(["--punctuation-file"])
def testErrorOutputFile(self):
with self.assertRaises(IndexError):
sortArgs(["--output"])
class TestExtractText(unittest.TestCase):
def setUp(self):
os.chdir('arXivExtracter')
def tearDown(self):
os.chdir('..')
def testMinimalExample(self):
file_ = os.path.abspath(os.path.join('..','testFiles','minimalExample.tex'))
sArgs = sortArgs([])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "")
def testSampleOneDefault(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example1.tex'))
sArgs = sortArgs([])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article One This is a sample document to test expected extraction."+\
" Section One Title This is section one. Section Two This is section two.")
def testSampleOneNoPunc(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example1.tex'))
sArgs = sortArgs(["--no-punctuation"])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article One This is a sample document to test expected extraction"+\
" Section One Title This is section one Section Two This is section two")
def testSampleOneIgnoreDefault(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example1.tex'))
sArgs = sortArgs(["--ignore-default-extraction","--extract-class","word"])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "This is a sample document to test expected extraction."+\
" This is section one. This is section two.")
def testSampleOneReplaceClass(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example1.tex'))
sArgs = sortArgs(["--replace-class","hword","Header"])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Header Header Header This is a sample document to test expected extraction."+\
" Header Header Header This is section one. Header Header This is section two.")
def testSample2default(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example2.tex'))
sArgs = sortArgs([])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article Two This is a sample document to test expected extraction."+\
" Section One Title This is section one. This is an inline equation with text surrounding it."+\
" This is a normal equation This is a figure")
def testSample2ExtractClass(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example2.tex'))
sArgs = sortArgs(["--extract-class","oword"])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article Two This is a sample document to test expected extraction."+\
" Section One Title This is section one. This is an inline equation with text surrounding it."+\
" This is a normal equation This is a figure This is a figure caption")
def testSample2ReplaceInlineMath(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example2.tex'))
sArgs = sortArgs(["--replace-inline-math","inline_math"])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article Two This is a sample document to test expected extraction."+\
" Section One Title This is section one. This is an inline equation inline_math with text surrounding it."+\
" This is a normal equation This is a figure")
def testSample2PuncFile(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example2.tex'))
pFile = os.path.abspath(os.path.join('..','testFiles','samplePuncFile.txt'))
sArgs = sortArgs(["--punctuation-file",pFile])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article Two This is a sample document to test expected extraction."+\
" Section One Title This is section one. This is an inline equation with text surrounding it."+\
" This is a normal equation This is a figure:")
def testSample2PuncFileInlineMath(self):
file_ = os.path.abspath(os.path.join('..','testFiles','example2.tex'))
pFile = os.path.abspath(os.path.join('..','testFiles','samplePuncFile.txt'))
sArgs = sortArgs(["--replace-inline-math","inline_math","--punctuation-file",pFile])
repRE, extRE, puncRE, delRE = compileRegex(sArgs[0], sArgs[1], sArgs[3],
sArgs[4], sArgs[2])
text = extractText(file_, repRE, extRE, puncRE, delRE)
self.assertEqual(text, "Sample Article Two This is a sample document to test expected extraction."+\
" Section One Title This is section one. This is an inline equation inline_math with text surrounding it."+\
" This is a normal equation This is a figure:")
if __name__ == "__main__":
unittest.main()