[0214596] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | #package: UAM Text Tools |
---|
| 4 | #component: dgc (dg compiler) |
---|
| 5 | #version: 1.0 |
---|
| 6 | #author: Tomasz Obrebski |
---|
| 7 | |
---|
| 8 | #use lib "ENV{HOME}/.utt/lib/perl"; |
---|
| 9 | #use strict; |
---|
| 10 | use Getopt::Long; |
---|
| 11 | use Data::Dumper; |
---|
| 12 | |
---|
| 13 | use attr; |
---|
| 14 | #use File::HomeDir; |
---|
| 15 | |
---|
| 16 | my $help=0; |
---|
| 17 | my $catfile=0; |
---|
| 18 | my $dicfile=0; |
---|
| 19 | my $gramfile=0; |
---|
| 20 | |
---|
| 21 | my $ncat=0; |
---|
| 22 | my $nrole=0; |
---|
| 23 | my $nsgl=0; |
---|
| 24 | my $nleft=0; |
---|
| 25 | my $nright=0; |
---|
| 26 | my $nreq=0; |
---|
| 27 | my $nlink=0; |
---|
| 28 | |
---|
| 29 | GetOptions("help|h" => \$help, |
---|
| 30 | "catfile|c=s" => \$catfile, |
---|
| 31 | "dicfile|d=s" => \$dicfile, |
---|
| 32 | "gramfile|g=s" => \$gramfile); |
---|
| 33 | |
---|
| 34 | if($help) |
---|
| 35 | { |
---|
| 36 | print <<'END' |
---|
| 37 | Usage: dgpcompile [OPTIONS] |
---|
| 38 | |
---|
| 39 | Options: |
---|
| 40 | --cats -c filename List of syntactic categories. |
---|
| 41 | --dic -d filename Dictionary. |
---|
| 42 | --help -h Help. |
---|
| 43 | END |
---|
| 44 | ; |
---|
| 45 | exit 0; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | die("At least one of --cats and --dic must be given.\n") if !$catfile && !$dicfile; |
---|
| 49 | |
---|
| 50 | my %cats; |
---|
| 51 | my %roles; |
---|
| 52 | my %agr; |
---|
| 53 | my %gov; |
---|
| 54 | |
---|
| 55 | loadcats($catfile) if $catfile; |
---|
| 56 | extractcats($dicfile) if $dicfile; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | $cats_re = qr/(?:$attr::cat_re\s*(?:,\s*$attr::cat_re)*)/; |
---|
| 60 | |
---|
| 61 | # class parse_class: |
---|
| 62 | # /$attr::cat_re/g; |
---|
| 63 | |
---|
| 64 | while(<>) |
---|
| 65 | { |
---|
| 66 | if(/^\s*AGR\s+(\S+)\s+(\S+)\s*$/) |
---|
| 67 | { |
---|
| 68 | push @{$agr{$1}}, $2; |
---|
| 69 | } |
---|
| 70 | elsif(/^\s*GOV\s+(\S+)\s+(\S+)\s*$/) |
---|
| 71 | { |
---|
| 72 | push @{$gov{$1}}, attr::parse($2); |
---|
| 73 | } |
---|
| 74 | elsif(/^\s*ROLE\s+\S+\s*$/) |
---|
| 75 | { |
---|
| 76 | $roles{$_}=1; |
---|
| 77 | print; |
---|
| 78 | } |
---|
| 79 | elsif(/^\s*SGL\s+\S+\s*$/) |
---|
| 80 | { |
---|
| 81 | ++$nsgl; |
---|
| 82 | print; |
---|
| 83 | } |
---|
| 84 | elsif(/^\s*REQ\s+(\S+)\s+(\S+)\s*$/) |
---|
| 85 | { |
---|
| 86 | print "#$_"; |
---|
| 87 | my $cat = attr::parse $1; |
---|
| 88 | for my $atomcat (keys %cats) |
---|
| 89 | { |
---|
| 90 | if(attr::match @$cat, @{$cats{$atomcat}}) |
---|
| 91 | { |
---|
| 92 | print "REQ ".$atomcat." $2\n"; |
---|
| 93 | ++$nreq; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | elsif(/^\s*LEFT\s+\S+\s*$/) |
---|
| 98 | { |
---|
| 99 | ++$nleft; |
---|
| 100 | print; |
---|
| 101 | } |
---|
| 102 | elsif(/^\s*RIGHT\s+\S+\s*$/) |
---|
| 103 | { |
---|
| 104 | ++$nright; |
---|
| 105 | print; |
---|
| 106 | } |
---|
| 107 | elsif(($hs,$ds,$r) = /^\s*LINK\s+($cats_re)\s+($cats_re)\s+(\S+)\s*$/) |
---|
| 108 | { |
---|
| 109 | print "#$_"; |
---|
| 110 | for $h ($hs =~ /$attr::cat_re/g) |
---|
| 111 | { |
---|
| 112 | for $d ($ds =~ /$attr::cat_re/g) |
---|
| 113 | { |
---|
| 114 | addlinks($h,$d,$r); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | print; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | sub addlinks |
---|
| 127 | { |
---|
| 128 | ($h,$d,$r) = @_; |
---|
| 129 | |
---|
| 130 | for my $a (@{$agr{$r}}) { print "#AGR $r $a\n"; } |
---|
| 131 | for my $c (@{$gov{$r}}) { print "#GOV $r ".attr::unparse(@$c)."\n"; } |
---|
| 132 | my $head = attr::parse $h; |
---|
| 133 | my $dep = attr::parse $d; |
---|
| 134 | |
---|
| 135 | for my $atomhead (keys %cats) |
---|
| 136 | { |
---|
| 137 | if(attr::match @$head, @{$cats{$atomhead}}) |
---|
| 138 | { |
---|
| 139 | DEP: |
---|
| 140 | for my $atomdep (keys %cats) |
---|
| 141 | { |
---|
| 142 | next DEP if ! attr::match @$dep, @{$cats{$atomdep}}; |
---|
| 143 | |
---|
| 144 | for my $a (@{$agr{$r}}) |
---|
| 145 | { |
---|
| 146 | next DEP if ! attr::agree(@{$cats{$atomhead}},@{$cats{$atomdep}},$a); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | for my $c (@{$gov{$r}}) |
---|
| 150 | { |
---|
| 151 | next DEP if ! attr::match(@$c,@{$cats{$atomdep}}); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | print "LINK "; |
---|
| 155 | print $atomhead." "; |
---|
| 156 | print $atomdep." $r\n"; |
---|
| 157 | ++$nlink; |
---|
| 158 | |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | printf STDERR "%6d CAT statements\n", 0+keys(%cats); |
---|
| 166 | printf STDERR "%6d ROLE statements\n", 0+keys(%roles); |
---|
| 167 | printf STDERR "%6d SGL statements\n", $nsgl; |
---|
| 168 | printf STDERR "%6d REQ statements\n", $nreq; |
---|
| 169 | printf STDERR "%6d LEFT statements\n", $nleft; |
---|
| 170 | printf STDERR "%6d RIGHT statements\n", $nright; |
---|
| 171 | printf STDERR "%6d LINK statements\n", $nlink; |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | sub extractcats |
---|
| 175 | { |
---|
| 176 | my $file = shift; |
---|
| 177 | open DICFILE, "canonize $file |"; |
---|
| 178 | while(<DICFILE>) |
---|
| 179 | { |
---|
| 180 | while(/,([^[:space:];]+)/g) |
---|
| 181 | { |
---|
| 182 | $cat=$1; |
---|
| 183 | next if !$cat || exists $cats{$cat}; |
---|
| 184 | $ncat++; |
---|
| 185 | print "CAT $1\n"; |
---|
| 186 | $cats{$cat}=attr::parse($cat); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | close DICFILE; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | sub loadcats |
---|
| 194 | { |
---|
| 195 | my $file = shift; |
---|
| 196 | open CATFILE, "canonize $file |"; |
---|
| 197 | while(<CATFILE>) |
---|
| 198 | { |
---|
| 199 | tr/ \t\n//d; |
---|
| 200 | next if !$_ || exists $cats{$_}; |
---|
| 201 | print "CAT $_\n"; |
---|
| 202 | ++$ncat; |
---|
| 203 | $cats{$_}=attr::parse($_); |
---|
| 204 | } |
---|
| 205 | close CATFILE; |
---|
| 206 | } |
---|