acomp.pl
9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/perl -w
my $Version="1.1";
#####
# Version 1.0 Released September 9, 1997
# - Initial Release
# Version 1.1 Released October 24, 1997
# - added the -f flag to interpret fragments
my $Usage="Usage: acompl.pl [ -t -s -f ] [ -i fmt ] -m min -l lex Infile|- OutFile|-\n".
"Version: $Version\n".
"Desc: The 'acomp' program expands compound words in German orthography into\n".
" the constituent parts of those compound words. The program uses the\n".
" LDC German Lexicon as a basis for the expansions. Input is read from\n".
" InFile or STDIN if infile is '-' and output is written to Outfile or\n".
" STDOUT if OutFile is '-'\n".
"Options:\n".
" -m min Set the minimum length on a compound costituent part to 'min'.\n".
" Default is 2.\n".
" -l lex File name of the LDC German Dictionary\n".
" -t Disable the processing of triplet consonants\n".
" -s Disable the processing of -s and -en insertions\n".
" -f Divide word fragments as best as you can\n".
" -i fmt Set the input file formant to 'fmt'. The possible choices are:\n".
" txt -> plain text, the default\n".
" ctm -> CTM format, ignores all but the 5th column, and if\n".
" a division occurs and a confidence score is present,\n".
" the confidence score is copied to all parts.\n".
" stm -> STM format, change only the text field of the stm record\n".
"\n";
$Lex = "";
$MaxLen = 0;
%LexHash = ();
%CompHash = ();
$MinCompLen = 2;
$DoTriplet = 1;
$DoInserts = 1;
$InFmt = "txt";
$InFile = "";
$OutFile = "";
$Frag = 0;
$| = 1; # This will cause stdout to be flushed whenever we print to it.
use Getopt::Long;
my $ret = GetOptions ("l=s",
"m=s",
"t",
"s",
"i:s",
"f");
die "\n$usage\nError: Failed to parse argements" if (! $ret);
if (defined($opt_l)) { $Lex = $opt_l; } else { die("$Usage\n\nError: Lexicon required via -l.\n"); }
if (defined($opt_m)) { $MinCompLen = $opt_m; }
if (defined($opt_t)) { $DoTriplet = 0; $opt_t = 0;}
if (defined($opt_s)) { $DoInserts = 0; $opt_s = 0;}
if (defined($opt_f)) { $Frag = 1; $opt_f = 0;}
if (defined($opt_i)) {
die("$Usage\n\nError: Undefined input format '$opt_i'")
if ($opt_i !~ /^(txt|ctm|stm)$/);
$InFmt = $opt_i;
}
#### The main functions arguements:
if ($#ARGV > 1) { print "\n$Usage\nToo many arguements\n\n"; exit 1; }
if ($#ARGV == 0) { print "\n$Usage\nOutput Not Specified\n\n"; exit 1; }
if ($#ARGV == -1) { print "\n$Usage\nInput and Output Not Specified\n\n";
exit 1; }
$InFile=$ARGV[0];
$OutFile=$ARGV[1];
die("$Usage\nError: Input file $InFile does not exist\n")
if ($InFile ne "-" && ! -r $InFile);
########################## MAIN #####################D
&LoadLex();
open(IN,"$InFile") || die("Error: Unable to open input file '$InFile'");
open(OUT,">$OutFile") || die("Error: Unable to open output file '$OutFile'");
while (<IN>){
chop;
if ($_ =~ /^;;/){
print OUT "$_\n";
} elsif ($InFmt eq "txt"){
print OUT &String_LU($_)."\n";
} elsif ($InFmt eq "ctm"){
s/^\s+//;
local(@ctm);
local(@new_words);
local($i);
local($newt);
local($conf);
@ctm = split(/\s+/,$_);
if ($#ctm <= 4) { $conf = "" ; } else { $conf = $ctm[5]; }
$newt = &String_LU($ctm[4]);
@new_words = split(/\s+/,$newt);
for ($i=0; $i<=$#new_words; $i++){
printf OUT ("%s %s %.2f %.2f %s %s\n",$ctm[0],$ctm[1],
$ctm[2] + ($i * ($ctm[3] / ($#new_words+1))),
$ctm[3] / ($#new_words + 1), $new_words[$i], $conf);
}
} elsif ($InFmt eq "stm"){
s/^\s+//;
local(@sl) = split(/\s+/,$_,7);
local($txt);
die "Error: Not enough fields in stm line '$_'\n" if ($#sl < 4);
if ($#sl == 4){
print OUT "$sl[0] $sl[1] $sl[2] $sl[3] $sl[4]\n";
} else {
print OUT "$sl[0] $sl[1] $sl[2] $sl[3] $sl[4]";
if ($sl[5] =~ /^<.*>$/){
print OUT " $sl[5]";
$txt = "";
if ($#sl == 6){ $txt = $sl[6]; }
} else {
$txt = $sl[5];
if ($#sl == 6){ $txt .= " ".$sl[6]; }
}
if ($txt ne ""){
print OUT " ".&String_LU($txt);
}
print OUT "\n";
}
}
}
close (IN); close(OUT);
################# END OF MAIN #########################
###########################################################
sub String_LU{
local($s) = @_;
local($new) = "";
local($ne);
$s =~ tr/a-z/A-Z/;
$s =~ tr/\340-\377/\300-\337/;
foreach $word(split(/\s+/,$s)){
if ($word =~ /^\(.*\)$/){
$word =~ s/^\((.*)\)$/$1/;
local($lookup) = &Comp_LU($word);
if ($word !~ /-$/ || (! $Frag)){
foreach $ne(split(/\s+/,$lookup)){
$new .= "(".$ne.") ";
}
} else {
$lookup =~ s/(\S+-)$/aq($1)/;
$new .= "$lookup ";
}
} else {
$new .= &Comp_LU($word)." ";
}
}
$new =~ s/ $//;
$new;
}
sub Comp_LU{
local($text) = @_;
local(@Comps) = ();
local($comp);
local($db) = 0;
if ($CompHash{$text} !~ /^$/){
print "hashed $text = $CompHash{$text}\n" if ($db);
$CompHash{$text};
} else {
if ($Frag && $text =~ /-$/) {
print "Fragment Lookup '$text'\n" if ($db);
local($len) = length($text);
local($htext) = "";
local($nocomp) = 1;
local($i, $j);
for ($i=$len-2; $i>=$MinCompLen && $nocomp; $i--){
$htext = substr($text,0,$i);
&is_Compound("",$htext,"",*Comps);
if ($#Comps != -1){
print "Fragment match @Comps\n" if ($db);
foreach ($j=0; $j <= $#Comps; $j++){
$Comps[$j] .= " ".substr($text,$i);
}
$nocomp = 0;
}
}
} else {
print "compute $text " if ($db);
&is_Compound("",$text,"",*Comps);
}
if ($#Comps == -1) {
$comp = $text;
} elsif ($#Comps > 0) {
$comp = "{";
foreach $c (@Comps){
$comp .= " $c /";
}
$comp =~ s:/$:}:;
} else {
$comp = $Comps[0];
}
$CompHash{$text} = $comp;
print "compute $text = $comp\n" if ($db);
$comp;
}
}
sub is_Compound{
local($hist,$val,$ind,*comps) = @_;
local($sval);
local($resid);
local($vallen) = length($val);
local($srchlen) = $MaxLen;
local($db) = 0;
local($stop_recur) = 0;
local($i);
$srchlen = $vallen if ($vallen < $MaxLen);
printf $ind."is_Compound hist: '$hist' val: '$val' slen: $srchlen\n" if ($db);
if ($LexHash{$val} eq "1"){
### The word is already in the hashed list, return it.
local($co);
($co = "$hist $val") =~ s/^ //;
print "$ind******* SUCCESS Search value pre-defined '$co'\n" if ($db);
push(@comps,$co);
1;
} else {
### loop through all of the characters in a
for ($i=$srchlen; $i>=$MinCompLen && ! $stop_recur; $i--){
$sval = substr($val,0,$i);
print $ind." $sval\n" if ($db);
if ($LexHash{$sval} eq "1"){
$resid = substr($val,$i);
print $ind." Possible subword '$sval' residual '$resid'\n" if ($db);
if (length($resid) >= $MinCompLen){
if (&is_Compound($hist." $sval",$resid, $ind." |",*comps) == 1){
$stop_recur = 1;
print "$ind**** Curtailing search for normal lookup\n" if ($db);
} else {
print "$ind**** Failed to Curtail search\n" if ($db);
}
} else {
print "$ind**** Residual length < MinCompLen $MinCompLen\n" if ($db);
}
#### handle the geminate constant conditional
if (! $stop_recur && $DoTriplet && $sval =~ /([bcdfghjklmnpqrstvwxyz])\1$/){
$resid = substr($val,$i-1);
print $ind." Possible geminate consonate subword residual $resid\n" if ($db);
if (length($resid) >= $MinCompLen){
if (&is_Compound($hist." $sval",$resid, $ind." |",*comps) == 1){
$stop_recur = 1;
print "$ind**** Curtailing search for triplet consonants\n" if ($db);
} else {
print "$ind**** Failed to Curtail search\n" if ($db);
}
}
}
#### handle the insertion of syllables
if (! $stop_recur && $DoInserts && ( $resid =~ /^([sS]|[eE][nN])/ ) ){
local($gen);
if ($resid =~ /^[sS]/){ $resid = substr($val,$i+1); $gen = "s" }
if ($resid =~ /^[eE][nN]/){ $resid = substr($val,$i+2); $gen = "en" }
print $ind." Possible additional '$gen' on residual '$gen'$resid\n" if ($db);
if (length($resid) >= $MinCompLen){
if (&is_Compound($hist." $sval",$resid, $ind." |",*comps) == 1){
$stop_recur = 1;
print "$ind**** Curtailing search for insertion of syllables\n" if ($db);
} else {
print "$ind**** Failed to Curtail search\n" if ($db);
}
}
}
}
}
$stop_recur;
}
}
sub LoadLex{
local($len);
local($db) = 0;
open(LEX,$Lex) || die("Unable to open Lexicon '$Lex'");
print "Reading Lexicon: '$Lex'\n Each . is 10000 entries: " if ($db);
while (<LEX>){
chop;
($word) = split;
$word =~ tr/a-z/A-Z/;
$word =~ tr/\340-\377/\300-\337/;
if ($word =~ /[-_]/){
($sep = $word) =~ s/[-_]+/ /g;
($cmp = $word) =~ s/_//g;
foreach $sw(split(/\s+/,$sep)){
$LexHash{$sw} = "1";
$len = length($sw);
$MaxLen = $len if ($MaxLen < $len);
}
$CompHash{$cmp} = $sep;
if ($cmp =~ /-/){
$cmp =~ s/-//g;
$CompHash{$cmp} = $sep;
}
} else {
$LexHash{$word} = "1";
$CompHash{$word} = $word;
}
if (($. + 1) % 10000 == 0){
print "." if ($db);
}
}
print "\n $. entries loaded, Largest word is $MaxLen\n" if ($db);
close(LEX);
# print "LexHash:\n"; foreach $key(sort(keys %LexHash)) { print " $key -> $LexHash{$key}\n"; }
# print "CompHash:\n"; foreach $key(sort(keys %CompHash)) { print " $key -> $CompHash{$key}\n"; }
}