source: src/dgc.rb/dgc.rb @ f924e4b

Last change on this file since f924e4b was e7de6cc, checked in by Tomasz Obrebski <to@…>, 14 years ago

new version of dgp
added dgc, tre and compdic components
compiledic renamed to compdic_utf8
./configure updated

  • Property mode set to 100644
File size: 6.2 KB
Line 
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
8use lib "/usr/local/lib/utt";
9use lib "$ENV{'HOME'}/.local/lib/utt";
10
11use strict;
12use Getopt::Long;
13use Data::Dumper;
14use attr;
15use File::HomeDir;
16
17my $systemconfigfile='/usr/local/etc/utt/dgc.conf';
18my $userconfigfile=home()."/.utt/dgc.conf";
19
20Getopt::Long::Configure('no_ignore_case_always');
21
22my $help=0;
23my $catfile=0;
24my $dicfile=0;
25my $gramfile=0;
26my $outputfile=0;
27
28#read configuration files###########################
29my $file;
30foreach $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
61GetOptions("help|h" => \$help,
62           "categories|c=s" => \$catfile,
63           "dictionary|d=s" => \$dicfile,
64           "grammar|g=s" => \$gramfile,
65           "outputfile|o=s" => \$outputfile);
66
67my $homedir = $ENV{'HOME'};
68$catfile =~ s/~/$homedir/;
69$dicfile =~ s/~/$homedir/;
70$gramfile =~ s/~/$homedir/;
71$outputfile =~ s/~/$homedir/;
72
73
74if($help)
75{
76    print <<'END'
77Usage: dgc [OPTIONS]
78
79Options:
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.
85END
86;
87    exit 0;
88}
89
90die("At least one of --cats and --dic must be given.\n") if !$catfile && !$dicfile;
91
92my $ncat=0;
93my $nrole=0;
94my $nsgl=0;
95my $nleft=0;
96my $nright=0;
97my $nreq=0;
98my $nlink=0;
99my $nflag=0;
100my $nlong=0;
101my $nclass=0;
102
103my %cats;
104my %classes; 
105my %roles;
106my %agr;
107my %gov;
108
109if(!$outputfile) {
110        *OUTPUT = *STDOUT;
111}
112elsif($outputfile eq "-") {
113    *OUTPUT = *STDOUT;
114}
115else {
116        open(OUTPUT, ">$outputfile") or die("Can't open output file: $outputfile!");
117}
118
119
120loadcats($catfile) if $catfile;
121extractcats($dicfile) if $dicfile;
122
123
124my $cats_re = qr/(?:$attr::cat_re\s*(?:,\s*$attr::cat_re)*)/;
125
126my $class_re = qr/(?:\@\w+)/;
127
128# class parse_class:
129# /$attr::cat_re/g;
130
131
132if(!$gramfile) { 
133        *INPUT = *STDIN;
134}
135elsif($gramfile eq "-"){
136    *INPUT = *STDIN;
137}
138else {
139        open(INPUT, $gramfile) or die("Unable to open: $gramfile!");
140}
141
142while(<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
236sub 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
275printf STDERR "%6d CAT   statements\n", 0+keys(%cats);
276printf STDERR "%6d ROLE  statements\n", 0+keys(%roles);
277printf STDERR "%6d SGL   statements\n", $nsgl;
278printf STDERR "%6d REQ   statements\n", $nreq;
279printf STDERR "%6d LEFT  statements\n", $nleft;
280printf STDERR "%6d RIGHT statements\n", $nright;
281printf STDERR "%6d LINK  statements\n", $nlink;
282printf STDERR "%6d CLASS statements\n", $nclass;
283printf STDERR "%6d FLAG  statements\n", $nflag;
284
285
286sub 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
305sub 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
Note: See TracBrowser for help on using the repository browser.