| Akron | f3efc9e | 2026-06-03 10:46:37 +0200 | [diff] [blame^] | 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
| 4 | use Test::Script; |
| 5 | use Test::TempDir::Tiny; |
| 6 | |
| 7 | my $UNZIP = `sh -c 'command -v unzip'`; |
| 8 | chomp $UNZIP; |
| 9 | |
| 10 | if ($UNZIP eq '') { |
| 11 | plan skip_all => 'No unzip executable found in PATH.'; |
| 12 | } |
| 13 | |
| 14 | my $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. |
| 18 | 1 Geras geras ADJ bdv. Case=Nom 2 amod _ _ |
| 19 | 2 rytas rytas NOUN dkt. Case=Nom 0 root _ _ |
| 20 | 3 . . PUNCT skyr. _ 2 punct _ SpaceAfter=No |
| 21 | |
| 22 | # text = Kaip sekasi? |
| 23 | 1 Kaip kaip ADV prv. _ 2 advmod _ _ |
| 24 | 2 sekasi sektis VERB vksm. _ 0 root _ SpaceAfter=No |
| 25 | 3 ? ? PUNCT skyr. _ 2 punct _ _ |
| 26 | |
| 27 | CONLLU |
| 28 | |
| 29 | my $test_tempdir = tempdir(); |
| 30 | my $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 | |
| 38 | my $zipcontent_off = ''; |
| 39 | script_runs( |
| 40 | [ 'script/conllu2korapxml', '-f', 'ud', $offset_file ], |
| 41 | { stdout => \$zipcontent_off }, |
| 42 | "conllu2korapxml computes offsets from # text" |
| 43 | ); |
| 44 | |
| 45 | my $zipfile_off = "$test_tempdir/test_offset.zip"; |
| 46 | if ($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 | } |
| 90 | else { |
| 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 |
| 102 | my $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 |
| 106 | 1 One one NUM num. _ 0 root _ _ |
| 107 | 2 two two NUM num. _ 1 flat _ _ |
| 108 | 3 three three NUM num. _ 1 flat _ _ |
| 109 | |
| 110 | CONLLU |
| 111 | |
| 112 | my $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 | |
| 120 | my $zipcontent_nsa = ''; |
| 121 | script_runs( |
| 122 | [ 'script/conllu2korapxml', '-f', 'ud', $nsa_file ], |
| 123 | { stdout => \$zipcontent_nsa }, |
| 124 | "conllu2korapxml handles tokens with no SpaceAfter" |
| 125 | ); |
| 126 | |
| 127 | my $zipfile_nsa = "$test_tempdir/test_no_spaceafter.zip"; |
| 128 | if ($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 | } |
| 147 | else { |
| 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) |
| 154 | my $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 |
| 158 | 1 foo foo NOUN n. _ 0 root _ SpaceAfter=No |
| 159 | 2 ( ( PUNCT skyr. _ 3 punct _ SpaceAfter=No |
| 160 | 3 bar bar NOUN n. _ 1 appos _ SpaceAfter=No |
| 161 | 4 ) ) PUNCT skyr. _ 3 punct _ SpaceAfter=No |
| 162 | 5 baz baz NOUN n. _ 1 conj _ _ |
| 163 | 6 end end NOUN n. _ 1 conj _ _ |
| 164 | |
| 165 | CONLLU |
| 166 | |
| 167 | my $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 | |
| 175 | my $zipcontent_adj = ''; |
| 176 | script_runs( |
| 177 | [ 'script/conllu2korapxml', '-f', 'ud', $adj_file ], |
| 178 | { stdout => \$zipcontent_adj }, |
| 179 | "conllu2korapxml handles adjacent tokens without spaces" |
| 180 | ); |
| 181 | |
| 182 | my $zipfile_adj = "$test_tempdir/test_adjacent.zip"; |
| 183 | if ($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 | } |
| 211 | else { |
| 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 |
| 221 | my $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 |
| 225 | 1 the the DET det. _ 2 det _ _ |
| 226 | 2 cat cat NOUN n. _ 0 root _ _ |
| 227 | 3 and and CCONJ cc. _ 5 cc _ _ |
| 228 | 4 the the DET det. _ 5 det _ _ |
| 229 | 5 dog dog NOUN n. _ 2 conj _ _ |
| 230 | |
| 231 | CONLLU |
| 232 | |
| 233 | my $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 | |
| 241 | my $zipcontent_rep = ''; |
| 242 | script_runs( |
| 243 | [ 'script/conllu2korapxml', '-f', 'ud', $rep_file ], |
| 244 | { stdout => \$zipcontent_rep }, |
| 245 | "conllu2korapxml handles repeated token forms" |
| 246 | ); |
| 247 | |
| 248 | my $zipfile_rep = "$test_tempdir/test_repeat.zip"; |
| 249 | if ($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 | } |
| 271 | else { |
| 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 |
| 279 | my $single_data = <<'CONLLU'; |
| 280 | # filename = TEST/TEST/TEST_SNG_001/base/tokens.xml |
| 281 | # text_id = TEST_TEST.TEST_SNG_001 |
| 282 | # text = Hello |
| 283 | 1 Hello hello INTJ intj. _ 0 root _ _ |
| 284 | |
| 285 | # text = World |
| 286 | 1 World world NOUN n. _ 0 root _ _ |
| 287 | |
| 288 | CONLLU |
| 289 | |
| 290 | my $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 | |
| 298 | my $zipcontent_sng = ''; |
| 299 | script_runs( |
| 300 | [ 'script/conllu2korapxml', '-f', 'ud', $sng_file ], |
| 301 | { stdout => \$zipcontent_sng }, |
| 302 | "conllu2korapxml handles single-token sentences" |
| 303 | ); |
| 304 | |
| 305 | my $zipfile_sng = "$test_tempdir/test_single.zip"; |
| 306 | if ($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 | } |
| 322 | else { |
| 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 |
| 328 | my $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 |
| 334 | 1 Geras geras ADJ bdv. Case=Nom 2 amod _ _ |
| 335 | 2 rytas rytas NOUN dkt. Case=Nom 0 root _ _ |
| 336 | 3 . . PUNCT skyr. _ 2 punct _ _ |
| 337 | |
| 338 | CONLLU |
| 339 | |
| 340 | my $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 | |
| 348 | my $zipcontent_exp = ''; |
| 349 | script_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 | |
| 355 | my $zipfile_exp = "$test_tempdir/test_explicit.zip"; |
| 356 | if ($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 | } |
| 366 | else { |
| 367 | fail("Explicit offsets win: token uses from=100 (not computed 0)"); |
| 368 | } |
| 369 | |
| 370 | done_testing; |