blob: bec7f291648ff073e7496acdce7bedb0dbb56054 [file] [log] [blame]
Akronf3efc9e2026-06-03 10:46:37 +02001use strict;
2use warnings;
3use Test::More;
4use Test::Script;
5use Test::TempDir::Tiny;
6
7my $UNZIP = `sh -c 'command -v unzip'`;
8chomp $UNZIP;
9
10if ($UNZIP eq '') {
11 plan skip_all => 'No unzip executable found in PATH.';
12}
13
14my $offset_data = <<'CONLLU';
15# filename = TEST/TEST/TEST_OFF_001/base/tokens.xml
16# text_id = TEST_TEST.TEST_OFF_001
17# text = Geras rytas.
181 Geras geras ADJ bdv. Case=Nom 2 amod _ _
192 rytas rytas NOUN dkt. Case=Nom 0 root _ _
203 . . PUNCT skyr. _ 2 punct _ SpaceAfter=No
21
22# text = Kaip sekasi?
231 Kaip kaip ADV prv. _ 2 advmod _ _
242 sekasi sektis VERB vksm. _ 0 root _ SpaceAfter=No
253 ? ? PUNCT skyr. _ 2 punct _ _
26
27CONLLU
28
29my $test_tempdir = tempdir();
30my $offset_file = "$test_tempdir/test_offset.conllu";
31{
32 open(my $ofh, '>:encoding(UTF-8)', $offset_file)
33 or die "Cannot write test file: $!";
34 print $ofh $offset_data;
35 close($ofh);
36}
37
38my $zipcontent_off = '';
39script_runs(
40 [ 'script/conllu2korapxml', '-f', 'ud', $offset_file ],
41 { stdout => \$zipcontent_off },
42 "conllu2korapxml computes offsets from # text"
43);
44
45my $zipfile_off = "$test_tempdir/test_offset.zip";
46if ($zipcontent_off) {
47 open(my $zfh, '>:raw', $zipfile_off) or die "Cannot write zip: $!";
48 print $zfh $zipcontent_off;
49 close($zfh);
50
51 my $morpho_xml = `$UNZIP -p $zipfile_off 'TEST/TEST/TEST_OFF_001/ud/morpho.xml' 2>/dev/null`;
52
53 # Sentence 1: "Geras rytas." (12 chars, starts at 0)
54 # Geras: 0-5, rytas: 6-11, .: 11-12
55 like($morpho_xml,
56 qr/id="s1_n1" from="0" to="5"/,
57 "Computed offset: token 'Geras' at 0..5");
58 like($morpho_xml,
59 qr/id="s1_n2" from="6" to="11"/,
60 "Computed offset: token 'rytas' at 6..11");
61 like($morpho_xml,
62 qr/id="s1_n3" from="11" to="12"/,
63 "Computed offset: token '.' at 11..12 (SpaceAfter=No)");
64
65 # Sentence 2: "Kaip sekasi?" (12 chars, starts at 13 = 12 + 1 space)
66 # Kaip: 13-17, sekasi: 18-24, ?: 24-25
67 like($morpho_xml,
68 qr/id="s2_n1" from="13" to="17"/,
69 "Computed offset: token 'Kaip' at 13..17 (sentence 2)");
70 like($morpho_xml,
71 qr/id="s2_n2" from="18" to="24"/,
72 "Computed offset: token 'sekasi' at 18..24");
73 like($morpho_xml,
74 qr/id="s2_n3" from="24" to="25"/,
75 "Computed offset: token '?' at 24..25 (SpaceAfter=No)");
76
77 # Verify dependency XML has correct offsets (head references)
78 my $dep_xml = `$UNZIP -p $zipfile_off 'TEST/TEST/TEST_OFF_001/ud/dependency.xml' 2>/dev/null`;
79
80 # Token 1 "Geras" (0..5) -> head token 2 "rytas" (6..11)
81 like($dep_xml,
82 qr/id="s1_n1" from="0" to="5".*?<span from="6" to="11"/s,
83 "Dependency: 'Geras' head points to 'rytas' offsets");
84
85 # Token 2 "rytas" (6..11) -> head 0 = sentence span (0..12)
86 like($dep_xml,
87 qr/id="s1_n2" from="6" to="11".*?<span from="0" to="12"/s,
88 "Dependency: 'rytas' head points to sentence span");
89}
90else {
91 fail("Computed offset: token 'Geras' at 0..5");
92 fail("Computed offset: token 'rytas' at 6..11");
93 fail("Computed offset: token '.' at 11..12 (SpaceAfter=No)");
94 fail("Computed offset: token 'Kaip' at 13..17 (sentence 2)");
95 fail("Computed offset: token 'sekasi' at 18..24");
96 fail("Computed offset: token '?' at 24..25 (SpaceAfter=No)");
97 fail("Dependency: 'Geras' head points to 'rytas' offsets");
98 fail("Dependency: 'rytas' head points to sentence span");
99}
100
101# No SpaceAfter at all - every token has normal space separation
102my $no_spaceafter_data = <<'CONLLU';
103# filename = TEST/TEST/TEST_NSA_001/base/tokens.xml
104# text_id = TEST_TEST.TEST_NSA_001
105# text = One two three
1061 One one NUM num. _ 0 root _ _
1072 two two NUM num. _ 1 flat _ _
1083 three three NUM num. _ 1 flat _ _
109
110CONLLU
111
112my $nsa_file = "$test_tempdir/test_no_spaceafter.conllu";
113{
114 open(my $nfh, '>:encoding(UTF-8)', $nsa_file)
115 or die "Cannot write test file: $!";
116 print $nfh $no_spaceafter_data;
117 close($nfh);
118}
119
120my $zipcontent_nsa = '';
121script_runs(
122 [ 'script/conllu2korapxml', '-f', 'ud', $nsa_file ],
123 { stdout => \$zipcontent_nsa },
124 "conllu2korapxml handles tokens with no SpaceAfter"
125);
126
127my $zipfile_nsa = "$test_tempdir/test_no_spaceafter.zip";
128if ($zipcontent_nsa) {
129 open(my $zfh, '>:raw', $zipfile_nsa) or die "Cannot write zip: $!";
130 print $zfh $zipcontent_nsa;
131 close($zfh);
132
133 my $morpho_nsa = `$UNZIP -p $zipfile_nsa 'TEST/TEST/TEST_NSA_001/ud/morpho.xml' 2>/dev/null`;
134
135 # "One two three" = 13 chars
136 # One: 0-3, two: 4-7, three: 8-13
137 like($morpho_nsa,
138 qr/id="s1_n1" from="0" to="3"/,
139 "No SpaceAfter: token 'One' at 0..3");
140 like($morpho_nsa,
141 qr/id="s1_n2" from="4" to="7"/,
142 "No SpaceAfter: token 'two' at 4..7");
143 like($morpho_nsa,
144 qr/id="s1_n3" from="8" to="13"/,
145 "No SpaceAfter: token 'three' at 8..13");
146}
147else {
148 fail("No SpaceAfter: token 'One' at 0..3");
149 fail("No SpaceAfter: token 'two' at 4..7");
150 fail("No SpaceAfter: token 'three' at 8..13");
151}
152
153# Adjacent tokens - no space between consecutive tokens (SpaceAfter=No)
154my $adjacent_data = <<'CONLLU';
155# filename = TEST/TEST/TEST_ADJ_001/base/tokens.xml
156# text_id = TEST_TEST.TEST_ADJ_001
157# text = foo(bar)baz end
1581 foo foo NOUN n. _ 0 root _ SpaceAfter=No
1592 ( ( PUNCT skyr. _ 3 punct _ SpaceAfter=No
1603 bar bar NOUN n. _ 1 appos _ SpaceAfter=No
1614 ) ) PUNCT skyr. _ 3 punct _ SpaceAfter=No
1625 baz baz NOUN n. _ 1 conj _ _
1636 end end NOUN n. _ 1 conj _ _
164
165CONLLU
166
167my $adj_file = "$test_tempdir/test_adjacent.conllu";
168{
169 open(my $afh, '>:encoding(UTF-8)', $adj_file)
170 or die "Cannot write test file: $!";
171 print $afh $adjacent_data;
172 close($afh);
173}
174
175my $zipcontent_adj = '';
176script_runs(
177 [ 'script/conllu2korapxml', '-f', 'ud', $adj_file ],
178 { stdout => \$zipcontent_adj },
179 "conllu2korapxml handles adjacent tokens without spaces"
180);
181
182my $zipfile_adj = "$test_tempdir/test_adjacent.zip";
183if ($zipcontent_adj) {
184 open(my $zfh, '>:raw', $zipfile_adj) or die "Cannot write zip: $!";
185 print $zfh $zipcontent_adj;
186 close($zfh);
187
188 my $morpho_adj = `$UNZIP -p $zipfile_adj 'TEST/TEST/TEST_ADJ_001/ud/morpho.xml' 2>/dev/null`;
189
190 # "foo(bar)baz end" = 15 chars
191 # foo: 0-3, (: 3-4, bar: 4-7, ): 7-8, baz: 8-11, end: 12-15
192 like($morpho_adj,
193 qr/id="s1_n1" from="0" to="3"/,
194 "Adjacent: token 'foo' at 0..3");
195 like($morpho_adj,
196 qr/id="s1_n2" from="3" to="4"/,
197 "Adjacent: token '(' at 3..4 (no space before)");
198 like($morpho_adj,
199 qr/id="s1_n3" from="4" to="7"/,
200 "Adjacent: token 'bar' at 4..7 (no space before)");
201 like($morpho_adj,
202 qr/id="s1_n4" from="7" to="8"/,
203 "Adjacent: token ')' at 7..8 (no space before)");
204 like($morpho_adj,
205 qr/id="s1_n5" from="8" to="11"/,
206 "Adjacent: token 'baz' at 8..11 (no space before)");
207 like($morpho_adj,
208 qr/id="s1_n6" from="12" to="15"/,
209 "Adjacent: token 'end' at 12..15 (space before)");
210}
211else {
212 fail("Adjacent: token 'foo' at 0..3");
213 fail("Adjacent: token '(' at 3..4 (no space before)");
214 fail("Adjacent: token 'bar' at 4..7 (no space before)");
215 fail("Adjacent: token ')' at 7..8 (no space before)");
216 fail("Adjacent: token 'baz' at 8..11 (no space before)");
217 fail("Adjacent: token 'end' at 12..15 (space before)");
218}
219
220# Repeated token form - same word appears multiple times in sentence
221my $repeat_data = <<'CONLLU';
222# filename = TEST/TEST/TEST_REP_001/base/tokens.xml
223# text_id = TEST_TEST.TEST_REP_001
224# text = the cat and the dog
2251 the the DET det. _ 2 det _ _
2262 cat cat NOUN n. _ 0 root _ _
2273 and and CCONJ cc. _ 5 cc _ _
2284 the the DET det. _ 5 det _ _
2295 dog dog NOUN n. _ 2 conj _ _
230
231CONLLU
232
233my $rep_file = "$test_tempdir/test_repeat.conllu";
234{
235 open(my $rfh, '>:encoding(UTF-8)', $rep_file)
236 or die "Cannot write test file: $!";
237 print $rfh $repeat_data;
238 close($rfh);
239}
240
241my $zipcontent_rep = '';
242script_runs(
243 [ 'script/conllu2korapxml', '-f', 'ud', $rep_file ],
244 { stdout => \$zipcontent_rep },
245 "conllu2korapxml handles repeated token forms"
246);
247
248my $zipfile_rep = "$test_tempdir/test_repeat.zip";
249if ($zipcontent_rep) {
250 open(my $zfh, '>:raw', $zipfile_rep) or die "Cannot write zip: $!";
251 print $zfh $zipcontent_rep;
252 close($zfh);
253
254 my $morpho_rep = `$UNZIP -p $zipfile_rep 'TEST/TEST/TEST_REP_001/ud/morpho.xml' 2>/dev/null`;
255
256 # "the cat and the dog" = 19 chars
257 # the: 0-3, cat: 4-7, and: 8-11, the: 12-15, dog: 16-19
258 like($morpho_rep,
259 qr/id="s1_n1" from="0" to="3"/,
260 "Repeated: first 'the' at 0..3");
261 like($morpho_rep,
262 qr/id="s1_n2" from="4" to="7"/,
263 "Repeated: 'cat' at 4..7");
264 like($morpho_rep,
265 qr/id="s1_n4" from="12" to="15"/,
266 "Repeated: second 'the' at 12..15 (not matching first)");
267 like($morpho_rep,
268 qr/id="s1_n5" from="16" to="19"/,
269 "Repeated: 'dog' at 16..19");
270}
271else {
272 fail("Repeated: first 'the' at 0..3");
273 fail("Repeated: 'cat' at 4..7");
274 fail("Repeated: second 'the' at 12..15 (not matching first)");
275 fail("Repeated: 'dog' at 16..19");
276}
277
278# Single-token sentence - minimal sentence with only one word
279my $single_data = <<'CONLLU';
280# filename = TEST/TEST/TEST_SNG_001/base/tokens.xml
281# text_id = TEST_TEST.TEST_SNG_001
282# text = Hello
2831 Hello hello INTJ intj. _ 0 root _ _
284
285# text = World
2861 World world NOUN n. _ 0 root _ _
287
288CONLLU
289
290my $sng_file = "$test_tempdir/test_single.conllu";
291{
292 open(my $sfh, '>:encoding(UTF-8)', $sng_file)
293 or die "Cannot write test file: $!";
294 print $sfh $single_data;
295 close($sfh);
296}
297
298my $zipcontent_sng = '';
299script_runs(
300 [ 'script/conllu2korapxml', '-f', 'ud', $sng_file ],
301 { stdout => \$zipcontent_sng },
302 "conllu2korapxml handles single-token sentences"
303);
304
305my $zipfile_sng = "$test_tempdir/test_single.zip";
306if ($zipcontent_sng) {
307 open(my $zfh, '>:raw', $zipfile_sng) or die "Cannot write zip: $!";
308 print $zfh $zipcontent_sng;
309 close($zfh);
310
311 my $morpho_sng = `$UNZIP -p $zipfile_sng 'TEST/TEST/TEST_SNG_001/ud/morpho.xml' 2>/dev/null`;
312
313 # Sentence 1: "Hello" (5 chars, starts at 0)
314 # Sentence 2: "World" (5 chars, starts at 6 = 5 + 1 space)
315 like($morpho_sng,
316 qr/id="s1_n1" from="0" to="5"/,
317 "Single-token: 'Hello' at 0..5");
318 like($morpho_sng,
319 qr/id="s2_n1" from="6" to="11"/,
320 "Single-token: 'World' at 6..11 (after space separator)");
321}
322else {
323 fail("Single-token: 'Hello' at 0..5");
324 fail("Single-token: 'World' at 6..11 (after space separator)");
325}
326
327# Explicit offsets take precedence over # text when both are present
328my $explicit_wins_data = <<'CONLLU';
329# filename = TEST/TEST/TEST_EXP_001/base/tokens.xml
330# text_id = TEST_TEST.TEST_EXP_001
331# text = Geras rytas.
332# start_offsets = 0 100 200 300
333# end_offsets = 999 199 299 399
3341 Geras geras ADJ bdv. Case=Nom 2 amod _ _
3352 rytas rytas NOUN dkt. Case=Nom 0 root _ _
3363 . . PUNCT skyr. _ 2 punct _ _
337
338CONLLU
339
340my $exp_file = "$test_tempdir/test_explicit.conllu";
341{
342 open(my $efh, '>:encoding(UTF-8)', $exp_file)
343 or die "Cannot write test file: $!";
344 print $efh $explicit_wins_data;
345 close($efh);
346}
347
348my $zipcontent_exp = '';
349script_runs(
350 [ 'script/conllu2korapxml', '-f', 'ud', $exp_file ],
351 { stdout => \$zipcontent_exp },
352 "conllu2korapxml uses explicit offsets when both # text and offsets present"
353);
354
355my $zipfile_exp = "$test_tempdir/test_explicit.zip";
356if ($zipcontent_exp) {
357 open(my $zfh, '>:raw', $zipfile_exp) or die "Cannot write zip: $!";
358 print $zfh $zipcontent_exp;
359 close($zfh);
360
361 my $morpho_exp = `$UNZIP -p $zipfile_exp 'TEST/TEST/TEST_EXP_001/ud/morpho.xml' 2>/dev/null`;
362 like($morpho_exp,
363 qr/id="s1_n1" from="100" to="199"/,
364 "Explicit offsets win: token uses from=100 (not computed 0)");
365}
366else {
367 fail("Explicit offsets win: token uses from=100 (not computed 0)");
368}
369
370done_testing;