-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest_clean_html_mapper.py
216 lines (192 loc) · 6.46 KB
/
test_clean_html_mapper.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
214
215
216
import unittest
from data_juicer.core.data import NestedDataset as Dataset
from data_juicer.ops.mapper.clean_html_mapper import CleanHtmlMapper
from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase
class CleanHtmlMapperTest(DataJuicerTestCaseBase):
def setUp(self):
self.op = CleanHtmlMapper()
def _run_helper(self, samples):
dataset = Dataset.from_list(samples)
dataset = dataset.map(self.op.process, batch_size=2)
for data in dataset:
self.assertEqual(data['text'], data['target'])
def test_complete_html_text(self):
samples = [
{
'text':
'<header><nav><ul>'
'<tile>测试</title>'
'<li><a href="#">Home</a></li>'
'<li><a href="#">About</a></li>'
'<li><a href="#">Services</a></li>'
'<li><a href="#">Contact</a></li></ul></nav></header>'
'<main><h1>Welcome to My Website</h1>'
'<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
'<button>Learn More</button></main>'
'<footer><p>© '
'2021 My Website. All Rights Reserved.</p></footer>',
'target':
'测试\n*Home\n*About\n*Services\n*Contact'
'Welcome to My WebsiteLorem ipsum dolor sit amet, '
'consectetur adipiscing elit.'
'Learn More© 2021 My Website. All Rights Reserved.'
},
]
self._run_helper(samples)
def test_html_en_text(self):
samples = [
{
'text': '<p>This is a test</p>',
'target': 'This is a test'
},
{
'text':
'<a href=\'https://www.example.com/file.html?;name=Test\''
' rel=\'noopener noreferrer\' target=\'_blank\'>Test</a>',
'target': 'Test'
},
{
'text':
'<p>This is a test</p>'
'<div class=\"article-content\"><div><p>This is a test</p>'
'<p>This is a test</p>'
'<p><span>Test</span>:This is a test</p>'
'<p><span>Test</span>:This is a test</p></div></div>'
'<p></p><div></div>',
'target':
'This is a test'
'This is a test'
'This is a test'
'Test:This is a test'
'Test:This is a test'
},
]
self._run_helper(samples)
def test_html_zh_text(self):
samples = [
{
'text': '<p>这是个测试</p>',
'target': '这是个测试'
},
{
'text':
'<a href=\'https://www.example.com/file.html?;name=Test\''
' rel=\'noopener noreferrer\' target=\'_blank\'>测试</a>',
'target': '测试'
},
{
'text':
'<p>这是1个测试。</p>'
'<div class=\"article-content\"><div><p>这是2个测试。</p>'
'<p>这是3个测试。</p>'
'<p><span>测试</span>:这是4个测试。</p>'
'<p><span>测试</span>:这是5个测试。</p></div></div>'
'<p></p><div></div>',
'target':
'这是1个测试。'
'这是2个测试。'
'这是3个测试。'
'测试:这是4个测试。'
'测试:这是5个测试。'
},
]
self._run_helper(samples)
def test_no_html_text(self):
samples = [
{
'text': 'This is a test',
'target': 'This is a test'
},
{
'text': '这是个测试',
'target': '这是个测试'
},
{
'text': '12345678',
'target': '12345678'
},
]
self._run_helper(samples)
def test_fake_html_text(self):
samples = [
{
'text': 'This is a test</p>',
'target': 'This is a test'
},
{
'text': '<p>这是个测试',
'target': '这是个测试'
},
{
'text': '<kkkeabcd>hello</kkkeabcd>',
'target': 'hello'
},
{
'text': '<a1bcd>这是个测试</a1bcd>',
'target': '这是个测试'
},
{
'text': '<测试>这是个测试</测试>',
'target': '<测试>这是个测试'
},
{
'text':
'abc="https://www.example.com/file.html?name=Test\" 测试',
'target':
'abc="https://www.example.com/file.html?name=Test" 测试'
},
{
'text':
'href="https://www.example.com/file.html;name=Test">测试',
'target':
'href="https://www.example.com/file.html;name=Test">测试'
},
{
'text':
'<abc="https://www.example.com/file.html?name=Test">测试',
'target': '测试'
},
]
self._run_helper(samples)
def test_whitespace_text(self):
samples = [
{
'text': ' ',
'target': ''
},
{
'text': '',
'target': ''
},
{
'text': ' This is a test',
'target': 'This is a test'
},
{
'text': ' This is a test ',
'target': 'This is a test '
},
]
self._run_helper(samples)
def test_only_list_text(self):
samples = [
{
'text': '<li>Apple</li>',
'target': '*Apple'
},
{
'text': '<li>苹果</li>',
'target': '*苹果'
},
{
'text': '<ol>Apple</ol>',
'target': '*Apple'
},
{
'text': '<ol>苹果</ol>',
'target': '*苹果'
},
]
self._run_helper(samples)
if __name__ == '__main__':
unittest.main()