-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-model.pl
executable file
·226 lines (169 loc) · 3.9 KB
/
build-model.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env perl
use strict;
use warnings;
use sigtrap 'handler' => \&storeModel, 'INT', 'ABRT', 'QUIT', 'TERM';
use File::Path qw(make_path);
use PerlIO::gzip;
use Getopt::Long qw/GetOptionsFromArray/;
binmode(STDOUT, ':bytes');
binmode(STDERR, ':bytes');
my $lineId = 1;
my $wordId = 1;
my %dict = ();
my $ngram = 5;
my $fileList;
my $outDir;
my %opts = (
ngram => \$ngram,
out => \$outDir,
fl => \$fileList
);
my @optionspec = (
"ngram=i",
"out=s",
"fl=s",
);
GetOptions(\%opts, @optionspec) or exit 1;
# ngram
if ( ! defined($ngram) ) {
print STDERR "N-gram size must be specified! (--ngram)\n";
help();
}
# file list
if ( ! defined($fileList) ) {
print STDERR "Path to file list must be specified! (--fl)\n";
help();
}
if ( ! -f $fileList ) {
print STDERR "File list [$fileList] doesn\'t exist.\n";
help();
}
# output dir
if ( ! defined($outDir) ) {
print STDERR "Path to output directory must be specified! (--output)\n";
help();
}
my $sub = "";
my $subsub = "";
my $padding = ' ' x ($ngram - 1);
my @files = ();
open(my $fhList, "<", $fileList) or die $fileList . ": " . $!;
while ( <$fhList> ) {
chomp;
next if ! $_;
my ($code, $file) = split(/\t/, $_);
push(@files, [$code, $file]);
print STDERR "\nReading file: $file\n";
if ( ! -f $file ) {
print STDERR "Skipping $_\n";
next;
}
my $fh = my_open($file);
while ( <$fh> ) {
chomp;
s/ +/ /g;
s/^ +//g;
s/ +$//g;
if ( ! $_ ) {
next;
}
$_ = $padding . $_ . $padding;
{
use bytes;
for my $i (0 .. bytes::length($_) - $ngram) {
$sub = substr($_, $i, $ngram);
for my $j (1 .. $ngram) {
# for my $j ($ngram .. $ngram) {
$subsub = bytes::substr($sub, 0, $j);
# if ( $subsub =~ /[[:digit:][:punct:]]/ ) {
# next;
# }
if ( ! defined($dict{$j}{$subsub}) ) {
$dict{$j}{$subsub}{___total___} = 0;
$wordId++;
}
$dict{$j}{$subsub}{___total___}++;
$dict{$j}{$subsub}{$code}++;
}
}
}
if ( $lineId % 1000 == 0 ) {
print STDERR "*";
if ( $lineId % 10000 == 0 ) {
print STDERR "\t" . sprintf("%9d\t%9d\n", $lineId, $wordId) ;
}
}
$lineId++;
}
close($fh);
}
print STDERR "\n\n";
print STDERR "Total\nLines: $lineId\nWordIds: $wordId\n";
storeModel();
sub storeModel
{
print STDERR "\n";
make_path($outDir);
push(@files, ['___total___', '___total___']);
for my $item (sort { $a->[0] cmp $b->[0] } @files) {
my $code = $item->[0];
for my $j (1 .. $ngram) {
my $outFile = $code . ".$j". ".txt.gz";
print STDERR "Storing model for $code and $j to $outFile\n";
open(my $fhModel, ">:gzip:bytes", $outDir. "/" . $outFile) or die $!;
{
no warnings;
my %resDict = ();
for my $k (keys %{$dict{$j}}) {
if ( defined($dict{$j}{$k}{$code}) ) {
$resDict{$k} = $dict{$j}{$k}{$code};
}
}
for my $k (sort { $resDict{$b} <=> $resDict{$a} } keys %resDict) {
print $fhModel "$k\t$resDict{$k}\n";
}
}
close($fhModel);
}
}
exit 0;
}
sub my_open {
my $f = shift;
if ($f eq "-") {
binmode(STDIN, ":bytes");
return *STDIN;
}
die "Not found: $f" if ! -e $f;
my $opn;
my $hdl;
my $ft = `file '$f'`;
# file might not recognize some files!
if ($f =~ /\.gz$/ || $ft =~ /gzip compressed data/) {
$opn = "zcat $f |";
} elsif ($f =~ /\.bz2$/ || $ft =~ /bzip2 compressed data/) {
$opn = "bzcat $f |";
} else {
$opn = "$f";
}
open $hdl, $opn or die "Can't open '$opn': $!";
binmode $hdl, ":bytes";
return $hdl;
}
sub help
{
print <<DOC;
Usage:
build-model.pl --ngram=N --fl=file-list --out=output-dir
Creates model for YALI recognizer.
--ngram=N
Ngram size.
--fl=FILE
File with list of files used for building models.
This file contains two columns, the first one
with languge code and the second one with path to text file
--out=DIR
Output directory for created models.
DOC
exit(0);
}