summaryrefslogtreecommitdiff
path: root/next-lib/src/parse/oald/asc2gf
blob: 046d4f5c0fd0d8de99e08e080590a5120d9d6319 (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#! /usr/bin/perl
#
# Perl script to process OALD machine-readable ASCII file
# into a GF lexicon
#
# Usage: ./asc2gf < ascii_0710-1.txt
#
# Bjorn Bringert 2008,
# based on asc2lex by
# Matthew Purver, 11/2001

# skip header section
while ( <STDIN> ) {
    last if /<\/TEIHEADER>/;
}

# read a line from stdin
while ( $line = <STDIN> ) {

    # remove SGML tags
    $line =~ s/<[^<>]+>//g;

    # split line into fields according to spec (line may be empty now)
    if ( $line =~ /^(.{23}).{23}(.{23}).{1}(.{58})$/ ) {

  	# trim white space
	for ( ( $word, $pos, $cat ) = ( $1, $2, $3 ) ) {
	    s/\s*$//;
	}

	# make word lower-case atomic string
	$word =~ s/\"/\\\"/g;   # " -> \"
	$word =~ tr/A-Z/a-z/;   # lower case

	# move diacritics to the following letter
	$word =~ s/~n/ñ/g;
	$word =~ s/<c/ç/g;
	$word =~ s/"a/ä/g;
	$word =~ s/"o/ö/g;
	$word =~ s/"u/ü/g;
	$word =~ s/\^a/â/g;
	$word =~ s/\^e/ê/g;
	$word =~ s/\^o/ô/g;
	$word =~ s/`a/à/g;
	$word =~ s/`e/è/g;
	$word =~ s/_e/é/g;

	$name = $word;
	$name =~ s/ /_/g; # space -> _
	$name =~ s/-/_/g;   # - -> _


	# get PoS & subcat info
	@pos = split( /,/, $pos );
	$cat =~ s/,/\',\'/g;
	( $cat = "\'$cat\'" ) unless ( $cat eq '' );

	# set up Prolog-style string & put into array
	foreach ( @pos ) {
	    ( $pcode, $infl, $freq )=split(//);

	    # for verbs, get inflected forms
	    if ( $pcode =~ /^[GHIJ]/ ) {
		$pos = 'verb';
		# if this is a root form, work out the inflected forms
		if ( $infl =~ /^\d/ ) {
		    if ( $infl == 0 ) {
			( $vbz = $word ) =~ s/$/s/;
			( $vbg = $word ) =~ s/$/ing/;
			( $vbd = $word ) =~ s/$/ed/;
		    }
		    elsif ( $infl == 1 ) {
			( $vbz = $word ) =~ s/$/es/;
			( $vbg = $word ) =~ s/$/ing/;
			( $vbd = $word ) =~ s/$/ed/;
		    }
		    elsif ( $infl == 2 ) {
			( $vbz = $word ) =~ s/e$/es/;
			( $vbg = $word ) =~ s/e$/ing/;
			( $vbd = $word ) =~ s/e$/ed/;
		    }
		    elsif ( $infl == 3 ) {
			( $vbz = $word ) =~ s/y$/ies/;
			( $vbg = $word ) =~ s/y$/ying/;
			( $vbd = $word ) =~ s/y$/ied/;
		    }
		    elsif ( $infl == 4 ) {
			( $vbz = $word ) =~ s/$/s/;
			( $vbg = $word ) =~ s/(\w)$/$1$1ing/;
			( $vbd = $word ) =~ s/(\w)$/$1$1ed/;
		    }
		    elsif ( $infl == 5 ) {
			# for irregulars, just mark as such for now, we'll guess later
			$vbz = 'IRREG';
			$vbg = 'IRREG';
			$vbd = 'IRREG';
		    }

		    $lin = "mkV \"$word\" \"$vbz\" \"$vbd\" \"$vbd\" \"$vbg\"";		    

		    if ($pcode eq 'G') {
		      $words{"${name}_VX"} = "mkVX ($lin)";
		    } 
		    if ($pcode eq 'I' || $pcode eq 'J') {
		      $words{"${name}_V"} = "$lin";
		    }
		    if ($pcode eq 'H' || $pcode eq 'J') {
		      $words{"${name}_V2"} = "mkV2 ($lin)";
		    }
		}
		# if this is an inflected form, save for guessing irregulars later
		elsif ( $infl =~ /^a/ ) {
		    push( @vbz, $word );
		}
		elsif ( $infl =~ /^b/ ) {
		    push( @vbg, $word );
		}
		elsif ( $infl =~ /^c/ ) {
		    push( @vbd, $word );
		}
		elsif ( $infl =~ /^d/ ) {
		    push( @vbn, $word );
		}
	    }
	    # for nouns, get plural form
	    elsif( $pcode =~ /^[KLMNY]/ ) {
		$pos = 'noun';
		$pcode =~ s/^K/count/;
		$pcode =~ s/^L/mass/;
		$pcode =~ s/^M/both/;
		$pcode =~ s/^N/proper/;
		if ( $pcode =~ /^Y/ ) {
		    $pcode = 'count' if $infl =~ /^[>\)\]]/;
		    $pcode = 'mass' if $infl =~ /^\}/;
		    $pcode = 'proper' if $infl =~ /^[:=~]/;
		}
		# if this is a singular form, work out plural form
		unless ( $infl =~ /^j/ ) {
		    $pl = '-';
		    if ( $infl == 6 ) {
			( $pl = $word ) =~ s/$/s/;
		    }
		    elsif ( $infl == 7 ) {
			( $pl = $word ) =~ s/$/es/;
		    }
		    elsif ( $infl == 8 ) {
			( $pl = $word ) =~ s/y$/ies/;
		    }
		    elsif ( $infl =~ /^[9k\]]/ ) {
			$pl = $word;
		    }
		    elsif ( $infl =~ /^i/ ) {
			# for irregulars, let's just make a guess and mark with '*'
			# this could be done better, as for verbs, but I can't be bothered now
			$pl = $word;
  			( $pl =~ s/^((wo)?m)an/$1en\*/ ) or
  			    ( $pl =~ s/man(-|$)/men$1\*/ ) or
  			      ( $pl =~ s/-in-law/s-in-law\*/ ) or
  			      ( $pl =~ s/um$/a\*/ ) or
  			      ( $pl =~ s/us$/i\*/ ) or
  			      ( $pl =~ s/a$/ae\*/ ) or
  			      ( $pl =~ s/on$/a\*/ ) or
  			      ( $pl =~ s/is$/es\*/ ) or
  			      ( $pl =~ s/o$/i\*/ ) or
  			      ( $pl =~ s/child$/children\*/ ) or
  			      ( $pl =~ s/oot$/eet\*/ ) or
  			      ( $pl =~ s/ooth$/eeth\*/ ) or
  			      ( $pl =~ s/([lm])ouse$/$1ice\*/ ) or
  			      ( $pl =~ s/f(e)?$/ves\*/ ) or
  			      ( $pl =~ s/[ei]x$/ices\*/ ) or
  			      ( $pl =~ s/eau$/eaux\*/ ) or
  			      ( $pl = 'IRREG' );
		    }
		    # if plural-only, swap root form & plural
		    elsif ( $infl =~ /^\)/ ) {
			$pl = $word;
			$word = '-';
		    }
		    ( $infl =~ s/^[:l]/per/ ) or ( $infl =~ s/^[mn]/loc/ ) or ( $infl = '_' );
		    $words{"${name}_N"} = "mkN \"$word\" \"$pl\"";
		}
	    }
	    # for adjectives, get comparative & superlative forms
	    elsif( $pcode =~ /^O/ ) {
		$pos = 'adj';
		# if this is root form, work out inflected forms
		unless ( $infl =~ /^[rs]/ ) {
		    if ( $infl =~ /^[Apqt]/ ) {
			$comp = $sup = '-';
		    }
		    elsif ( $infl =~ /^B/ ) {
			( $comp = $word ) =~ s/$/r/;
			( $sup = $word ) =~ s/$/st/;
		    }
		    elsif ( $infl =~ /^C/ ) {
			( $comp = $word ) =~ s/$/er/;
			( $sup = $word ) =~ s/$/est/;
		    }
		    elsif ( $infl =~ /^D/ ) {
			( $comp = $word ) =~ s/y$/ier/;
			( $sup = $word ) =~ s/y$/iest/;
		    }
		    elsif ( $infl =~ /^E/ ) {
			# for irregulars, let's just have a guess and mark with '*'
			# (there aren't very many of these)
			( $comp = $word ) =~ s/(\w)$/$1$1er\*/;
			( $sup = $word ) =~ s/(\w)$/$1$1est\*/;
		    }
		    $infl =~ s/^[ABCDE]/normal/;
		    $infl =~ s/^p/pred/;
		    $infl =~ s/^q/attr/;
		    $infl =~ s/^t/affix/;

		    $words{"${name}_A"} = "mkA \"$word\" \"$comp\"";
		}
	    }
	    # for adverbs, just add all info to @adv array
	    elsif( $pcode =~ /^P/ ) {
		$pos = 'adv';
		$infl =~ s/^[u\+]/normal/;
		$infl =~ s/^w/whrel/;
		$infl =~ s/^v/whq/;
		$words{"${name}_Adv"} = "mkAdv \"$word\"";
	    }
	    # for pronouns, work out some case/person info
	    elsif( $pcode =~ s/^Q/_/ ) {
		$pos = 'pron';
		$infl =~ s/^x/normal/;
		$infl =~ s/^y/whq/;
		$infl =~ s/^z/whrel/;
		$class = '_';
		# reflexive pronouns
		if ( ( $word =~ /self$/ ) or 
		     ( $word =~ /selves$/ ) ) {
		    $pcode = 'acc';
		}
		# accusative personal pronouns
		if ( ( $word =~ /^him/ ) or
		     ( $word =~ /^her/ ) or
		     ( $word =~ /^them/ ) or
		     ( $word eq 'us' ) or
		     ( $word eq 'thee' ) or
		     ( $word eq 'me' ) ) {
		    $pcode = 'acc';
		    $class = 'per';
		}
		# nominative personal pronouns
		if ( ( $word eq 'he' ) or
		     ( $word eq 'she' ) or
		     ( $word eq 'they' ) or
		     ( $word eq 'we' ) or
		     ( $word eq 'thou' ) or
		     ( $word eq 'i' ) ) {
		    $pcode = 'nom';
		    $class = 'per';
		}
		# other personal pronouns
		if ( ( $word =~ /.+one/ ) or
		     ( $word =~ /one.+/ ) or
		     ( $word =~ /body/ ) or
		     ( $word =~ /^you/ ) or
		     ( $word =~ /^who/ ) ) {
		    $class = 'per';
		}
		# non-personal pronouns
		if ( $word =~ /thing/ ) {
		    $class = 'nper';
		}
		# otherwise case/person info will be '_' (anon variable)
		# add full spec to @pron array
		#push( @pron, "$pos( \'$word\', $pcode, $infl, $class ).\n" );
	    }
	    # for determiners, leave anon variable as placeholder for semantics
	    elsif( $pcode =~ /^[RS]/ ) {
		$pos = 'det';
		$pcode =~ s/^R/def/;
		$pcode =~ s/^S/indef/;
		#push( @det, "$pos( \'$word\', $pcode, _ ).\n" );
	    }
	    # for prepositions - nothing to say
	    elsif( $pcode =~ s/^T/prep/ ) {
		$pos = 'prep';
		#push( @prep, "$pos( \'$word\', $pcode ).\n" );
	    }
	    # for conjunctions - nothing to say
	    elsif( $pcode =~ s/^V/conj/ ) {
		$pos = 'conj';
		#push( @conj, "$pos( \'$word\', $pcode ).\n" );
	    }
	    # for miscellaneous, leave '-' as placeholder for illocutionary info
	    elsif( $pcode =~ /^[UWXZ]/ ) {
		$pos = 'misc';
		#push( @prefix, "$pos( \'$word\', $pcode, '-' ).\n" ) if ( $pcode =~ s/^U/prefix/ );
		#push( @interj, "$pos( \'$word\', $pcode, '-' ).\n" ) if ( $pcode =~ s/^W/interj/ );
		#push( @partcl, "$pos( \'$word\', $pcode, '-' ).\n" ) if ( $pcode =~ s/^X/partcl/ );
		#push( @unknown, "$pos( \'$word\', $pcode, '-' ).\n" ) if ( $pcode =~ s/^Z/unknown/ );
	    }
	}
    }
}

$absfile = "Oald.gf";
$cncfile = "OaldEng.gf";

open (ABS, '>', $absfile);
open (CNC, '>', $cncfile);



# print a nice comment at the top
$header = "-- GF lexicon, from OALD machine-readable dictionary\n" 
        . "-- Produced by asc2gf, based on asc2lex, Matthew Purver 11/2001\n\n";
print ABS $header;
print CNC $header;

print ABS "abstract Oald = {\n";
print CNC "concrete OaldEng of Oald = {\n";

foreach $name (sort (keys %words)) {
  ($cat = $name) =~ s/.*_([A-Z\d])$/$1/;
  $lin = $words{$name};
  print ABS "fun $name : $cat;\n";
  print CNC "lin $name = $lin;\n";
  print "$name\n";
}

print ABS "}";
print CNC "}";

close(ABS);
close(CNC);

print "\nWrote lexicon to $absfile and $cncfile\n";

exit 0;












# now have a guess at irregular verb forms (marking the best guess with '*')
foreach $verb ( @verb ) {
    if ( $verb =~ /verb\( \'([^\']+)\', \'IRREG/ ) {
	$word = $1;
	$vbz = findbest( $word, @vbz );
	$vbg = findbest( $word, @vbg );
	$vbd = findbest( $word, @vbd );
	$vbn = findbest( $word, @vbn );
	$verb =~ s/($word\', \')IRREG(\', \')IRREG(\', \')IRREG(\', \')IRREG/\*$1$vbz$2$vbg$3$vbd$4$vbn/;
    }
}

# now print everything out (so we can group PoSs together)
print @verb, "\n", @noun, "\n", @adj, "\n", @adv;
print "\n", @pron, "\n", @det, "\n", @prep, "\n", @conj;
print "\n", @prefix, "\n", @interj, "\n", @partcl, "\n", @unknown;


# find closest string match
# similarity measure is just the length of identical prefix
# prefer shorter strings in the case of equal similarity
sub findbest 
{
    my ( $word, @array ) = @_;

    $bestlen = 0;
    foreach $test ( @array ) {
	if ( ( substr( $word, 0, $bestlen-1 ) eq substr( $test, 0, $bestlen-1 ) ) &&
	     ( length( $test ) < length( $best ) ) ) {
	    $best = $test;
	}
	while ( ( substr( $word, 0, $bestlen ) eq substr( $test, 0, $bestlen ) ) &&
		( $bestlen <= length( $test ) ) ) {
	    $bestlen++;
	    $best = $test;
	}
    }
    return $best;
}