1 | #! /usr/bin/env perl |
---|
2 | |
---|
3 | #package: UAM Text Tools |
---|
4 | #component: compiledic |
---|
5 | #version: 1.0 |
---|
6 | #author: Tomasz Obrebski |
---|
7 | |
---|
8 | use strict; |
---|
9 | use locale; |
---|
10 | use File::HomeDir; |
---|
11 | use File::Basename; |
---|
12 | use File::Temp; |
---|
13 | use File::Copy; |
---|
14 | use Getopt::Long; |
---|
15 | |
---|
16 | |
---|
17 | my $linesPerFile = 20000; |
---|
18 | |
---|
19 | |
---|
20 | my $systemconfigfile='/usr/local/etc/utt/compiledic.conf'; |
---|
21 | my $userconfigfile=home()."/.utt/compiledic.conf"; |
---|
22 | |
---|
23 | Getopt::Long::Configure('no_ignore_case_always'); |
---|
24 | |
---|
25 | #my $symfile='~/.utt/pl/pl_PL.iso-8859-2.sym'; |
---|
26 | my $symbols=0; |
---|
27 | my $help=0; |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | #read configuration files########################### |
---|
32 | my $file; |
---|
33 | foreach $file ($systemconfigfile, $userconfigfile){ |
---|
34 | if(open(CONFIG, $file)){ |
---|
35 | while (<CONFIG>) { |
---|
36 | chomp; |
---|
37 | s/#.*//; |
---|
38 | s/^\s+//; |
---|
39 | s/\s+$//; |
---|
40 | next unless length; |
---|
41 | my ($name, $value) = split(/\s*=\s*/, $_, 2); |
---|
42 | if($name eq "symbols"){ |
---|
43 | $symbols=$value; |
---|
44 | } |
---|
45 | elsif(($name eq "help")or($name eq "h")){ |
---|
46 | $help=1; |
---|
47 | } |
---|
48 | |
---|
49 | } |
---|
50 | close CONFIG; |
---|
51 | } |
---|
52 | } |
---|
53 | ######################################################### |
---|
54 | |
---|
55 | GetOptions("symbols=s" => \$symbols, |
---|
56 | "help|h" => \$help); |
---|
57 | |
---|
58 | if($help) |
---|
59 | { |
---|
60 | print <<'END' |
---|
61 | Usage: compiledic [OPTIONS] dictionaryfile |
---|
62 | |
---|
63 | Options: |
---|
64 | --help -h Help. |
---|
65 | --symbols=FILE Symbol file. |
---|
66 | END |
---|
67 | ; |
---|
68 | exit 0; |
---|
69 | } |
---|
70 | |
---|
71 | ################################################## |
---|
72 | |
---|
73 | -f $symbols or die("Symbol file not found."); |
---|
74 | |
---|
75 | @ARGV > 0 or die("Source dictionary not given."); |
---|
76 | |
---|
77 | my $file = shift; |
---|
78 | |
---|
79 | -f $file or die("Source dictionary not found."); |
---|
80 | |
---|
81 | $file =~ /(.*)\.dic/ or die("The input file must have .dic extension."); |
---|
82 | |
---|
83 | my $filenameprefix = $1; |
---|
84 | |
---|
85 | ################################################## |
---|
86 | |
---|
87 | # Tworzymy katalog tymczasowy, gdzie wszystko bedzie umieszczane. |
---|
88 | my $tmp_root = File::Temp::tempdir( CLEANUP => 1 ); |
---|
89 | |
---|
90 | (undef, my $symfile) = File::Temp::tempfile( DIR => $tmp_root, SUFFIX => ".sym" ); |
---|
91 | my $symfilenoext=$symfile; |
---|
92 | $symfilenoext =~ s/\.sym$//; |
---|
93 | my $labfile = $symfilenoext . '.lab'; |
---|
94 | my $sclfile = $symfilenoext . '.scl'; |
---|
95 | |
---|
96 | copy($symbols, $symfile); |
---|
97 | |
---|
98 | # Przygotowanie etykiet |
---|
99 | |
---|
100 | `lexmakelab $symfilenoext`; |
---|
101 | |
---|
102 | |
---|
103 | # Analiza pliku slownika |
---|
104 | |
---|
105 | print "preparing file..........................................."; |
---|
106 | #print "... sed section .....\n"; |
---|
107 | (undef, my $file_sed) = File::Temp::tempfile( DIR => $tmp_root, SUFFIX => ".sed" ); |
---|
108 | `sed -r "s/([[:punct:]])/\[\\1\]/g" < $file > $file_sed`; |
---|
109 | |
---|
110 | print "OK\n"; |
---|
111 | |
---|
112 | #dzielimy plik na wiele czesci, uruchamiamy lexcomplex dla kazdej |
---|
113 | #czesci osobno, nastepnie laczymy to za pomoca programu fsmunion |
---|
114 | |
---|
115 | #print "Dziele slownik na mniejsze czesci..."; |
---|
116 | |
---|
117 | open(IN, $file_sed); |
---|
118 | |
---|
119 | my $lineCount = 0; |
---|
120 | my $fileCount = 0; |
---|
121 | |
---|
122 | open(FILE, ">$tmp_root/slo_$fileCount"); |
---|
123 | |
---|
124 | while (<IN>) { |
---|
125 | |
---|
126 | if (++$lineCount >= $linesPerFile) { |
---|
127 | $fileCount++; |
---|
128 | $lineCount = 0; |
---|
129 | |
---|
130 | close(FILE); |
---|
131 | # print "Tworze nowy plik tymczasowy: slo_".$fileCount."\n"; |
---|
132 | open(FILE, ">$tmp_root/slo_".$fileCount); |
---|
133 | } |
---|
134 | |
---|
135 | print(FILE $_); |
---|
136 | } |
---|
137 | |
---|
138 | #print "OK\n"; |
---|
139 | |
---|
140 | print "building partial automata"; |
---|
141 | |
---|
142 | #32 kropki, fileCount plikow |
---|
143 | my $filesPerDot = $fileCount/32; |
---|
144 | my $files=$filesPerDot; |
---|
145 | my $dots=0; |
---|
146 | |
---|
147 | for (my $i=0; $i<=$fileCount; $i++) { |
---|
148 | |
---|
149 | if ($files >= $filesPerDot) { |
---|
150 | $files = 0; |
---|
151 | print "."; |
---|
152 | $dots++; |
---|
153 | } |
---|
154 | $files++; |
---|
155 | |
---|
156 | `lexcomplex -l $labfile -S $sclfile < $tmp_root/slo_$i > $tmp_root/slownik_$i.fsm`; |
---|
157 | |
---|
158 | } |
---|
159 | if ($dots < 32) { |
---|
160 | for (my $i=0; $i<32 - $dots; $i++) { |
---|
161 | print "."; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | print "OK\n"; |
---|
166 | |
---|
167 | unlink <$tmp_root/slo_*>; |
---|
168 | |
---|
169 | print "building final automaton"; |
---|
170 | |
---|
171 | #35 kropek... |
---|
172 | my $ndots=33; |
---|
173 | $filesPerDot = $fileCount/$ndots; |
---|
174 | $files=$filesPerDot; |
---|
175 | $dots=0; |
---|
176 | |
---|
177 | copy("$tmp_root/slownik_0.fsm", "$tmp_root/slownik1.fsm"); |
---|
178 | |
---|
179 | for (my $i=1; $i<=$fileCount; $i++) { |
---|
180 | |
---|
181 | if ($files >= $filesPerDot) { |
---|
182 | $files = 0; |
---|
183 | print "."; |
---|
184 | $dots++; |
---|
185 | } |
---|
186 | $files++; |
---|
187 | |
---|
188 | `fsmunion $tmp_root/slownik_$i slownik1.fsm > $tmp_root/slownik2.fsm`; |
---|
189 | |
---|
190 | move("$tmp_root/slownik2.fsm", "$tmp_root/slownik1.fsm") || die "Unable to move $tmp_root/slownik2.fsm!\n"; |
---|
191 | } |
---|
192 | |
---|
193 | if ($dots < $ndots) { |
---|
194 | for (my $i=0; $i<$ndots - $dots; $i++) { |
---|
195 | print "."; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | #`fsmunion $tmp_root/* > $tmp_root/slownik1.fsm`; |
---|
200 | |
---|
201 | print "OK\n"; |
---|
202 | |
---|
203 | print "removing epsilon-transitions............................."; |
---|
204 | |
---|
205 | `fsmrmepsilon $tmp_root/slownik1.fsm > $tmp_root/slownik2.fsm`; |
---|
206 | |
---|
207 | unlink ("$tmp_root/slownik1.fsm"); |
---|
208 | |
---|
209 | print "OK\n"; |
---|
210 | |
---|
211 | print "determinizing automaton.................................."; |
---|
212 | |
---|
213 | `fsmdeterminize $tmp_root/slownik2.fsm > $tmp_root/slownik1.fsm`; |
---|
214 | |
---|
215 | unlink ("$tmp_root/slownik2.fsm"); |
---|
216 | |
---|
217 | print "OK\n"; |
---|
218 | |
---|
219 | print "minimizing automaton....................................."; |
---|
220 | |
---|
221 | `fsmminimize $tmp_root/slownik1.fsm > $tmp_root/slownik.fsm`; |
---|
222 | |
---|
223 | #`rm slownik1.fsm`; |
---|
224 | |
---|
225 | print "OK\n"; |
---|
226 | |
---|
227 | print "converting fsm format to bin............................."; |
---|
228 | |
---|
229 | `fsmprint -i $labfile $tmp_root/slownik.fsm > $tmp_root/slownik.txt`; |
---|
230 | |
---|
231 | `fsm2aut $tmp_root/slownik.txt > $tmp_root/slownik.aut`; |
---|
232 | |
---|
233 | `aut2fsa < $tmp_root/slownik.aut > $filenameprefix.bin`; |
---|
234 | |
---|
235 | print "OK\n"; |
---|
236 | |
---|
237 | print "removing temporary files................................."; |
---|
238 | |
---|
239 | unlink <$tmp_root/*>; |
---|
240 | unlink ($tmp_root); |
---|
241 | |
---|
242 | print "OK\n"; |
---|