[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 | |
---|
[19dfa5c] | 8 | # wymaga niejawnie programu canonize!!!! |
---|
[0214596] | 9 | #use lib "ENV{HOME}/.utt/lib/perl"; |
---|
[19dfa5c] | 10 | |
---|
| 11 | use strict; |
---|
[0214596] | 12 | use Getopt::Long; |
---|
| 13 | use Data::Dumper; |
---|
| 14 | |
---|
| 15 | use attr; |
---|
[19dfa5c] | 16 | use File::HomeDir; |
---|
| 17 | |
---|
| 18 | my $systemconfigfile='/usr/local/etc/utt/dgc.conf'; |
---|
| 19 | my $userconfigfile=home()."/.utt/dgc.conf"; |
---|
| 20 | |
---|
| 21 | Getopt::Long::Configure('no_ignore_case_always'); |
---|
[0214596] | 22 | |
---|
| 23 | my $help=0; |
---|
| 24 | my $catfile=0; |
---|
| 25 | my $dicfile=0; |
---|
| 26 | my $gramfile=0; |
---|
[19dfa5c] | 27 | my $outputfile=0; |
---|
[0214596] | 28 | |
---|
[19dfa5c] | 29 | #read configuration files########################### |
---|
| 30 | my $file; |
---|
| 31 | foreach $file ($systemconfigfile, $userconfigfile){ |
---|
| 32 | if(open(CONFIG, $file)){ |
---|
| 33 | while (<CONFIG>) { |
---|
| 34 | chomp; |
---|
| 35 | s/#.*//; |
---|
| 36 | s/^\s+//; |
---|
| 37 | s/\s+$//; |
---|
| 38 | next unless length; |
---|
| 39 | my ($name, $value) = split(/\s*=\s*/, $_, 2); |
---|
| 40 | if(($name eq "catfile")or($name eq "c")){ |
---|
| 41 | $catfile=$value; |
---|
| 42 | } |
---|
| 43 | elsif(($name eq "dicfile")or($name eq "d")){ |
---|
| 44 | $dicfile=$value; |
---|
| 45 | } |
---|
| 46 | elsif(($name eq "gramfile")or($name eq "g")){ |
---|
| 47 | $gramfile=$value; |
---|
| 48 | } |
---|
| 49 | elsif(($name eq "outputfile")or($name eq "o")){ |
---|
| 50 | $outputfile=$value; |
---|
| 51 | } |
---|
| 52 | elsif(($name eq "help")or($name eq "h")){ |
---|
| 53 | $help=1; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | close CONFIG; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | ######################################################### |
---|
[0214596] | 61 | |
---|
| 62 | GetOptions("help|h" => \$help, |
---|
| 63 | "catfile|c=s" => \$catfile, |
---|
| 64 | "dicfile|d=s" => \$dicfile, |
---|
[19dfa5c] | 65 | "gramfile|g=s" => \$gramfile, |
---|
| 66 | "outputfile|o=s" => \$outputfile); |
---|
[0214596] | 67 | |
---|
| 68 | if($help) |
---|
| 69 | { |
---|
| 70 | print <<'END' |
---|
[19dfa5c] | 71 | Usage: dgc [OPTIONS] |
---|
[0214596] | 72 | |
---|
| 73 | Options: |
---|
[19dfa5c] | 74 | --catfile -c filename List of syntactic categories. |
---|
| 75 | --dicfile -d filename Dictionary. |
---|
| 76 | --gramfile -g filename List of grammar rules. |
---|
| 77 | --outputfile -o filename Output filename. |
---|
[0214596] | 78 | --help -h Help. |
---|
| 79 | END |
---|
| 80 | ; |
---|
| 81 | exit 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | die("At least one of --cats and --dic must be given.\n") if !$catfile && !$dicfile; |
---|
| 85 | |
---|
[19dfa5c] | 86 | my $ncat=0; |
---|
| 87 | my $nrole=0; |
---|
| 88 | my $nsgl=0; |
---|
| 89 | my $nleft=0; |
---|
| 90 | my $nright=0; |
---|
| 91 | my $nreq=0; |
---|
| 92 | my $nlink=0; |
---|
| 93 | |
---|
[0214596] | 94 | my %cats; |
---|
| 95 | my %roles; |
---|
| 96 | my %agr; |
---|
| 97 | my %gov; |
---|
| 98 | |
---|
[19dfa5c] | 99 | if(!$outputfile) { |
---|
| 100 | *OUTPUT = *STDOUT; |
---|
| 101 | } |
---|
| 102 | elsif($outputfile eq "-") { |
---|
| 103 | *OUTPUT = *STDOUT; |
---|
| 104 | } |
---|
| 105 | else { |
---|
| 106 | open(OUTPUT, ">$outputfile") or die("Can't open output file: $outputfile!"); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
[0214596] | 111 | loadcats($catfile) if $catfile; |
---|
| 112 | extractcats($dicfile) if $dicfile; |
---|
| 113 | |
---|
| 114 | |
---|
[19dfa5c] | 115 | my $cats_re = qr/(?:$attr::cat_re\s*(?:,\s*$attr::cat_re)*)/; |
---|
[0214596] | 116 | |
---|
| 117 | # class parse_class: |
---|
| 118 | # /$attr::cat_re/g; |
---|
| 119 | |
---|
[19dfa5c] | 120 | |
---|
| 121 | if(!$gramfile) { |
---|
| 122 | *INPUT = *STDIN; |
---|
| 123 | } |
---|
| 124 | elsif($gramfile eq "-"){ |
---|
| 125 | *INPUT = *STDIN; |
---|
| 126 | } |
---|
| 127 | else { |
---|
| 128 | open(INPUT, $gramfile) or die("Unable to open: $gramfile!"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | while(<INPUT>) |
---|
[0214596] | 132 | { |
---|
| 133 | if(/^\s*AGR\s+(\S+)\s+(\S+)\s*$/) |
---|
| 134 | { |
---|
| 135 | push @{$agr{$1}}, $2; |
---|
| 136 | } |
---|
| 137 | elsif(/^\s*GOV\s+(\S+)\s+(\S+)\s*$/) |
---|
| 138 | { |
---|
| 139 | push @{$gov{$1}}, attr::parse($2); |
---|
| 140 | } |
---|
| 141 | elsif(/^\s*ROLE\s+\S+\s*$/) |
---|
| 142 | { |
---|
| 143 | $roles{$_}=1; |
---|
[19dfa5c] | 144 | print OUTPUT; |
---|
[0214596] | 145 | } |
---|
| 146 | elsif(/^\s*SGL\s+\S+\s*$/) |
---|
| 147 | { |
---|
| 148 | ++$nsgl; |
---|
[19dfa5c] | 149 | print OUTPUT; |
---|
[0214596] | 150 | } |
---|
| 151 | elsif(/^\s*REQ\s+(\S+)\s+(\S+)\s*$/) |
---|
| 152 | { |
---|
[19dfa5c] | 153 | print OUTPUT "#$_"; |
---|
[0214596] | 154 | my $cat = attr::parse $1; |
---|
| 155 | for my $atomcat (keys %cats) |
---|
| 156 | { |
---|
| 157 | if(attr::match @$cat, @{$cats{$atomcat}}) |
---|
| 158 | { |
---|
[19dfa5c] | 159 | print OUTPUT "REQ ".$atomcat." $2\n"; |
---|
[0214596] | 160 | ++$nreq; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | elsif(/^\s*LEFT\s+\S+\s*$/) |
---|
| 165 | { |
---|
| 166 | ++$nleft; |
---|
[19dfa5c] | 167 | print OUTPUT; |
---|
[0214596] | 168 | } |
---|
| 169 | elsif(/^\s*RIGHT\s+\S+\s*$/) |
---|
| 170 | { |
---|
| 171 | ++$nright; |
---|
[19dfa5c] | 172 | print OUTPUT; |
---|
[0214596] | 173 | } |
---|
[19dfa5c] | 174 | elsif(my ($hs,$ds,$r) = /^\s*LINK\s+($cats_re)\s+($cats_re)\s+(\S+)\s*$/) |
---|
[0214596] | 175 | { |
---|
[19dfa5c] | 176 | print OUTPUT "#$_"; |
---|
| 177 | for my $h ($hs =~ /$attr::cat_re/g) |
---|
[0214596] | 178 | { |
---|
[19dfa5c] | 179 | for my $d ($ds =~ /$attr::cat_re/g) |
---|
[0214596] | 180 | { |
---|
| 181 | addlinks($h,$d,$r); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | else |
---|
| 187 | { |
---|
[19dfa5c] | 188 | print OUTPUT; |
---|
[0214596] | 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | sub addlinks |
---|
| 194 | { |
---|
[19dfa5c] | 195 | my ($h,$d,$r) = @_; |
---|
[0214596] | 196 | |
---|
[19dfa5c] | 197 | for my $a (@{$agr{$r}}) { print OUTPUT "#AGR $r $a\n"; } |
---|
| 198 | for my $c (@{$gov{$r}}) { print OUTPUT "#GOV $r ".attr::unparse(@$c)."\n"; } |
---|
[0214596] | 199 | my $head = attr::parse $h; |
---|
| 200 | my $dep = attr::parse $d; |
---|
| 201 | |
---|
| 202 | for my $atomhead (keys %cats) |
---|
| 203 | { |
---|
| 204 | if(attr::match @$head, @{$cats{$atomhead}}) |
---|
| 205 | { |
---|
| 206 | DEP: |
---|
| 207 | for my $atomdep (keys %cats) |
---|
| 208 | { |
---|
| 209 | next DEP if ! attr::match @$dep, @{$cats{$atomdep}}; |
---|
| 210 | |
---|
| 211 | for my $a (@{$agr{$r}}) |
---|
| 212 | { |
---|
| 213 | next DEP if ! attr::agree(@{$cats{$atomhead}},@{$cats{$atomdep}},$a); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | for my $c (@{$gov{$r}}) |
---|
| 217 | { |
---|
| 218 | next DEP if ! attr::match(@$c,@{$cats{$atomdep}}); |
---|
| 219 | } |
---|
| 220 | |
---|
[19dfa5c] | 221 | print OUTPUT "LINK "; |
---|
| 222 | print OUTPUT $atomhead." "; |
---|
| 223 | print OUTPUT $atomdep." $r\n"; |
---|
[0214596] | 224 | ++$nlink; |
---|
| 225 | |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | printf STDERR "%6d CAT statements\n", 0+keys(%cats); |
---|
| 233 | printf STDERR "%6d ROLE statements\n", 0+keys(%roles); |
---|
| 234 | printf STDERR "%6d SGL statements\n", $nsgl; |
---|
| 235 | printf STDERR "%6d REQ statements\n", $nreq; |
---|
| 236 | printf STDERR "%6d LEFT statements\n", $nleft; |
---|
| 237 | printf STDERR "%6d RIGHT statements\n", $nright; |
---|
| 238 | printf STDERR "%6d LINK statements\n", $nlink; |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | sub extractcats |
---|
| 242 | { |
---|
| 243 | my $file = shift; |
---|
| 244 | open DICFILE, "canonize $file |"; |
---|
| 245 | while(<DICFILE>) |
---|
| 246 | { |
---|
| 247 | while(/,([^[:space:];]+)/g) |
---|
| 248 | { |
---|
[19dfa5c] | 249 | my $cat=$1; |
---|
[0214596] | 250 | next if !$cat || exists $cats{$cat}; |
---|
| 251 | $ncat++; |
---|
[19dfa5c] | 252 | print OUTPUT "CAT $1\n"; |
---|
[0214596] | 253 | $cats{$cat}=attr::parse($cat); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | close DICFILE; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | |
---|
| 260 | sub loadcats |
---|
| 261 | { |
---|
| 262 | my $file = shift; |
---|
| 263 | open CATFILE, "canonize $file |"; |
---|
| 264 | while(<CATFILE>) |
---|
| 265 | { |
---|
| 266 | tr/ \t\n//d; |
---|
| 267 | next if !$_ || exists $cats{$_}; |
---|
[19dfa5c] | 268 | print OUTPUT "CAT $_\n"; |
---|
[0214596] | 269 | ++$ncat; |
---|
| 270 | $cats{$_}=attr::parse($_); |
---|
| 271 | } |
---|
| 272 | close CATFILE; |
---|
| 273 | } |
---|
[19dfa5c] | 274 | |
---|