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