Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 1 | #! /usr/bin/perl -w |
| 2 | |
| 3 | |
| 4 | ########################################################################################################################################################### |
| 5 | # vrt2tei.pl |
| 6 | # eureco |
| 7 | # leibniz-institut fuer deutsche sprache / csc finland esbo |
| 8 | # august 2024 |
| 9 | # |
| 10 | # |
| 11 | # using XML::Twig , see http://www.xmltwig.org/, https://metacpan.org/pod/XML::Twiga |
| 12 | # |
Harald Lüngen | ccd8490 | 2024-08-27 16:03:47 +0300 | [diff] [blame] | 13 | # usage: see below the usage fugnction |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 14 | # Usage: ./vrt2tei.pl <vrtxmlfile.xml> <outfile> |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 15 | # <vrtxmlfile>: xml-ised vrt file |
| 16 | # |
| 17 | # |
| 18 | # TODO: |
| 19 | # 1 insert dtd spec, or ref to TEI |
| 20 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 21 | # 3a remove the vrt positional attribute comment line / all comment lines |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 22 | # 3b add @head and @deprel to I5 sowie auch @msd |
| 23 | # 3c bearbeitung von @head und @deprel in tei2korapxml durch Nils? |
| 24 | # 3d build 30 billion corpus |
| 25 | |
| 26 | # 4a take care of IDs |
| 27 | # 4b see to the values of @xml:lang |
| 28 | # 5 abfangen von unerwarteten elementen dh andere als <sentence> und <paragraph> |
| 29 | # 5a wort reihenfolge nochmal checken |
| 30 | # 6 checks and balances |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 31 | # 7 How to encode Kielipankki and National Library of Finland? in teiCorpus Header |
| 32 | # 8 construct <idsDoc>s for the months (or go for TEI) |
| 33 | # 9 parallelisation in bash and application on sub corpora of KLK |
| 34 | # 10 re-implementation of the gawk code in the perl script |
| 35 | # 12 re-implement creation of text header from xml file in another twig / parametrize TEI vs I5 |
| 36 | |
| 37 | |
| 38 | |
| 39 | #remember |
| 40 | #formatted.xml:105613: element w: validity error : No declaration for attribute deprel of element w |
| 41 | #formatted.xml:105613: element w: validity error : No declaration for attribute head of element w |
| 42 | |
| 43 | |
| 44 | # |
| 45 | # |
| 46 | ############################################################################################################################################################ |
| 47 | |
| 48 | |
| 49 | use strict; |
| 50 | use warnings; |
| 51 | |
| 52 | use XML::Twig; |
| 53 | use XML::Generator ':pretty'; # apparently no effect when using flush(); |
| 54 | |
| 55 | |
| 56 | use locale; # diese drei Zeilen, damit \b im regex nicht Umlaute und ß matcht. |
| 57 | use POSIX qw(locale_h); # to be able to use setlocale() |
| 58 | #setlocale(LC_ALL,'de_DE'); |
| 59 | setlocale(LC_ALL, "fi_FI"); |
| 60 | use utf8; |
| 61 | use open qw( :std :encoding(UTF-8) ); |
| 62 | |
| 63 | use Time::Piece; |
| 64 | use Tie::IxHash; |
| 65 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 66 | |
| 67 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 68 | #---------------------- |
| 69 | # check file arguments: |
| 70 | #---------------------- |
| 71 | |
| 72 | # arg0 infile: vrt-xml |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 73 | |
Harald Lüngen | a20e69d | 2024-08-29 13:33:08 +0300 | [diff] [blame] | 74 | unless($ARGV[0]) {&usage_message()} ; # min arg0, the input file |
| 75 | if ($ARGV[1]) {&usage_message()}; # max arg0, the input file |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 76 | |
| 77 | |
| 78 | #################### |
| 79 | # GLOBAL VARIABLES |
| 80 | #################### |
| 81 | |
| 82 | my $encoding = "UTF-8"; |
| 83 | #my $encoding = "iso-8859-1"; # dieses $encoding ist NUR fuer das output s.u. twig funktion |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 84 | my $textcounter = 0; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 85 | |
| 86 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 87 | |
| 88 | my $twig=""; |
| 89 | my $teiCorpusHeaderDoc=""; |
| 90 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 91 | # global variables pertaining to the original corpus : |
| 92 | my $kielipankkiCorpus = "klk-fi-v2-vrt"; |
| 93 | |
| 94 | |
| 95 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 96 | |
| 97 | #------------------------------------------------------------------ |
| 98 | # read corpusHeaderSkeleton document and get header out of it |
| 99 | #------------------------------------------------------------------ |
| 100 | |
| 101 | my $teiCorpusHeaderDocTwig = new XML::Twig( |
| 102 | keep_spaces => 1, |
| 103 | keep_atts_order => 1, |
| 104 | comments => 'drop', |
| 105 | ); |
| 106 | |
| 107 | |
| 108 | $teiCorpusHeaderDocTwig->parsefile("teiCorpusHeaderSkeleton.tei.xml"); |
| 109 | my $corpusHeader = $teiCorpusHeaderDocTwig->root; # getting the teiHeader for corpus out of the teiCorpusHeaderSkeleton document |
| 110 | |
| 111 | |
| 112 | #------------------------------------------------------------------ |
| 113 | # read textHeaderSkeleton document adn get header out of it |
| 114 | #------------------------------------------------------------------ |
| 115 | |
| 116 | my $teiTextHeaderDocTwig = new XML::Twig( |
| 117 | keep_spaces => 1, |
| 118 | keep_atts_order => 1, |
| 119 | comments => 'drop', |
| 120 | ); |
| 121 | |
| 122 | $teiTextHeaderDocTwig->parsefile("teiTextHeaderSkeleton.tei.xml"); |
| 123 | my $textHeader = $teiTextHeaderDocTwig->root; # getting the teiHeader for corpus out of the teiTextHeaderSkeleton document |
| 124 | |
| 125 | |
| 126 | #---------------------------------- |
| 127 | # read input VRT-XML document |
| 128 | #---------------------------------- |
| 129 | |
| 130 | open(my $IN, "< $ARGV[0]") || die("$0: cannot open file for reading: $ARGV[0]"); # open input file and initialise filehandel, actually does not seem to be needed |
| 131 | # as parsefile() (s.b.) is applied to the filename |
| 132 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 133 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 134 | ##################### |
| 135 | # M A I N |
| 136 | ##################### |
| 137 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 138 | #------------------------------------------------------------------------------------------------------------- |
| 139 | # start twig for input and call start tag handler for root and twig handler for each <text> in the VRT |
| 140 | #------------------------------------------------------------------------------------------------------------- |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 141 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 142 | |
| 143 | $twig = new XML::Twig( |
| 144 | keep_spaces => 1, # dadurch auch whitespaces an ehemeligen elementgrenzen im output |
| 145 | keep_atts_order => 1, # requires Tie::IxHash |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 146 | comments => 'drop', |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 147 | start_tag_handlers => { |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 148 | texts => sub{root(@_, $corpusHeader)} |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 149 | }, |
| 150 | twig_handlers => { |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 151 | # text => \&text |
| 152 | text => sub{text(@_, $textHeader)} |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 153 | }, |
| 154 | # dtd_handlers => { # ToDo for I5 |
| 155 | # \&set_dtd; |
| 156 | # } |
Harald Lüngen | a20e69d | 2024-08-29 13:33:08 +0300 | [diff] [blame] | 157 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 158 | output_encoding => $encoding, |
| 159 | ); |
| 160 | |
| 161 | $twig->parsefile($ARGV[0]); |
| 162 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 163 | |
| 164 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 165 | |
| 166 | |
| 167 | ########### |
| 168 | # END MAIN |
| 169 | ########### |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | ############################## |
| 175 | # S U B R O U T I N E S |
| 176 | ############################## |
| 177 | |
| 178 | # sub set_dtd [ |
| 179 | # my $twig, $dtd = @_; |
| 180 | # my $internal = qq|\nPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n"DTD/xhtml1-strict.dtd"|; |
| 181 | # |
| 182 | # $twig->twig_doctype('html', undef, undef, $internal); |
| 183 | # } |
| 184 | |
| 185 | |
| 186 | |
| 187 | sub root { |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 188 | my ($twig, $root, $corpusHeader) =@_; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 189 | |
| 190 | $root->set_gi('teiCorpus'); |
| 191 | $root->set_att("xmlns", 'http://www.tei-c.org/ns/1.0'); |
| 192 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 193 | &insertCorpusHeader($root, $corpusHeader); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 197 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 198 | sub insertCorpusHeader{ |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 199 | my ($root, $corpusHeader) =@_; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 200 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 201 | #--------------------------------------------------------------------------- |
| 202 | # get some metadata for the current output corpus based on source and year |
| 203 | #--------------------------------------------------------------------------- |
| 204 | |
Harald Lüngen | ccd8490 | 2024-08-27 16:03:47 +0300 | [diff] [blame] | 205 | my @array = split(/\//, $ARGV[0]); |
| 206 | my $l = scalar(@array); |
| 207 | my $source = $array[$l-1]; |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 208 | $source =~ s/([0-9][0-9][0-9][0-9])\.xml$//; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 209 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 210 | my $language="Finnish"; |
| 211 | my $lang_tla="fi"; |
| 212 | |
| 213 | my $yy = $1; # $1 now containts substring in first bracket in regex above |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 214 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 215 | my $ctitle = $source . " " . $yy . ", from ". $kielipankkiCorpus . " for EuReCo"; # to do: also get name of corpus (klk-fi-v2-vrt) |
| 216 | |
| 217 | |
| 218 | #----------------------- |
| 219 | # set corpus header |
| 220 | #----------------------- |
| 221 | |
| 222 | &set_title( $corpusHeader, $source, $yy, $kielipankkiCorpus); |
| 223 | &set_sourceDesc($corpusHeader, $source, $yy, $kielipankkiCorpus); |
| 224 | |
| 225 | my $teiCorpusHeader = $corpusHeader->paste("first_child", $root); |
| 226 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 227 | } |
| 228 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 229 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 230 | #---------------------------- |
| 231 | # handler &text for <text> |
| 232 | #---------------------------- |
| 233 | |
| 234 | sub text { |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 235 | my ($twig, $text, $textHeader) = @_; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 236 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 237 | $textcounter++; |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 238 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 239 | |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 240 | # ToDo: catch all other, unexpected children of root |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 241 | |
| 242 | #-------------------------------------------------------------------------- |
| 243 | # Get text metadata (attributes of <text>) and create teiHeader for <text> |
| 244 | #-------------------------------------------------------------------------- |
| 245 | |
| 246 | my $textattsref = $text->atts(); # $textattsref is now a reference to a hash and should be used with '->' |
| 247 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 248 | |
| 249 | &createTextHeader($text, $textattsref, $textHeader); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 250 | |
| 251 | #-------------------------- |
| 252 | # create <TEI> from <text> |
| 253 | #-------------------------- |
| 254 | |
| 255 | # set vrt <text> to <TEI> and delete all attributes after they were were saved above |
| 256 | $text->del_atts; |
| 257 | $text->set_gi("TEI"); |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 258 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 259 | #------------------------------------------------------------------ |
| 260 | # create the <tei:text>, <body>, <div> elements inside <TEI> |
| 261 | #------------------------------------------------------------------ |
| 262 | |
| 263 | my $ttext_element = XML::Twig::Elt->new('text'); |
| 264 | my $body_element = XML::Twig::Elt->new('body'); |
| 265 | my $div_element = XML::Twig::Elt->new('div'); |
| 266 | |
| 267 | # set atts |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 268 | $div_element ->set_att("type", "page"); # ToDo: this is specific to KLK |
| 269 | $ttext_element->set_att("xml:lang", 'fi'); # as in ICC-NOR |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 270 | |
| 271 | # paste |
| 272 | $ttext_element->paste('last_child', $text); |
| 273 | $body_element ->paste('last_child', $ttext_element); |
| 274 | $div_element ->paste('last_child', $body_element); |
| 275 | |
| 276 | |
| 277 | #------------------------------- |
| 278 | # create <p> from <paragraph> |
| 279 | #------------------------------- |
| 280 | |
| 281 | my @paragraphs = $text->children( 'paragraph'); |
| 282 | |
| 283 | foreach my $paragraph (@paragraphs) { |
| 284 | |
| 285 | &setP($paragraph); |
| 286 | |
| 287 | $paragraph->move('last_child', $div_element); |
| 288 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 289 | #------------------------------ |
| 290 | # create <s> from <sentence> |
| 291 | #------------------------------ |
| 292 | |
| 293 | my @sentences = $paragraph->children('sentence'); |
| 294 | foreach my $sentence (@sentences) { |
| 295 | |
| 296 | &setS($sentence); |
| 297 | |
| 298 | |
| 299 | #-------------------------------------- |
| 300 | # create <w> (word) from each $line |
| 301 | #-------------------------------------- |
| 302 | |
| 303 | my @lines = split(/\n+/, $sentence->xml_text); |
| 304 | $sentence->set_text("\n"); |
| 305 | |
| 306 | for my $line (@lines){ # Todo: Reihenfolge checken |
| 307 | if($line ne "" ){ |
| 308 | my $w_element = XML::Twig::Elt->new('w'); |
| 309 | &createW($w_element, $line); |
| 310 | $w_element->paste('last_child', $sentence); |
| 311 | } |
Harald Lüngen | caab080 | 2024-08-23 17:28:22 +0300 | [diff] [blame] | 312 | } # end words |
| 313 | } # end sentences |
| 314 | } # end paragraphs |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 315 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 316 | # $twig->set_pretty_print( 'record'); |
Harald Lüngen | a20e69d | 2024-08-29 13:33:08 +0300 | [diff] [blame] | 317 | # $twig->flush($OUT); |
| 318 | $twig->flush("/dev/stdout"); |
| 319 | } |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 320 | |
| 321 | sub createTextHeader{ |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 322 | my ($text, $textattsref, $textHeader) = @_; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 323 | |
| 324 | # USE 01 binding_id="2246025" |
| 325 | # USE 02 date="2021-01-15" |
| 326 | # 03 datefrom="20210115" |
| 327 | # 04 dateto="20210115" |
| 328 | # 05 elec_date="_" |
| 329 | # 06 file="" |
| 330 | # USE 07 filename_metadata="finclarin_siirto_k2021/0039-5552/2021/alto/2246025_0039-5552_2021-01-15_SK0221_mets.xml" |
| 331 | # USE 08 filename_orig ="finclarin_siirto_k2021/0039-5552/2021/alto/2246025_0039-5552_2021-01-15_SK0221_page-2021011502210030301.xml |
| 332 | # USE 09 id="t-bcd0f3fa-bbd3dac4" |
| 333 | # 10 img_url="" |
| 334 | # USE 11 issue_date="15.01.2021" |
| 335 | # USE 12 issue_no="SK0221" |
| 336 | # USE 13 issue_title="Suomen Kuvalehti" |
| 337 | # USE 14 label="Suomen Kuvalehti no. SK0221 15.01.2021" |
| 338 | # USE 16 language="fi" |
| 339 | # USE 17 page_id="p1" |
| 340 | # USE 18 page_no="None" |
| 341 | # 19 part_name="_" |
| 342 | # 20 publ_id="0039-5552" |
| 343 | # 21 publ_part="" |
| 344 | # USE 22 publ_title="Suomen Kuvalehti" |
| 345 | # USE 23 publ_type="aikakausi" |
| 346 | # USE 24 sentcount="70" |
| 347 | # USE 25 sum_lang="|xxx:44|fin:23|eng:3|" |
| 348 | # 26 timefrom="000000" |
| 349 | # 27 timeto="235959" |
| 350 | # USE 28 tokencount="304" |
| 351 | # 29 version_added="KLK-fi-2021"> |
| 352 | |
| 353 | |
| 354 | my $BID = $textattsref->{'binding_id'}; |
| 355 | my $DATE = $textattsref->{'date'}; |
| 356 | my $METAFILENAME = $textattsref->{'filename_metadata'}; |
| 357 | my $ORIGFILENAME = $textattsref->{'filename_orig'}; |
| 358 | my $ID = $textattsref->{'id'}; |
| 359 | my $ISSUEDATE = $textattsref->{'issue_date'}; |
| 360 | my $ISSUENO = $textattsref->{'issue_no'}; |
| 361 | my $ISSUETITLE = $textattsref->{'issue_title'}; |
| 362 | my $LABEL = $textattsref->{'label'}; |
| 363 | my $LANGUAGE = $textattsref->{'language'}; |
| 364 | my $PAGEID = $textattsref->{'page_id'}; |
| 365 | my $PAGENO = $textattsref->{'page_no'}; |
| 366 | my $PUBLTITLE = $textattsref->{'publ_title'}; |
| 367 | my $PUBLTYPE = $textattsref->{'publ_type'}; |
| 368 | my $SENTCOUNT = $textattsref->{'sentcount'}; |
| 369 | my $SUMLANG = $textattsref->{'sum_lang'}; |
| 370 | my $TOKENCOUNT = $textattsref->{'tokencount'}; |
| 371 | |
| 372 | |
| 373 | #----------------------------- |
| 374 | # Derived Metadata variables |
| 375 | #----------------------------- |
| 376 | |
| 377 | my @datearray = split("-", $DATE); |
| 378 | my @langarray = split("|", $SUMLANG); |
| 379 | my @namearray = split(/[\.\/]/, $ORIGFILENAME); # use $namearray[4] as ID for the page |
| 380 | |
| 381 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 382 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 383 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 384 | #----------------------------------------------------------------------- |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 385 | # CREATE text-teiHeader ACCORDING TO THE SKELETON in $textHeader |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 386 | #----------------------------------------------------------------------- |
| 387 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 388 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 389 | $textHeader->paste('first_child', $text); |
| 390 | |
| 391 | #----------------------------------------------- |
| 392 | # <teiHeader> |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 393 | # <fileDesc n="EuReCo-KLK-FIN_[$ID]"> |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 394 | # <titleStmt> |
| 395 | # <title>[$LABEL, page $PAGENO]</title> |
| 396 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 397 | $textHeader->first_child("fileDesc") -> set_att('n', "EuReCo-". $kielipankkiCorpus . $ID); |
| 398 | |
| 399 | $textHeader->first_child("fileDesc") -> first_child("titleStmt")->first_child("title") |
| 400 | ->set_text($LABEL . ", Text #" . $textcounter); # Case KLK; PAGENO scheint meist "None" zu sein |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 401 | |
| 402 | #----------------------------------------------- |
| 403 | # <fileDesc> |
| 404 | # <sourceDesc> |
| 405 | # <biblStruct> |
| 406 | # <analytic> |
| 407 | # <title type="main">[$LABEL, page $PAGENO]</title> |
| 408 | # <date>[$DATE]</date> |
| 409 | # <date type="year">TODO</date> |
| 410 | # <date type="month">TODO</date> |
| 411 | # <date type="day">TODO</date> |
| 412 | # <idno type="PAGEID">$PAGEID</idno> |
| 413 | # <idno type="BINDINGID">$BID</idno> |
| 414 | # <idno type="ID">$ID</idno> |
| 415 | # <idno type="KIELIPANKKI_METAFILENAME">$METAFILENAME</idno> |
| 416 | # <idno type="KIELIPANKKI_ORIGFILENAME">$ORIGFILENAME</idno> |
| 417 | # <textLang>$LANGUAGE</textLang> |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 418 | |
| 419 | my $analytic = $textHeader->get_xpath("./fileDesc/sourceDesc/biblStruct/analytic", 0); |
| 420 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 421 | $analytic->first_child("title") ->set_text($LABEL . ", Text #" . $textcounter); # Case KLK; PAGENO scheint meist "None" zu sein |
| 422 | $analytic->get_xpath('./date[@type="date"]', 0) ->set_text($DATE); |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 423 | $analytic->get_xpath('./date[@type="year"]', 0) ->set_text($datearray[0]); |
| 424 | $analytic->get_xpath('./date[@type="month"]', 0) ->set_text($datearray[1]); |
| 425 | $analytic->get_xpath('./date[@type="day"]', 0) ->set_text($datearray[2]); |
| 426 | $analytic->get_xpath('./idno[@type="PAGEID"]', 0) ->set_text($PAGEID); |
| 427 | $analytic->get_xpath('./idno[@type="BINDINGID"]', 0) ->set_text($BID); |
| 428 | $analytic->get_xpath('./idno[@type="ID"]', 0) ->set_text($ID); |
| 429 | $analytic->get_xpath('./idno[@type="KIELIPANKKI_METAFILENAME"]', 0) ->set_text($METAFILENAME); |
| 430 | $analytic->get_xpath('./idno[@type="KIELIPANKKI_ORIGFILENAME"]', 0) ->set_text($ORIGFILENAME); |
| 431 | $analytic->first_child('textLang') ->set_text($LANGUAGE); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 432 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 433 | # <monogr> |
| 434 | # <title>$PUBLTITLE</title> |
| 435 | # <imprint> |
| 436 | # <pubPlace>TODO</pubPlace> |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 437 | # <publisher>TODO</publisher> |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 438 | # </imprint> |
| 439 | # <biblScope unit="ISSUETITLE"/> |
| 440 | # <biblScope unit="ISSUENO"/> |
| 441 | # <biblScope unit="ISSUEDATE"/> |
| 442 | # <biblScope unit="pp">$PAGENO</biblScope> |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 443 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 444 | my $monogr = $textHeader->get_xpath("./fileDesc/sourceDesc/biblStruct/monogr", 0); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 445 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 446 | $monogr->first_child("title") ->set_text($PUBLTITLE); |
| 447 | $monogr->first_child("imprint")->first_child("pubPlace") ->set_text("ToDo"); # imprint is needed for tei validity |
| 448 | $monogr->first_child("imprint")->first_child("publisher") ->set_text("ToDo"); # imprint is needed for tei validity |
| 449 | $monogr->get_xpath('./biblScope[@unit="ISSUETITLE"]', 0) ->set_text($ISSUETITLE); |
| 450 | $monogr->get_xpath('./biblScope[@unit="ISSUENO"]', 0) ->set_text($ISSUENO); |
| 451 | $monogr->get_xpath('./biblScope[@unit="ISSUEDATE"]', 0) ->set_text($ISSUEDATE); |
| 452 | $monogr->get_xpath('./biblScope[@unit="pp"]', 0) ->set_text($PAGENO); # Achtung - PAGENO scheint meist "None" zu sein |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 453 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 454 | # <encodingDesc> |
| 455 | # <tagsDecl> |
| 456 | # <namespace name="http://www.tei-c.org/ns/1.0"> |
| 457 | # <tagUsage gi="s" occurs="SENTCOUNT"/> |
| 458 | # <tagUsage gi="w" occurs="TOKENCOUNT"/> |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 459 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 460 | $textHeader->get_xpath('./encodingDesc/tagsDecl/namespace/tagUsage[@gi="s"]', 0) -> set_att('occurs', $SENTCOUNT); |
| 461 | $textHeader->get_xpath('./encodingDesc/tagsDecl/namespace/tagUsage[@gi="w"]', 0) -> set_att('occurs', $TOKENCOUNT); |
| 462 | |
| 463 | # <profileDesc> |
| 464 | # <langUsage> |
| 465 | # <language ident="fi" usage="|xxx:44|fin:23|eng:3|"/> |
| 466 | # </langUsage> |
| 467 | # <textClass> |
| 468 | # <classCode scheme="kielipankki_klk">$PUBLTYPE</classCode> |
| 469 | # <classCode scheme="kielipankki_klk_mapped">TODO</classCode> |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 470 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 471 | $textHeader->get_xpath('./profileDesc/langUsage/language', 0) ->set_att('ident', $LANGUAGE); |
| 472 | $textHeader->get_xpath('./profileDesc/langUsage/language', 0) ->set_att('usage', $SUMLANG); |
| 473 | # in @usage muss eigt. ein integer; am besten inhalt von SUMLANG aufdroeseln und mehrere <language> machen |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 474 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 475 | $textHeader->get_xpath('./profileDesc/textClass/classCode[@scheme="kielipankki_klk"]', 0) ->set_text($PUBLTYPE); |
| 476 | $textHeader->get_xpath('./profileDesc/textClass/classCode[@scheme="kielipankki_klk_mapped"]', 0)->set_text("ToDo"); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 477 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 478 | # <revisionDesc> |
| 479 | # <change when="TODO" who="HL">TEI version for EuReCo</change> |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 480 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 481 | $textHeader->get_xpath('./revisionDesc/change', 0) ->set_att('when', localtime->ymd('-')); |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 482 | |
| 483 | |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 484 | #----------------------------------- |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 485 | # END OF CREATING TEIHEADER |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 486 | #----------------------------------- |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 487 | |
| 488 | } |
Harald Lüngen | 695ac1d | 2024-09-05 08:55:21 +0300 | [diff] [blame^] | 489 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 490 | sub setP { |
| 491 | my ($paragraph) = @_; |
| 492 | |
| 493 | $paragraph->set_gi('p'); |
| 494 | |
| 495 | # <paragraph id="p-bcd0f3fa-bbd3dac4-815ead7a" sum_lang="|fin:1|"> |
| 496 | # atts of <paragraph>: |
| 497 | # @id USE |
| 498 | # @sum_lang USE: put in xml:lang and prefix the value with "x-" for private value |
| 499 | |
| 500 | $paragraph->set_att("xml:lang", "x-" . $paragraph->att("sum_lang")); |
| 501 | $paragraph->del_att("sum_lang"); |
Harald Lüngen | a20e69d | 2024-08-29 13:33:08 +0300 | [diff] [blame] | 502 | # $paragraph->change_att_name('id', 'xml:id'); |
| 503 | $paragraph->del_att("id"); # diese id ist auch nicht eindeutig!! |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 504 | } |
| 505 | sub setS { |
| 506 | my ($sentence) = @_; |
| 507 | |
| 508 | $sentence->set_gi('s'); |
| 509 | |
| 510 | # the atts of <sentence>: |
| 511 | # USE 1 @id="s-bcd0f3fa-bbd3dac4-f7429090" |
| 512 | # USE 2 @lang="fin" -> xml:lang |
| 513 | # ? 3 @lang_conf="0.6734853"> -> ToDo @cert ? |
| 514 | |
| 515 | # set attrs of <s> |
| 516 | $sentence->set_att("xml:lang", $sentence->att("lang")); # ToDo: convert the value / introduce a hash for lookup (input values: "fin", "xxx", ....) |
Harald Lüngen | faf8d48 | 2024-08-27 21:19:47 +0300 | [diff] [blame] | 517 | # $sentence->change_att_name('id', 'xml:id'); # nicht eindeutig |
| 518 | $sentence->del_att('id'); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 519 | $sentence->del_att("lang"); # replaced by xml:lang |
| 520 | $sentence->del_att("lang_conf"); # for the time being |
| 521 | |
| 522 | } |
| 523 | |
| 524 | sub createW { |
| 525 | my ($w_element, $line) = @_; |
| 526 | |
| 527 | #--------------------------- |
| 528 | # Get the tags (=columns) |
| 529 | #--------------------------- |
| 530 | |
| 531 | my @tags = split(/\t/, $line); |
| 532 | |
| 533 | # set content of <w> i.e. the token |
| 534 | $w_element->set_text($tags[0]); |
| 535 | |
| 536 | # vrt positional-attributes in corpus KLK: |
| 537 | # USE [0] word |
| 538 | # USE [1] ref (id for reference of dephead) |
| 539 | # USE [2] lemma |
| 540 | # ? [3] lemmacomp (lemma with compound info - could go in @norm, as tag abuse?) |
| 541 | # USE [4] pos |
| 542 | # USE [5] msd |
| 543 | # USE [6] dephead |
| 544 | # USE [7] deprel |
| 545 | # [8] content (ocr-process) |
| 546 | # [9] vpos (ocr-process) |
| 547 | # [10] ocr (ocr-process) |
| 548 | # [11] cc (ocr-process) |
| 549 | # [12] hyph (ocr-process) |
| 550 | # [13] style (ocr-process) |
| 551 | # [14] lex (korp semantic disambiguation from G"oteborg) |
| 552 | |
| 553 | # set the attributes of <w>: |
| 554 | $w_element->set_att("n", $tags[1]); |
| 555 | # $w_element->set_att("id", "w_" . $namearray[4] . $sentence->att("xml:id") . "_" . $tags[1]); |
| 556 | # so zusammengebaute ID ist auch nicht eindeutig... |
Harald Lüngen | faf8d48 | 2024-08-27 21:19:47 +0300 | [diff] [blame] | 557 | $w_element->del_att("id"); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 558 | $w_element->set_att("lemma", $tags[2]); |
| 559 | # $w_element->set_att("norm", $tags[3]); # tag abuse of @norm |
| 560 | $w_element->set_att("pos", $tags[4]); |
| 561 | $w_element->set_att("msd", $tags[5]); |
Harald Lüngen | ccd8490 | 2024-08-27 16:03:47 +0300 | [diff] [blame] | 562 | #TMP $w_element->set_att("head", $tags[6]); |
| 563 | #TMP $w_element->set_att("deprel", $tags[7]); |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 564 | |
| 565 | } |
| 566 | |
Harald Lüngen | db5e6e7 | 2024-09-04 17:41:18 +0300 | [diff] [blame] | 567 | |
| 568 | sub set_title{ |
| 569 | my ($corpusHeader, $source, $yy, $kielipankkiCorpus) = @_; |
| 570 | |
| 571 | my $cTitleString = $source . " " . $yy . ", from ". $kielipankkiCorpus . " for EuReCo"; |
| 572 | |
| 573 | #<teiHeader> |
| 574 | # <fileDesc> |
| 575 | # <titleStmt> |
| 576 | # <title>[Aamulehti2021] from [klk-fi-v2-vrt for] EuReCo</title> |
| 577 | # </titleStmt> |
| 578 | # <!-- ... --> |
| 579 | # </fileDesc> |
| 580 | #</teiHeader> |
| 581 | |
| 582 | my $cTitleNode = $corpusHeader->first_child("fileDesc")->first_child("titleStmt")->first_child("title"); |
| 583 | |
| 584 | $cTitleNode->set_text($cTitleString); |
| 585 | |
| 586 | } |
| 587 | |
| 588 | sub set_sourceDesc{ |
| 589 | my ($corpusHeader, $source, $yy, $kielipankkiCorpus) = @_; |
| 590 | |
| 591 | my $cBiblString = $source . " " . $yy . ", from ". $kielipankkiCorpus . " for EuReCo"; |
| 592 | |
| 593 | #<teiHeader> |
| 594 | # <fileDesc> |
| 595 | # <!-- ... --> |
| 596 | # <sourceDesc> |
| 597 | # <bibl>[Aamulehti2021] from [klk-fi-v2-vrt]</bibl> |
| 598 | # </sourceDesc> |
| 599 | # <!-- ... --> |
| 600 | # </fileDesc> |
| 601 | #</teiHeader> |
| 602 | |
| 603 | my $cBiblNode = $corpusHeader->first_child("fileDesc")->first_child("sourceDesc")->first_child("bibl"); |
| 604 | |
| 605 | $cBiblNode->set_text($cBiblString); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 614 | ################# |
| 615 | ## usage_message |
| 616 | ################# |
| 617 | |
| 618 | |
| 619 | sub usage_message { |
Harald Lüngen | a7e9162 | 2024-08-23 17:33:11 +0300 | [diff] [blame] | 620 | print " Usage: ./vrt2tei.pl <file.vrt.xml> <outfile>\n"; |
Harald Lüngen | 9d4e046 | 2024-08-23 09:34:22 +0300 | [diff] [blame] | 621 | print " <file.vrt.xml> is a VRT file converted to proper XML\n"; |
| 622 | exit; |
| 623 | } |
| 624 | |
| 625 | |