Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 1 | # vrt2xml.awk |
| 2 | # to be called for each yearly vrt file of KLK e.g. klk_fi_v2_2021.vrt |
| 3 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 4 | BEGIN { |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 5 | # getting OUTDIR and YEAR as parameters |
| 6 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 7 | OFS="\t"; |
| 8 | |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 9 | system("mkdir -p " OUTDIR); |
| 10 | |
| 11 | getline; |
| 12 | if($0 !~ /^\s*<!\-\- #vrt positional-attributes/) { |
| 13 | print "ERROR: Something went wrong when looking for positional attributes in first line" |
| 14 | } |
| 15 | else { |
| 16 | PATTS = $0; |
| 17 | } |
| 18 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 19 | CONTAIN["Aamulehti"] = 1; |
| 20 | CONTAIN["Etelä-Suomen Sanomat"] = 1; |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 21 | CONTAIN["Hämeen Sanomat"] = 1; |
| 22 | CONTAIN["Helsingin Sanomat"] = 1; |
| 23 | CONTAIN["Ilkka-Pohjalainen"] = 1; |
| 24 | CONTAIN["Ilta-Sanomat"] = 1; |
| 25 | CONTAIN["Iltalehti"] = 1; |
| 26 | CONTAIN["Kaleva"] = 1; |
| 27 | CONTAIN["Karjalainen"] = 1; |
| 28 | CONTAIN["Kauppalehti"] = 1; |
| 29 | CONTAIN["Keskipohjanmaa"] = 1; |
| 30 | CONTAIN["Keskisuomalainen"] = 1; |
| 31 | CONTAIN["Kouvolan Sanomat"] = 1; |
| 32 | CONTAIN["Kymen Sanomat"] = 1; |
| 33 | CONTAIN["Länsi-Savo"] = 1; |
| 34 | CONTAIN["Lapin Kansa"] = 1; |
| 35 | CONTAIN["Satakunnan Kansa"] = 1; |
| 36 | CONTAIN["Savon Sanomat"] = 1; |
| 37 | CONTAIN["Suomen Kuvalehti"] = 1; |
| 38 | CONTAIN["Turun Sanomat"] = 1; |
| 39 | |
| 40 | |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 41 | # start all outfiles according to selected titles in CONTAIN: |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 42 | for(c in CONTAIN){ |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 43 | |
| 44 | system("mkdir -p " OUTDIR "/" simplify(c)); |
| 45 | |
| 46 | OUTFILE = OUTDIR "/" simplify(c) "/" simplify(c) YEAR ".xml"; |
| 47 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 48 | printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") > OUTFILE; |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 49 | print PATTS >> OUTFILE; |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 50 | printf("<texts>\n") >> OUTFILE; |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 51 | print "Starting outfile " OUTFILE > "/dev/stderr"; |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 56 | /^\s*<text/ { |
| 57 | |
| 58 | # USE 01 binding_id="2246025" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 59 | # USE 02 date="2021-01-15" |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 60 | # 03 datefrom="20210115" |
| 61 | # 04 dateto="20210115" |
| 62 | # 05 elec_date="_" |
| 63 | # 06 file="" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 64 | # USE 07 filename_metadata="finclarin_siirto_k2021/0039-5552/2021/alto/2246025_0039-5552_2021-01-15_SK0221_mets.xml" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 65 | # USE 08 filename_orig="finclarin_siirto_k2021/0039-5552/2021/alto/2246025_0039-5552_2021-01-15_SK0221_page-2021011502210030301.xml |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 66 | # USE 09 id="t-bcd0f3fa-bbd3dac4" |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 67 | # 10 img_url="" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 68 | # USE 11 issue_date="15.01.2021" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 69 | # USE 12 issue_no="SK0221" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 70 | # USE 13 issue_title="Suomen Kuvalehti" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 71 | # USE 14 label="Suomen Kuvalehti no. SK0221 15.01.2021" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 72 | # USE 16 language="fi" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 73 | # USE 17 page_id="p1" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 74 | # USE 18 page_no="None" |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 75 | # 19 part_name="_" |
| 76 | # 20 publ_id="0039-5552" |
| 77 | # 21 publ_part="" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 78 | # USE 22 publ_title="Suomen Kuvalehti" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 79 | # USE 23 publ_type="aikakausi" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 80 | # USE 24 sentcount="70" |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 81 | # 25 sum_lang="|xxx:44|fin:23|eng:3|" |
| 82 | # 26 timefrom="000000" |
| 83 | # 27 timeto="235959" |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 84 | # USE 28 tokencount="304" |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 85 | # 29 version_added="KLK-fi-2021"> |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 86 | |
| 87 | |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 88 | BID = gensub(/^.+binding_id=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 89 | DATE = gensub(/^.+date=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 90 | METAFILENAME = gensub(/^.+filename_metadata=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 91 | ORIGFILENAME = gensub(/^.+filename_orig=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 92 | ID = gensub(/^.+id=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 93 | ISSUEDATE = gensub(/^.+issue_date=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 94 | ISSUENO = gensub(/^.+issue_no=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 95 | ISSUETITLE = gensub(/^.+issue_title=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 96 | LABEL = gensub(/^.+label=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 97 | LANGUAGE = gensub(/^.+language=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 98 | PAGEID = gensub(/^.+page_id=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 99 | PAGENO = gensub(/^.+page_no=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 100 | PUBLTITLE = gensub(/^.+publ_title=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 101 | PUBLTYPE = gensub(/^.+publ_type=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 102 | SENTCOUNT = gensub(/^.+sentcount=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 103 | TOKENCOUNT = gensub(/^.+tokencount=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 104 | |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 105 | |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 106 | # print BID, DATE, METAFILENAME, ORIGFILENAME, ID, ISSUEDATE, ISSUENO, ISSUETITLE, LABEL, LANGUAGE, PAGEID, PAGENO, PUBLTITLE, PUBLTYPE, SENTCOUNT, TOKENCOUNT |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 107 | |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 108 | OUTFILE = OUTDIR "/" simplify(PUBLTITLE) "/" simplify(PUBLTITLE) YEAR ".xml" ; |
| 109 | |
| 110 | # continue OUTFILEs if PUBTITLE of <text> is in CONTAIN: |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 111 | if(CONTAIN[PUBLTITLE]){print $0 > OUTFILE; } |
| 112 | } |
| 113 | |
| 114 | /^\s*<paragraph/ { |
| 115 | ## <paragraph id="p-bcd0f3fa-bbd3dac4-9a6ee1a8" sum_lang="|xxx:1|"> |
| 116 | PID = gensub(/^.+id=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 117 | SUMLANG = gensub(/^.+sum_lang=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 118 | |
| 119 | if(CONTAIN[PUBLTITLE]){print $0 >> OUTFILE; } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | /^\s*<sentence/ { |
| 125 | ## <sentence id="s-bcd0f3fa-bbd3dac4-5c959030" lang="xxx" lang_conf="0.1745286"> |
| 126 | SID = gensub(/^.+id=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 127 | LANG = gensub(/^.+lang=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 128 | LCONF = gensub(/^.+lang_conf=\"([^\"]+?)\".+$/, "\\1", "1", $0); |
| 129 | |
| 130 | if(CONTAIN[PUBLTITLE]){print $0 >> OUTFILE; } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | ($0 ~ /^\s*</ && $0 !~ /^.*<(\/?)(\!\-\-|text|paragraph|sentence)/) {printf("WARNING: unknown element in line %s\n%s", NR, $0) > "/dev/stderr";} |
| 135 | |
| 136 | |
| 137 | $0 !~ /^\s*</ { |
| 138 | if(CONTAIN[PUBLTITLE]){print $0 >> OUTFILE; } |
| 139 | } |
| 140 | |
| 141 | /^s*<\/(sentence|paragraph)>/ { |
| 142 | if(CONTAIN[PUBLTITLE]){print $0 >> OUTFILE; } |
| 143 | } |
| 144 | /^s*<\/text>/ { |
| 145 | if(CONTAIN[PUBLTITLE]){print $0 >> OUTFILE; } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | END { |
| 152 | |
| 153 | for(c in CONTAIN){ |
| 154 | if(CONTAIN[c] == 1){ |
Harald Lüngen | 984b2c6 | 2024-08-27 16:04:59 +0300 | [diff] [blame^] | 155 | OUTFILE = OUTDIR "/" simplify(c) "/" simplify(c) YEAR ".xml"; |
Harald Lungen | 7a0e674 | 2024-08-23 09:32:02 +0300 | [diff] [blame] | 156 | printf("</texts>\n") >> OUTFILE; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | |
| 163 | function simplify(str) |
| 164 | { |
| 165 | gsub(/\s+/, "_", str); |
| 166 | gsub(/ü/,"u", str); |
| 167 | gsub(/ä/,"a",str); gsub(/ö/,"o",str); |
| 168 | gsub(/Ü/,"U",str); |
| 169 | gsub(/Ä/,"A",str); |
| 170 | gsub(/Ö/,"O",str); |
| 171 | |
| 172 | return(str); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |