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 | # wymaga niejawnie programu canonize!!!! |
---|
9 | #use lib "ENV{HOME}/.utt/lib/perl"; |
---|
10 | |
---|
11 | use strict; |
---|
12 | use Getopt::Long; |
---|
13 | use Data::Dumper; |
---|
14 | |
---|
15 | use attr; |
---|
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'); |
---|
22 | |
---|
23 | my $help=0; |
---|
24 | my $catfile=0; |
---|
25 | my $dicfile=0; |
---|
26 | my $gramfile=0; |
---|
27 | my $outputfile=0; |
---|
28 | |
---|
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 | ######################################################### |
---|
61 | |
---|
62 | GetOptions("help|h" => \$help, |
---|
63 | "catfile|c=s" => \$catfile, |
---|
64 | "dicfile|d=s" => \$dicfile, |
---|
65 | "gramfile|g=s" => \$gramfile, |
---|
66 | "outputfile|o=s" => \$outputfile); |
---|
67 | |
---|
68 | if($help) |
---|
69 | { |
---|
70 | print <<'END' |
---|
71 | Usage: dgc [OPTIONS] |
---|
72 | |
---|
73 | Options: |
---|
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. |
---|
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 | |
---|
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 | |
---|
94 | my %cats; |
---|
95 | my %roles; |
---|
96 | my %agr; |
---|
97 | my %gov; |
---|
98 | |
---|
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 | |
---|
111 | loadcats($catfile) if $catfile; |
---|
112 | extractcats($dicfile) if $dicfile; |
---|
113 | |
---|
114 | |
---|
115 | my $cats_re = qr/(?:$attr::cat_re\s*(?:,\s*$attr::cat_re)*)/; |
---|
116 | |
---|
117 | # class parse_class: |
---|
118 | # /$attr::cat_re/g; |
---|
119 | |
---|
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>) |
---|
132 | { |
---|
133 | s/#.*//; |
---|
134 | s/^\s+//; |
---|
135 | s/\s+$//; |
---|
136 | if(/^AGR\s+(\S+)\s+(\S+)$/) |
---|
137 | { |
---|
138 | push @{$agr{$1}}, $2; |
---|
139 | } |
---|
140 | elsif(/^GOV\s+(\S+)\s+(\S+)$/) |
---|
141 | { |
---|
142 | push @{$gov{$1}}, attr::parse($2); |
---|
143 | } |
---|
144 | elsif(/^ROLE\s+\S+$/) |
---|
145 | { |
---|
146 | $roles{$_}=1; |
---|
147 | print OUTPUT "$_\n"; |
---|
148 | } |
---|
149 | elsif(/^SGL\s+\S+$/) |
---|
150 | { |
---|
151 | ++$nsgl; |
---|
152 | print OUTPUT "$_\n"; |
---|
153 | } |
---|
154 | elsif(/^REQ\s+(\S+)\s+(\S+)$/) |
---|
155 | { |
---|
156 | print OUTPUT "#$_\n"; |
---|
157 | my $cat = attr::parse $1; |
---|
158 | for my $atomcat (keys %cats) |
---|
159 | { |
---|
160 | if(attr::match @$cat, @{$cats{$atomcat}}) |
---|
161 | { |
---|
162 | print OUTPUT "REQ ".$atomcat." $2\n"; |
---|
163 | ++$nreq; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | elsif(/^LEFT\s+\S+$/) |
---|
168 | { |
---|
169 | ++$nleft; |
---|
170 | print OUTPUT "$_\n"; |
---|
171 | } |
---|
172 | elsif(/^RIGHT\s+\S+$/) |
---|
173 | { |
---|
174 | ++$nright; |
---|
175 | print OUTPUT "$_\n"; |
---|
176 | } |
---|
177 | elsif(my ($hs,$ds,$r) = /^LINK\s+($cats_re)\s+($cats_re)\s+(\S+)$/) |
---|
178 | { |
---|
179 | print OUTPUT "#$_\n"; |
---|
180 | for my $h ($hs =~ /$attr::cat_re/g) |
---|
181 | { |
---|
182 | for my $d ($ds =~ /$attr::cat_re/g) |
---|
183 | { |
---|
184 | addlinks($h,$d,$r); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | elsif(/^$/) { |
---|
189 | # pomijamy puste linie oraz komentarze |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | print STDERR "Illegal format: $_\n"; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | sub addlinks |
---|
199 | { |
---|
200 | my ($h,$d,$r) = @_; |
---|
201 | |
---|
202 | for my $a (@{$agr{$r}}) { print OUTPUT "#AGR $r $a\n"; } |
---|
203 | for my $c (@{$gov{$r}}) { print OUTPUT "#GOV $r ".attr::unparse(@$c)."\n"; } |
---|
204 | my $head = attr::parse $h; |
---|
205 | my $dep = attr::parse $d; |
---|
206 | |
---|
207 | for my $atomhead (keys %cats) |
---|
208 | { |
---|
209 | if(attr::match @$head, @{$cats{$atomhead}}) |
---|
210 | { |
---|
211 | DEP: |
---|
212 | for my $atomdep (keys %cats) |
---|
213 | { |
---|
214 | next DEP if ! attr::match @$dep, @{$cats{$atomdep}}; |
---|
215 | |
---|
216 | for my $a (@{$agr{$r}}) |
---|
217 | { |
---|
218 | next DEP if ! attr::agree(@{$cats{$atomhead}},@{$cats{$atomdep}},$a); |
---|
219 | } |
---|
220 | |
---|
221 | for my $c (@{$gov{$r}}) |
---|
222 | { |
---|
223 | next DEP if ! attr::match(@$c,@{$cats{$atomdep}}); |
---|
224 | } |
---|
225 | |
---|
226 | print OUTPUT "LINK "; |
---|
227 | print OUTPUT $atomhead." "; |
---|
228 | print OUTPUT $atomdep." $r\n"; |
---|
229 | ++$nlink; |
---|
230 | |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | printf STDERR "%6d CAT statements\n", 0+keys(%cats); |
---|
238 | printf STDERR "%6d ROLE statements\n", 0+keys(%roles); |
---|
239 | printf STDERR "%6d SGL statements\n", $nsgl; |
---|
240 | printf STDERR "%6d REQ statements\n", $nreq; |
---|
241 | printf STDERR "%6d LEFT statements\n", $nleft; |
---|
242 | printf STDERR "%6d RIGHT statements\n", $nright; |
---|
243 | printf STDERR "%6d LINK statements\n", $nlink; |
---|
244 | |
---|
245 | |
---|
246 | sub extractcats |
---|
247 | { |
---|
248 | my $file = shift; |
---|
249 | open DICFILE, "canonize $file |"; |
---|
250 | while(<DICFILE>) |
---|
251 | { |
---|
252 | while(/,([^[:space:];]+)/g) |
---|
253 | { |
---|
254 | my $cat=$1; |
---|
255 | next if !$cat || exists $cats{$cat}; |
---|
256 | $ncat++; |
---|
257 | print OUTPUT "CAT $1\n"; |
---|
258 | $cats{$cat}=attr::parse($cat); |
---|
259 | } |
---|
260 | } |
---|
261 | close DICFILE; |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | sub loadcats |
---|
266 | { |
---|
267 | my $file = shift; |
---|
268 | open CATFILE, "canonize $file |"; |
---|
269 | while(<CATFILE>) |
---|
270 | { |
---|
271 | tr/ \t\n//d; |
---|
272 | next if !$_ || exists $cats{$_}; |
---|
273 | print OUTPUT "CAT $_\n"; |
---|
274 | ++$ncat; |
---|
275 | $cats{$_}=attr::parse($_); |
---|
276 | } |
---|
277 | close CATFILE; |
---|
278 | } |
---|
279 | |
---|