]> git.donarmstrong.com Git - deb_pkgs/scowl.git/blob - src/add-affixes
Merge tag 'upstream/2015.08.24'
[deb_pkgs/scowl.git] / src / add-affixes
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings;
5 #no warnings 'uninitialized';
6
7 # Usage: add-affixes [<inc_level>] [<flags>]
8
9 my $inc_level = 0;
10 if ($ARGV[0] =~ /^[\d.]+$/) {
11   $inc_level = $ARGV[0];
12   shift @ARGV;
13 }
14
15 #print STDERR "Include Level: $inc_level\n";
16
17 my $use_all = 0;
18 my $just_possessive = 0;
19 my $no_possessive = 0;
20 foreach (@ARGV) {
21   if ($_ eq 'use-all') {$use_all = 2}
22   elsif ($_ eq 'use-some') {$use_all = 1}
23   elsif ($_ eq 'just-possessive') {$just_possessive = 1}
24   elsif ($_ eq 'no-possessive') {$no_possessive = 1}
25   else {die "ERROR: Invalid flag $_\n";}
26 }
27
28 open F, "r/alt12dicts/2of12id.txt" or die;
29
30 my %lookup;
31 my %remove;
32 my %possessive;
33 # possessive_cross = additional forms that should be looked up in the
34 #   possessive hash
35 my %possessive_cross;
36
37 while (<F>) {
38   s/\r?\n$// or die;
39   # (flags, base word, part of speach, infl forms)
40   my ($d,$w,$p,$a) = /^([-@]?)(\w+) (.).*: ?(.*)$/ or die;
41   $possessive{$w} = "$w\'s\n" if $p eq 'N' && ($d eq '' || $use_all);
42   next if $use_all;
43   my @a = $a =~ /([-~@!\w]+)/g;
44   @a = map {"$d$_"} @a if ($d);
45   my (@a0,@a1);
46   foreach (@a) {if (s/^[~!-]//) {push @a0, $_} else {push @a1, $_}}
47   $remove{"$w:$p"} = 1 unless @a1;
48   foreach (@a0) {$remove{"$w:$p:$_"} = 1}
49 }
50
51 # Maybe using AGID isn't a good idea here, many of the entries in AGID
52 # are unchecked, for example "associationism" may be an uncountable
53 # noun, but since the base entry is not in 2of12id it is not flagged
54 # that way, thus the plural "associationisms" gets included.  However,
55 # AGID still needs to be used for uppercase words since they are not
56 # in 2of12id.  For now I won't worry about it since it primary effects
57 # level 70 of SCOWL.
58
59 open F, "r/infl/infl.txt" or die;
60
61 while (<F>) {
62   # (base word, part of speach, guess flag, infl forms)
63   my ($w,$p,$q,$a) = /(\S+) (.)(.*): (.+)/ or die;
64   # Add possive form if
65   #  AGID things it is a noun and "use-some" or Uppercase 
66   #    (since 2of12id doesn't include uppercase)
67   #  AGIG is guessing it is a noun and "use-all"
68   my $add_possessive = $p eq 'N' && (($q eq '' && ($use_all || $w =~ /^[A-Z]/)) 
69                                      || $use_all >= 2);
70   $possessive{$w} = "$w\'s\n" if $add_possessive;
71   next if $remove{"$w:$p"};
72   next unless $q eq '' || $use_all >= 2;
73   my @a = split /, | \| /, $a;
74   @a = grep {my ($word,$tags,$level)
75                  = /^([A-Za-z\']+)([~<!?]*)(| [\d.]+)(| {\S+})$/ or die $_;
76              $_ = $word;
77              $tags !~ /~|\?|!</ && $level <= $inc_level} @a;
78   @a = grep {not $remove{"$w:$p:$_"}} @a;
79   next unless @a;
80   $lookup{$w} .= join("\n",@a)."\n";
81
82   # For irregular nouns that have plurals that do not end in s
83   # then add the possessive form of the plural as well
84   next unless $add_possessive;
85   foreach (@a) {
86     next if /s$/;
87     $possessive{$_} .= "$_\'s\n";
88     push @{$possessive_cross{$w}}, $_
89   }
90 }
91
92 unless ($no_possessive) {
93
94   open F, "r/special/not-possessive" or die;
95
96   while (<F>) {
97     chop;
98     delete $possessive{$_};
99   }
100
101   open F, "working/possessive-also.lst" or die;
102
103   while (<F>) {
104     chop;
105     $possessive{$_} = "$_\'s\n";
106   }
107 }
108
109 while (<STDIN>) {
110   print;
111   chop;
112   my $w = $_;
113   print $lookup{$w} unless $just_possessive;
114   print $possessive{$w} unless $no_possessive;
115   unless ($just_possessive || $no_possessive) {
116     foreach (@{$possessive_cross{$w}}) {
117       print $possessive{$_}
118     }
119   }
120 }