source: app/src/dgp/dgc @ aa9f1bc

help
Last change on this file since aa9f1bc was 0214596, checked in by pawelk <pawelk@…>, 16 years ago

Dodalismy do pakietu utt komponent dgp (brak configow i innych bajerow).

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@24 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

  • Property mode set to 100755
File size: 3.5 KB
Line 
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;
10use Getopt::Long;
11use Data::Dumper;
12
13use attr;
14#use File::HomeDir;
15
16my $help=0;
17my $catfile=0;
18my $dicfile=0;
19my $gramfile=0;
20
21my $ncat=0;
22my $nrole=0;
23my $nsgl=0;
24my $nleft=0;
25my $nright=0;
26my $nreq=0;
27my $nlink=0;
28
29GetOptions("help|h" => \$help,
30           "catfile|c=s" => \$catfile,
31           "dicfile|d=s" => \$dicfile,
32           "gramfile|g=s" => \$gramfile);
33
34if($help)
35{
36    print <<'END'
37Usage: dgpcompile [OPTIONS]
38
39Options:
40   --cats -c filename           List of syntactic categories.
41   --dic  -d filename           Dictionary.
42   --help -h                    Help.
43END
44;
45    exit 0;
46}
47
48die("At least one of --cats and --dic must be given.\n") if !$catfile && !$dicfile;
49
50my %cats;
51my %roles;
52my %agr;
53my %gov;
54
55loadcats($catfile) if $catfile;
56extractcats($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
64while(<>)
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
126sub 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
165printf STDERR "%6d CAT   statements\n", 0+keys(%cats);
166printf STDERR "%6d ROLE  statements\n", 0+keys(%roles);
167printf STDERR "%6d SGL   statements\n", $nsgl;
168printf STDERR "%6d REQ   statements\n", $nreq;
169printf STDERR "%6d LEFT  statements\n", $nleft;
170printf STDERR "%6d RIGHT statements\n", $nright;
171printf STDERR "%6d LINK  statements\n", $nlink;
172
173
174sub 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
193sub 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}
Note: See TracBrowser for help on using the repository browser.