-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-dcdb-profile.perl
executable file
·253 lines (186 loc) · 5.79 KB
/
test-dcdb-profile.perl
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/perl -w
use lib '.';
use DiaColloDB;
use DiaColloDB::Utils qw(:json);
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
use File::Basename qw(basename);
use strict;
BEGIN {
select STDERR; $|=1; select STDOUT;
}
##----------------------------------------------------------------------
## Globals
##----------------------------------------------------------------------
##-- program vars
our $prog = basename($0);
our $verbose = 1;
our ($help,$version);
our $dbdir = undef;
our %coldb = (flags=>'r');
our $want_lemmata = 1;
our $want_dates = 1;
our $want_strings = undef; ##-- $want_lemmata || $want_dates
our $want_mi = 1;
our $want_ld = 1;
our $outfmt = 'text';
##----------------------------------------------------------------------
## Command-line processing
##----------------------------------------------------------------------
GetOptions(##-- general
'help|h' => \$help,
'version|V' => \$version,
'verbose|v=i' => \$verbose,
##-- local
'strings|s!' => sub { $want_dates=$want_lemmata=$_[1]; },
'lemmata|lemmas|lemma-strings|ls!' => \$want_lemmata,
'dates|date-strings|ds!' => \$want_dates,
'mi|m!' => \$want_mi,
'logdice|dice|ld|d!' => \$want_ld,
'scores|S!' => sub {$want_mi=$want_ld=$_[1]},
'text|t' => sub {$outfmt='text'},
'json|j' => sub {$outfmt='json'},
'null|noout' => sub {$outfmt=''},
);
pod2usage({-exitval=>0,-verbose=>0}) if ($help);
pod2usage({-exitval=>1,-verbose=>0,-msg=>"$prog: ERROR: no DBDIR specified!"}) if (@ARGV<1);
pod2usage({-exitval=>1,-verbose=>0,-msg=>"$prog: ERROR: no LEMMA specified!"}) if (@ARGV<2);
if ($version || $verbose >= 2) {
print STDERR "$prog version $DiaColloDB::VERSION by Bryan Jurish\n";
exit 0 if ($version);
}
##----------------------------------------------------------------------
## MAIN
##----------------------------------------------------------------------
##-- setup logger
DiaColloDB::Logger->ensureLog();
##-- open colloc-db
$dbdir = shift(@ARGV);
$dbdir =~ s{/$}{};
my $coldb = DiaColloDB->new(%coldb)
or die("$prog: failed to create new DiaColloDB object: $!");
$coldb->open($dbdir)
or die("$prog: DiaColloDB::open() failed for '$dbdir': $!");
##-- profile: get tuple-IDs
$coldb->trace("profile: get tuple-IDs");
my @lemmas = @ARGV;
my ($l,$li,$lxids,@xids);
my $lenum = $coldb->{lenum};
foreach $l (@lemmas) {
if (!defined($li=$lenum->s2i($l))) {
warn("$0: ignoring unknown lemma '$l'");
next;
}
push(@xids, @{$coldb->{l2x}->fetch($li)});
}
@xids = sort {$a <=> $b} @xids;
##-- profile: get co-frequency profile
$coldb->trace("profile: get co-frequency profile");
my $prf = $coldb->{cof}->profile(\@xids);
##-- stringify profile
my $sprf = $prf;
if ($want_lemmata || $want_dates) {
$coldb->trace("profile: stringify [", ($want_dates ? "+" : "-"), "dates, ", ($want_lemmata ? "+" : "-"), "lemmata]");
$sprf = $prf->new(N=>$prf->{N},f1=>$prf->{f1});
my $xenum = $coldb->{xenum};
my $pack_x = $coldb->{pack_x};
my ($xi2,$li2,$d2,$l2,$key2);
foreach my $xi2 (keys %{$prf->{f2}}) {
($li2,$d2) = unpack($pack_x, $xenum->i2s($xi2));
$l2 = $want_lemmata ? $lenum->i2s($li2) : $li2;
$key2 = "$d2\t$l2";
$sprf->{f2}{$key2} = $prf->{f2}{$xi2};
$sprf->{f12}{$key2} = $prf->{f12}{$xi2};
}
}
##-- compile collocation scores
if ($want_mi) {
$coldb->trace("compile_mi()");
$sprf->compile_mi();
}
if ($want_ld) {
$coldb->trace("compile_ld()");
$sprf->compile_ld();
}
##-- dump stringified profile
if ($outfmt eq 'text') {
$coldb->trace("saveTextFile()");
$sprf->saveTextFile('-');
}
elsif ($outfmt eq 'json') {
$coldb->trace("saveJsonFile()");
DiaColloDB::Utils::saveJsonFile($sprf, '-');
}
$coldb->trace("done.");
__END__
###############################################################
## pods
###############################################################
=pod
=head1 NAME
coldb-profile.perl - get a lemma-profile from a DiaColloDB
=head1 SYNOPSIS
coldb-profile.perl [OPTIONS] DBDIR LEMMA(S)...
General Options:
-help
-version
-verbose LEVEL
Local Options:
-[no]dates # do/don't include dates in output (default=do)
-[no]lemmas # do/don't include lemmata in output (default=do)
-[no]strings # alias for -[no]strings -[no]dates
-[no]mi # do/dont't compute MI*logf score (default=do)
-[no]dice # do/dont't compute log-Dice score (default=do)
-[no]scores # alias for -[no]mi -[no]dice
-text # use text output (default)
-json # use json output
-null # don't output profile at all
=cut
###############################################################
## OPTIONS
###############################################################
=pod
=head1 OPTIONS
=cut
###############################################################
# General Options
###############################################################
=pod
=head2 General Options
=over 4
=item -help
Display a brief help message and exit.
=item -version
Display version information and exit.
=item -verbose LEVEL
Set verbosity level to LEVEL. Default=1.
=back
=cut
###############################################################
# Other Options
###############################################################
=pod
=head2 Other Options
=over 4
=item -someoptions ARG
Example option.
=back
=cut
###############################################################
# Bugs and Limitations
###############################################################
=pod
=head1 BUGS AND LIMITATIONS
Probably many.
=cut
###############################################################
# Footer
###############################################################
=pod
=head1 ACKNOWLEDGEMENTS
Perl by Larry Wall.
=head1 AUTHOR
Bryan Jurish E<lt>moocow@cpan.orgE<gt>
=head1 SEE ALSO
perl(1).
=cut