Debian Codegraph @

importers/tei-p4/xdf2tei.pl

raw
1 #!/usr/bin/perl -w
2 # $Id$
3 # (c) Oct 2004 - July 2005 Michael Bunk
4 # This is GPL software.
5 #
6
7 use strict;
8
9 if(-t)
10 {
11 # we are connected to a terminal - print help
12 print STDERR "Call me as: $0 <inputfile.xdf >outputfile.tei\n";
13 print STDERR <<'ENDOFDOCS';
14
15 This script converts an xdf file from stdin into a TEI file on stdout. You will
16 need to prepend a TEI header and append some closing TEI tags to generate a
17 complete TEI file.
18
19 xdf was documented here: http://fdicts.com/xdf.php
20 Now that they moved to http://dicts.info, it seems the xdf format was
21 abandoned, since I didn't find documentation for it anymore.
22
23
24 Short Summary
25 -------------
26
27 # comment line
28 word1 [tabulator] word2 [tabulator] note1 [tabulator] note2 [tabulator] translator
29
30 The first note column contains information about word class. Standard are:
31 m - masculine noun
32 f - feminine noun
33 n - neuter noun
34 pl - plural noun
35 n: - noun
36 v: - verb
37 adj: - adjective
38 adv: - adverb
39 prep: - preposition
40 conj: - conjunction
41 interj: - interjection
42
43 Or information about the sphere (domain) where it is commony used if it is not
44 a common word. Standard are:
45
46 [abbr.] - abbreviation [fin.] - finance [myt.] - mythology
47 [agr.] - agricultural [geo.] - geographical [phra.] - phrase
48 [astr.] - astronomy [geol.] - geology [phy.] - physics
49 [aut.] - automobile industry [hist.] - history [polit.] - politics
50 [bio.] - biology [it.] - information technologies [rel.] - religion
51 [bot.] - botany [law.] - law term [sex.] - sexual term
52 [chem.] - chemistry [mat.] - mathematics [slang.] - slang term
53 [chil.] - children speech [med.] - medicine [sport.] - sport term
54 [col.] - colloquial [mil.] - military [tech.] - technology
55 [el.] - electrotechnics [mus.] - musical term [vulg.] - vulgar term
56
57 Or special notes which are specified in each xdf file they are used in. Special
58 notes are in () braces.
59
60 Example:
61 # Comment. Special note (dv) used for derived verb
62 work some word (dv) note for this translation John Smith
63
64
65 Mapping into TEI
66 ----------------
67
68 We split each line at the tabulator into 5 variables.
69 $note1 is further analyzed:
70
71 $word1 -> <orth>
72 $word2 -> <tr>
73 $note1 = note1a + note1b => <pos> + <usg type="dom">
74 $note2 -> <note>
75 $translator -> <note resp="$translator">
76
77 <entry>
78 <form>
79 <orth>dog</orth>
80 </form>
81 <gramGrp><pos>note1a</pos></gramGrp>
82 <trans>
83 <usg type="dom">note1b</usg>
84 <tr>word2</tr>
85 </trans>
86 <note resp="translator">note2</note>
87 </entry>
88 ENDOFDOCS
89 exit 0;
90 }
91
92 sub htmlencode
93 {
94 my $s = shift;
95 $s =~ s/&/&amp;/g;
96 $s =~ s/\"/&quot;/g;
97 $s =~ s/</&lt;/g;
98 $s =~ s/>/&gt;/g;
99 return $s;
100 }
101
102 sub mywarn
103 {
104 my $headword = shift;
105 print STDERR "$headword [$.]:\t", @_, "\n";
106 }
107
108 my $entries = 0;
109 while(<>)
110 {
111 # remove DOS line ending
112 s/\r\n//;
113
114 # remove Unicode Byte Order Mark
115 if(/\xef\xbb\xbf/)
116 {
117 print STDERR "removing Byte Order Mark\n";
118 s/\xef\xbb\xbf//;
119 }
120
121 if(/^\s*#(.*)/)
122 {
123 # it would be best to put xdf comments into the header
124 # but for this we should extract them with some grep command
125 # and put them into the header manually
126 print "<!-- $_ -->\n";
127 next;
128 }
129
130 my($word1, $word2, $note1, $note2, $translator) = split /\t/;
131
132 if(!defined($word1))
133 {
134 mywarn '', "Empty headword (word1). Skipping.";
135 next;
136 }
137 if(!defined($word2))
138 {
139 mywarn '', "Empty translation (word2). Skipping.";
140 next;
141 }
142
143 my($pos, $number);
144 undef $pos;
145 my $domain = "";
146 undef $number;
147 my $genus = "";
148
149 if($note1)
150 {
151 my @notes1 = split /\s/, $note1;
152 foreach(@notes1)
153 {
154 if(/^(\w+):$/)
155 {
156 mywarn $word1, "Part-of-Speech already set: $1 (had: $pos; note='$_')" if $pos;
157 $pos = $1;
158 next;
159 }
160 if(/^(pl)\.?$/)
161 {
162 mywarn $word1, "Number already set: $1 (had: $number; note='$_')" if $number;
163 $number = $1;
164 next;
165 }
166 if(/^(m|f|n)$/)
167 {
168 mywarn $word1, "Genus already set: $1 (had: $genus; note='$_')" if $genus;
169 $genus = $1;
170 next;
171 }
172
173 if(/^\[([^\.]+)\.\]$/)
174 {
175 mywarn $word1, "Domain already set: $1 (had: $domain; note='$_')" if $domain;
176 $domain = $1;
177 next;
178 }
179
180 mywarn $word1, "Unmatched part of note1: '$_'";
181 }
182 }
183
184 my $pos1 = ""; my $number1 = ""; my $gen1= "";
185 $pos1 = "<pos>$pos</pos>" if $pos;
186 $number1 = "<number>$number</number>" if $number;
187 $gen1 = "<gen>$genus</gen>" if $genus;
188
189 print " <entry>\n";
190 print " <form>\n";
191 print " <orth>". htmlencode($word1) ."</orth>\n";
192 print " </form>\n";
193 print " <gramGrp>$pos1$gen1$number1</gramGrp>\n" if $pos || $number || $genus;
194 print " <trans>\n";
195 print " <usg type=\"dom\">$domain</usg>\n" if $domain;
196 print " <tr>". htmlencode($word2) ."</tr>\n";
197 print " </trans>\n";
198 my $r='';
199 $r = " resp=\"". htmlencode($translator) ."\"" if $translator;
200 print " <note$r>". (defined($note2)?htmlencode($note2):'') ."</note>\n"
201 if $translator || $note2;
202 print " </entry>\n";
203 $entries++;
204 }
205 print STDERR "Wrote $entries entries.\n";
206