-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parse_info.py
executable file
·471 lines (431 loc) · 22.2 KB
/
test_parse_info.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
""" Johanna Götz """
import pytest
import sys
from parse_info import InfoboxLinkParser
def test__trim_string():
ilp = InfoboxLinkParser('')
assert ilp._trim_string('') == ''
assert ilp._trim_string('\r') == ''
assert ilp._trim_string('\n') == ''
assert ilp._trim_string('\t') == ''
assert ilp._trim_string('\r\ntest\t') == 'test'
def test__parse_link_1():
# Parse a single link
s = """[[Fear of the Dark (song)|Fear of the Dark]]"""
ilp = InfoboxLinkParser(s)
ilp._parse_link()
result = ilp.get_links()
assert result == [('Fear of the Dark (song)', 'Fear of the Dark')]
def test__parse_link_2():
# Parse a single link
s = """[[Iron Maiden]]"""
ilp = InfoboxLinkParser(s)
ilp._parse_link()
result = ilp.get_links()
assert result == [('Iron Maiden', 'Iron Maiden')]
def test__parse_link_3():
# Parse a single link
s = """"""
ilp = InfoboxLinkParser(s)
ilp._parse_link()
result = ilp.get_links()
assert result == []
def test_parse_1():
# Several links inside of a string
s = """
'''''Arachnophobia''''' is a 1990 American [[black comedy]] [[horror film]]
directed by [[Frank Marshall (producer)|Frank Marshall]]
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result = ilp.get_links()
assert result == [('black comedy', 'black comedy'),
('horror film', 'horror film'),
('Frank Marshall (producer)', 'Frank Marshall')]
def test_parse_2():
# The image will be ignored but the links in the description will be extracted
s = """
[[Image:Kai Hansen.jpg|180px|thumb|[[Kai Hansen]] of [[Gamma Ray (band)|Gamma Ray]],
ex-[[Helloween]] during a show in Barcelona, Spain.
Hansen is widely regarded as the "godfather of power metal".]]
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result = ilp.get_links()
assert result == [('Kai Hansen', 'Kai Hansen'),
('Gamma Ray (band)', 'Gamma Ray'),
('Helloween', 'Helloween')]
def test_parse_3():
# The image will be ignored but the links in the description will be extracted
s = """
{{Infobox book
| name = Coraline
| title_orig =
| translator =
| image = Coraline.jpg
| caption = Front cover by Dave McKean
| author = [[Neil Gaiman]]
| illustrator = [[Dave McKean]]
| cover_artist = Dave Mckean
| country = United Kingdom
| language = English
| series =
| subject =
| genre = [[Dark fantasy]]
| publisher = [[Bloomsbury]] (UK)<br />[[Harper Collins]] (US)
| release_date = 24 February 2002
| english_release_date =
| media_type = Print, [[e-book]], [[audiobook]]
| pages = 163
| dewey = 813
| isbn =0-06-113937-8
| oclc= 71822484
| congress = PZ7.G1273 Co 2002
| preceded_by =
| followed_by =
}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
assert result_links == [('Neil Gaiman', 'Neil Gaiman'),
('Dave McKean', 'Dave McKean'),
('Dark fantasy', 'Dark fantasy'),
('Bloomsbury', 'Bloomsbury'),
('Harper Collins', 'Harper Collins'),
('e-book', 'e-book'),
('audiobook', 'audiobook')]
assert result_infoboxes == [('book',
{'name': 'Coraline',
'title_orig': '',
'translator': '',
'image': 'Coraline.jpg',
'caption': 'Front cover by Dave McKean',
'author': 'Neil Gaiman',
'illustrator': 'Dave McKean',
'cover_artist': 'Dave Mckean',
'country': 'United Kingdom',
'language': 'English',
'series': '',
'subject': '',
'genre': 'Dark fantasy',
'publisher': 'Bloomsbury (UK)<br />Harper Collins (US)',
'release_date': '24 February 2002',
'english_release_date': '',
'media_type': 'Print, e-book, audiobook',
'pages': '163',
'dewey': '813',
'isbn': '0-06-113937-8',
'oclc': '71822484',
'congress': 'PZ7.G1273 Co 2002',
'preceded_by': '',
'followed_by': ''}
)]
def test_parse_4():
# The image will be ignored but the links in the description will be extracted
s = """
{{Short description|Order of flying mammals}}
{{Use dmy dates|date=November 2020}}
{{Use British English|date=November 2017}}
{{Automatic taxobox
|name = Bat
|fossil_range = {{Fossil range|52|0|[[Eocene]]–[[Holocene|Present]]}}
|image = <imagemap>
File:Wikipedia-Bats-001-v01.jpg|300px
rect 0 0 820 510 [[Common vampire bat]]
rect 0 510 820 950 [[Greater horseshoe bat]]
rect 0 950 820 1560 [[Greater short-nosed fruit bat]]
rect 1520 0 820 510 [[Egyptian fruit bat]]
rect 1520 510 820 950 [[Mexican free-tailed bat]]
rect 1520 950 820 1560 [[Greater mouse-eared bat]]
</imagemap>
|taxon = Chiroptera
|authority = [[Johann Friedrich Blumenbach|Blumenbach]], 1779
|subdivision_ranks = Suborders
|subdivision =
(traditional):
* [[Megachiroptera]]
* [[Microchiroptera]]
(present):
* [[Yinpterochiroptera]]
* [[Yangochiroptera]]
|range_map = Bat range.png
|range_map_caption = Worldwide distribution of bat species
}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
assert result_links == [('Eocene', 'Eocene'),
('Holocene', 'Present'),
('Common vampire bat', 'Common vampire bat'),
('Greater horseshoe bat', 'Greater horseshoe bat'),
('Greater short-nosed fruit bat', 'Greater short-nosed fruit bat'),
('Egyptian fruit bat', 'Egyptian fruit bat'),
('Mexican free-tailed bat', 'Mexican free-tailed bat'),
('Greater mouse-eared bat', 'Greater mouse-eared bat'),
('Johann Friedrich Blumenbach', 'Blumenbach'),
('Megachiroptera', 'Megachiroptera'),
('Microchiroptera', 'Microchiroptera'),
('Yinpterochiroptera', 'Yinpterochiroptera'),
('Yangochiroptera', 'Yangochiroptera')]
assert result_infoboxes == [('',
{'authority': 'Blumenbach, 1779',
'fossil_range': '{{Fossil range|52|0|Eocene–Present}}',
'image': '<imagemap>\n' +
' File:Wikipedia-Bats-001-v01.jpg|300px\n' +
' rect 0 0 820 510 Common vampire bat\n' +
' rect 0 510 820 950 Greater horseshoe bat\n' +
' rect 0 950 820 1560 Greater short-nosed fruit bat\n' +
' rect 1520 0 820 510 Egyptian fruit bat\n' +
' rect 1520 510 820 950 Mexican free-tailed bat\n' +
' rect 1520 950 820 1560 Greater mouse-eared bat\n' +
' </imagemap>',
'name': 'Bat',
'range_map': 'Bat range.png',
'range_map_caption': 'Worldwide distribution of bat species',
'subdivision': '(traditional):\n' +
' * Megachiroptera\n' +
' * Microchiroptera\n' +
' (present):\n' +
' * Yinpterochiroptera\n' +
' * Yangochiroptera',
'subdivision_ranks': 'Suborders',
'taxon': 'Chiroptera'}
)]
def test_parse_5():
# The image will be ignored but the links in the description will be extracted
s = """
{{Infobox galaxy
|name=Milky Way
|ra={{RA|17|45|40.0409}}
|dec={{DEC|−29|0|28.118}}
|constellation name=[[Sagittarius (constellation)|Sagittarius]]
|dist_ly={{convert|7.86|-|8.32|kpc|kly|order=flip|abbr=on}}
<ref name="boehle2016"/><ref name="Gillessen2016"/>
|image=[[File:ESO-VLT-Laser-phot-33a-07.jpg|300px]]
|mass={{val|0.8|-|1.5|e=12|u=}}<ref name="McMillan2011"/>
<ref name="McMillan2016"/>
<ref name="Kafle2012"/>
<ref name="Kafle2014"/>
|type=Sb, Sbc, or SB(rs)bc<ref name=ssr100_1_129/><ref>
{{cite web |first1=Hartmut |last1=Frommert |first2=Christine
|last2=Kronberg |date=August 26, 2005
|url=http://messier.seds.org/more/mw_type.html
|title=Classification of the Milky Way Galaxy
|access-date=May 30, 2015 |work=SEDS |url-status=live
|archive-url=https://web.archive.org/web/20150531031937/http://messier.seds.org/more/mw_type.html
|archive-date=May 31, 2015}}</ref><br />
([[barred spiral galaxy]])
|stars={{nowrap|100–400 billion}}
|size=[[Stellar disk]]: 185 ± 15 kly <ref name="nbcnews1" /><ref>
{{Cite web|url=https://www.space.com/41047-milky-way-galaxy-size-bigger-than-thought.html
|title=It Would Take 200,000 Years at Light Speed to Cross the Milky Way
|first=Elizabeth Howell 02|last=July 2018|website=Space.com}}</ref><br>
[[Dark matter halo]]: {{convert|1.9|±|0.4|Mly|kpc|abbr=on|lk=on}}
<ref name="croswell2020" /><ref name="dearson2020" />}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
assert result_links == [('Sagittarius (constellation)', 'Sagittarius'),
('File:ESO-VLT-Laser-phot-33a-07.jpg', '300px'),
('barred spiral galaxy', 'barred spiral galaxy'),
('Stellar disk', 'Stellar disk'),
('Dark matter halo', 'Dark matter halo')]
assert result_infoboxes == [('galaxy',
{'name': 'Milky Way',
'ra': '{{RA|17|45|40.0409}}',
'dec': '{{DEC|−29|0|28.118}}',
'constellation name': 'Sagittarius',
'dist_ly': '{{convert|7.86|-|8.32|kpc|kly|order=flip|abbr=on}}\n' +
' <ref name="boehle2016"/><ref ' +
'name="Gillessen2016"/>',
'image': '300px',
'mass': '{{val|0.8|-|1.5|e=12|u=}}<ref name="McMillan2011"/>\n' +
' <ref name="McMillan2016"/>\n' +
' <ref name="Kafle2012"/>\n' +
' <ref name="Kafle2014"/>',
'type': 'Sb, Sbc, or SB(rs)bc<ref name=ssr100_1_129/><ref>\n'
' {{cite web |first1=Hartmut |last1=Frommert |first2=Christine\n'
' |last2=Kronberg |date=August 26, 2005\n'
' |url=http://messier.seds.org/more/mw_type.html\n'
' |title=Classification of the Milky Way Galaxy\n'
' |access-date=May 30, 2015 |work=SEDS |url-status=live\n'
' |archive-url=https://web.archive.org/web/20150531031937/http://messier.seds.org/more/mw_type.html\n'
' |archive-date=May 31, 2015}}</ref><br />\n'
' (barred spiral galaxy)',
'stars': '{{nowrap|100–400 billion}}',
'size': 'Stellar disk: 185 ± 15 kly <ref name="nbcnews1" /><ref>\n'
' {{Cite web|url=https://www.space.com/41047-milky-way-galaxy-size-bigger-than-thought.html\n'
' |title=It Would Take 200,000 Years at Light Speed to Cross the Milky Way\n'
' |first=Elizabeth Howell 02|last=July 2018|website=Space.com}}</ref><br>\n'
' Dark matter halo: {{convert|1.9|±|0.4|Mly|kpc|abbr=on|lk=on}}\n'
' <ref name="croswell2020" /><ref name="dearson2020" />'
}
)]
def test_parse_6():
# Comments inside of infoboxes in separate lines
s = """
{{Infobox book
| name = Blade Runner 3: Replicant Night
| author = [[K. W. Jeter]]
| language = English
| country = United States
| genre = [[Science fiction]]
| publisher = [[Bantam Spectra|Spectra]]
| isbn = 0-553-09983-3
| <!-- See Wikipedia:WikiProject_Novels or Wikipedia:WikiProject_Books -->
| title_orig =
| translator =
| image = Blade Runner 3 Replicant Night KW Jeter cover.jpeg
| caption =Cover of the first edition
| cover_artist =
| series = ''[[Blade Runner (franchise)|Blade Runner]]''
| subject =
| release_date = October 1, 1996
| media_type = Print ([[Hardcover]], [[Paperback]])
| pages = 321
| dewey= 813/.54 20
| congress= PS3560.E85 B59 1996
| oclc= 34669233
| preceded_by = [[Blade Runner 2: The Edge of Human|The Edge of Human]]
| followed_by = [[Blade Runner 4: Eye and Talon|Eye and Talon]]
}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
# The links themselves will have a link text that got stripped of some basic templates
assert result_links == [('K. W. Jeter', 'K. W. Jeter'),
('Science fiction', 'Science fiction'),
('Bantam Spectra', 'Spectra'),
('Blade Runner (franchise)', 'Blade Runner'),
('Hardcover', 'Hardcover'),
('Paperback', 'Paperback'),
('Blade Runner 2: The Edge of Human', 'The Edge of Human'),
('Blade Runner 4: Eye and Talon', 'Eye and Talon')]
# Inside of infoboxes link texts will undergo some basic template stripping
assert result_infoboxes == [('book',
{'name': 'Blade Runner 3: Replicant Night',
'author': 'K. W. Jeter',
'language': 'English',
'country': 'United States',
'genre': 'Science fiction',
'publisher': 'Spectra',
'isbn': '0-553-09983-3',
'title_orig': '',
'translator': '',
'image': 'Blade Runner 3 Replicant Night KW Jeter cover.jpeg',
'caption': 'Cover of the first edition',
'cover_artist': '',
'series': '\'\'Blade Runner\'\'',
'subject': '',
'release_date': 'October 1, 1996',
'media_type': 'Print (Hardcover, Paperback)',
'pages': '321',
'dewey': '813/.54 20',
'congress': 'PS3560.E85 B59 1996',
'oclc': '34669233',
'preceded_by': 'The Edge of Human',
'followed_by': 'Eye and Talon'
}
)]
def test_parse_7():
# Templates inside of link texts in infoboxes which will be stripped
s = """
{{Infobox Bilateral relations|Danish-Fijian|Denmark|Fiji}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
# The links themselves will have a link text that got stripped of some basic templates
assert result_links == []
# Inside of infoboxes link texts will undergo some basic template stripping
assert result_infoboxes == [('bilateral relations', {})]
def test_parse_8():
# Templates inside of link texts in infoboxes which will be stripped
s = """
{{Infobox Bilateral relations|Ecuadorian–American|Ecuador|USA|map = Ecuador USA Locator 2.svg}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
# The links themselves will have a link text that got stripped of some basic templates
assert result_links == []
# Inside of infoboxes link texts will undergo some basic template stripping
assert result_infoboxes == [('bilateral relations',
{'map': 'Ecuador USA Locator 2.svg'}
)]
def test_parse_9():
# Templates inside of link texts in infoboxes which will be stripped
s = """
{{Infobox bilateral relations|Canadian–American|Canada|USA|filetype=svg|
mission1 = [[Embassy of Canada, Washington, D.C.|Canadian Embassy, Washington, D.C.]]|
mission2 = [[Embassy of the United States, Ottawa|United States Embassy, Ottawa]]|
envoytitle1 = [[List of Canadian ambassadors to the United States|Ambassador]]}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
# The links themselves will have a link text that got stripped of some basic templates
assert result_links == [('Embassy of Canada, Washington, D.C.', 'Canadian Embassy, Washington, D.C.'),
('Embassy of the United States, Ottawa', 'United States Embassy, Ottawa'),
('List of Canadian ambassadors to the United States', 'Ambassador')]
# Inside of infoboxes link texts will undergo some basic template stripping
assert result_infoboxes == [('bilateral relations',
{'filetype': 'svg',
'mission1': 'Canadian Embassy, Washington, D.C.',
'mission2': 'United States Embassy, Ottawa',
'envoytitle1': 'Ambassador'}
)]
def test_parse_10():
# Templates inside of link texts in infoboxes which will be stripped
s = """
{{Infobox testtest
|test_entry = Bla bla [[Test link|This is a test{{nbhyph}}link]]
|2nd_test_entry = Here's another test link: [[This is a wikilink|{{lang|de|Link zum Testen}}]]
}}
"""
ilp = InfoboxLinkParser(s)
ilp.parse()
result_links = ilp.get_links()
result_infoboxes = ilp.get_infoboxes()
# The links themselves will have a link text that got stripped of some basic templates
assert result_links == [('Test link', 'This is a test-link'),
('This is a wikilink', 'Link zum Testen')]
# Inside of infoboxes link texts will undergo some basic template stripping
assert result_infoboxes == [('testtest',
{'test_entry': 'Bla bla This is a test-link',
'2nd_test_entry': 'Here\'s another test link: Link zum Testen'}
)]
def test_strip_templates():
ilp = InfoboxLinkParser('')
assert ilp.strip_templates('') == ''
assert ilp.strip_templates('{{nbsp}}') == ' '
assert ilp.strip_templates('{{nbhyph}}') == '-'
assert ilp.strip_templates('{{emdash}}') == '--'
assert ilp.strip_templates('{{solar mass}}') == 'M'
assert ilp.strip_templates('50 {{solar mass}}') == '50 M'
assert ilp.strip_templates('{{lang|de|Bla}}') == 'Bla'
assert ilp.strip_templates('{{lang|de|DIN|Bla}}') == 'Bla'
assert ilp.strip_templates('{{smaller|Bla}}') == 'Bla'
assert ilp.strip_templates('Blub {{smaller|Bla}}') == 'Blub Bla'
assert ilp.strip_templates(
'''Blub {{smaller|Bla}} {{nbsp}} {{nowrap|Blub}}'''
) == 'Blub Bla Blub'
assert ilp.strip_templates('{{music|time|3|4}}') == '3/4'
assert ilp.strip_templates('{{music|scale|Bla}}') == 'Bla'
assert ilp.strip_templates('{{pi}}') == 'pi'
assert ilp.strip_templates('{{okina}}') == ''
assert ilp.strip_templates('{{okina}}{{shy}}{{won}}') == ''
assert ilp.strip_templates('{{chem|A|B|C|D}}') == 'ABCD'
if __name__ == "__main__":
pytest.main(sys.argv)