forked from gilnoh/gigaword-lm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperstory_runner.pl
54 lines (43 loc) · 1.87 KB
/
perstory_runner.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
#!/usr/bin/perl
use strict;
use warnings;
my $NGRAM_COUNT_COMMAND="ngram-count";
my $NGRAM_COUNT_OPTIONS=" -write-binary-lm "; # save as binary models, saves loading time, but does not (should not) affect any result
# > ngram-count -text [filename] -lm [modelname] [additional options]
# this will generate model with default (3-gram, default discount, etc), if no additional option is affecting model related parameters.
# get a path
die "Usage: At least one argument needed; a dir path.\n(e.g. perl perstory_runner.pl \"./models/document\"). \nThe script will build one LM for each .story news file in the path's subdirs\n" unless ($ARGV[0]);
# sanity check
my $x = `$NGRAM_COUNT_COMMAND`;
die "Unable to run the ngram-count executable. Maybe not in path?\n" if (!defined($x));
my $toppath = $ARGV[0];
opendir (my $dh, $toppath) or die "can't open dir $ARGV[0]\n";
my @subdir; # will hold all subdirectories of the given path
foreach (readdir($dh))
{
next if ( ($_ eq "..") );
my $path = $toppath . "/" . $_;
push @subdir, $path if (-d $path);
}
close $dh;
print STDERR "$toppath has ", scalar (@subdir), " dirs (subdirs + itself) to follow. For each of the .story, one LM will be built.\n";
my $file_count=0;
foreach my $d (@subdir)
{
print STDERR "working on $d ";
# glob the files in the dir.
my @ls = glob($d . "/*.story");
print STDERR scalar(@ls), " files\n";
# for each, run ngram-count for each file with the set option
foreach (@ls)
{
my $inputfile = $_;
my $outputmodel = $inputfile . ".model";
my $command = $NGRAM_COUNT_COMMAND . " -text " . $inputfile . " -lm " . $outputmodel . $NGRAM_COUNT_OPTIONS . ">> stdout 2>> stderr";
`$command`;
$file_count++;
print STDERR "." unless ($file_count % 100);
}
print STDERR "\n";
}
print STDERR "processed and generated $file_count model files in total\n";