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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| #!/usr/bin/perl
use strict;
use warnings;
use feature qw(:5.10);
use Fcntl qw(SEEK_SET);
use Getopt::Long;
use Data::Dumper;
use File::Util;
use List::Util qw(min max sum);
use List::MoreUtils qw(uniq indexes);
use Time::ETA;
use Term::ProgressBar;
my $VERSION = "6";
my ($systeminfo, $meminfo);
eval "use Win32::SystemInfo;";
$systeminfo = !$@;
if (!$systeminfo) {
eval "use Sys::MemInfo qw(freemem totalmem);";
$meminfo = !$@;
}
warn "Neither module Win32::SystemInfo, nor Sys:MemInfo is installed\n" if !$systeminfo && !$meminfo;
$|++;
# taille sequence dans le fichier initiale, du 2e fichier et frequence
my ($n, $m, $f_opt, $size_max, $debug, $save_mem) = (1) x 3;
my ($in, $out, $result) = (qw(Texte_FR.txt Texte_EN.txt));
my ($group_opt, $group_start, $group_count) = (100, undef, undef);
GetOptions ('n=i' => \$n, 'm=i' => \$m, 'f=s' => \$f_opt, 'in=s' => \$in, 'out=s' => \$out, 'res=s' => \$result, 'd' => \$debug, 's' => \$save_mem, 'g=s'=> \$group_opt);
$size_max = $ARGV[0];
$result //= "MMIv$VERSION-m$m-n$n-f$f_opt".($size_max ? "-$size_max" : "").".txt";
(my $group, $group_start, $group_count) = split ":", $group_opt;
my ($f_m, $f_n) = split ":", $f_opt;
my $file_size = $size_max // File::Util->line_count($in);
sub get_x_gram($$;$) {
my ($words, $x_gram_size, $filter_cb) = @_;
$words = [ split /\s+/, trim($words) ] if !ref $words;
my @x_gram = map { "@{$words}[$_ .. $_ + $x_gram_size-1]" } 0 .. @$words-$x_gram_size;
return !$filter_cb ? @x_gram : grep $filter_cb->($_), @x_gram;
}
sub trim($) {
my $string = shift;
if (ref($string)) {
$$string =~ s/^\s+|\s+$//g;
}
else {
$string =~ s/^\s+|\s+$//g;
return $string;
}
}
my $log2 = log(2);
sub log2($) {
my $n = shift;
return log($n)/$log2;
}
my $print_progress = 0.5;
{
sub get_available_memory;
my %mem;
my $avail_mem = get_available_memory;
my $initial_mem = $avail_mem;
sub get_available_memory {
my ($used_mem, $new_avail_mem);
if ($systeminfo) {
Win32::SystemInfo::MemoryStatus(\%mem, "MB");
#say "Current mem: ", Data::Dumper->Dump([\%mem], [qw(*mem)]);
$new_avail_mem = $mem{AvailPhys};
}
elsif ($meminfo) {
$new_avail_mem = freemem() / 1024;
}
$used_mem = ($avail_mem // 0) - $new_avail_mem;
$avail_mem = $new_avail_mem;
return wantarray ? ($avail_mem, $used_mem, $initial_mem - $new_avail_mem) : $avail_mem;
}
}
sub remove_x_gram($$$;$) {
#my ($x_grams, @x_gram_to_remove) = @_;
my ($x_grams, $x_gram_count, $label, $print) = @_;
if (!$save_mem) {
my ($x_gram_size, $remove_progress, $next_update);
$x_gram_size = split /\s+/, $x_grams->[0]->[0] // "";
if ($print) {
$remove_progress = Term::ProgressBar->new({ ETA => "linear", count => scalar(@$x_grams), name => $label });
$remove_progress->max_update_rate($print_progress);
$next_update = 0;
}
else {
print "$label ", scalar(@$x_grams);
}
my $i = 0;
foreach my $x_gram (@$x_grams) {
$next_update = $remove_progress->update($i) if $print && $i >= $next_update;
next if !@$x_gram;
if (ref($x_gram_count) eq 'HASH') {
splice @$x_gram, $_, 1, foreach grep !exists $x_gram_count->{$x_gram->[$_]}, reverse 0 .. $#{$x_gram};
}
else {
my @x_gram_to_remove;
@x_gram_to_remove = ref($x_gram_count) eq 'ARRAY' ? @$x_gram_count : $x_gram_count;
splice @$x_gram, $_, 1 foreach reverse indexes { $_ ~~ @x_gram_to_remove } @$x_gram;
}
$i++
}
say "";
}
else {
# Impossible to remove x_gram from the complete sentence as each word of the x_gram can be used in
# another adjoining x_gram.
# Filter must be done during x_gram generation
}
}
sub count_m_n_gram($$$$;$$) {
my ($files, $x_grams_sizes, $x_gram_filters, $file_size, $label, $keep_x_grams) = @_;
my (@m_grams, @n_grams) = ();
my (%m_gram_count, %n_gram_count) = ();
seek $_, 0, SEEK_SET foreach @$files;
my $count_progress = Term::ProgressBar->new({ ETA => "linear", count => $file_size, name => $label });
$count_progress->max_update_rate($print_progress);
my $next_update = 0;
my ($m_file, $n_file) = @$files;
while (defined(my $m_words = <$m_file>)) {
last if $. > $file_size;
$next_update = $count_progress->update($.) if $. >= $next_update;
my @m_gram = get_x_gram($m_words, $x_grams_sizes->[0]);
push @m_grams, $keep_x_grams ? [ @m_gram ] : $m_words;
$m_gram_count{$_}++ foreach @m_gram;
my $n_words = <$n_file>;
my @n_gram = get_x_gram($n_words, $x_grams_sizes->[1]);
push @n_grams, $keep_x_grams ? [ @n_gram ] : $n_words;
$n_gram_count{$_}++ foreach @n_gram;
}
seek $_, 0, SEEK_SET foreach @$files;
# delete non frequent enought x_grams
say "";
my @rare_x_gram;
if ($x_gram_filters->[0]) {
@rare_x_gram = grep $m_gram_count{$_} < $x_gram_filters->[0], keys %m_gram_count;
delete @m_gram_count{@rare_x_gram};
remove_x_gram(\@m_grams, \%m_gram_count, "Remove non frequent enough $m-mgrams");
}
if ($x_gram_filters->[1]) {
@rare_x_gram = grep $n_gram_count{$_} < $x_gram_filters->[1], keys %n_gram_count;
delete @n_gram_count{@rare_x_gram};
remove_x_gram(\@n_grams, \%n_gram_count, "Remove non frequent enough $n-ngrams");
}
return sum(values %m_gram_count), \@m_grams, \%m_gram_count, sum(values %n_gram_count), \@n_grams, \%n_gram_count;
}
my $previous_group_key_count;
sub compute_group($$$$$$$$$$$$$) {
my ($RESULT, $group_size, $m_gram_in_group, $m_grams, $m, $m_gram_count, $m_gram_total, $n_grams, $n, $n_gram_count, $n_gram_total, $key_total, $label) = @_;
my $group_progress = Term::ProgressBar->new({ ETA => "linear", count => scalar(@$m_grams), name => "Counting rules $label" });
$group_progress->max_update_rate($print_progress);
my ($next_update, $m_gram_position) = (0, 0);
my %key_count;
foreach my $m_gram_line (@$m_grams) {
$next_update = $group_progress->update($m_gram_position) if $m_gram_position >= $next_update;
$m_gram_line = [ get_x_gram($m_gram_line, $m) ] if !ref($m_gram_line);
my $n_gram_line = $n_grams->[$m_gram_position++];
$n_gram_line = [ get_x_gram($n_gram_line, $n) ] if !ref($n_gram_line);
# filter m_grams of current group
foreach my $m_gram (grep exists $m_gram_in_group->{$_}, @$m_gram_line) {
foreach my $n_gram (grep exists $n_gram_count->{$_}, @$n_gram_line) {
$key_count{$m_gram}->{$n_gram}++;
}
}
}
$group_progress->update($m_gram_position);
say sprintf "Memory: available: %dMo, used since last call: %dMo, total used: %dMo", get_available_memory;
my $group_key_count = sum(map { scalar(keys %{$key_count{$_}}) } keys %key_count);
if ($group_key_count) {
#say "\n$group_key_count (" . (keys %key_count) . " m-grams) keys in the group";
say "";
$group_progress = Term::ProgressBar->new({ ETA => "linear", count => $group_key_count, name => "Computing $group_key_count rules proba (" . (keys %key_count) . " m-grams)" });
$group_progress->max_update_rate($print_progress);
($next_update, my $key_group_position) = (0, 0);
foreach my $m_gram (sort keys %key_count) {
foreach my $n_gram (sort keys %{$key_count{$m_gram}}) {
$next_update = $group_progress->update($key_group_position) if $key_group_position >= $next_update;
my $mmi = $key_count{$m_gram}->{$n_gram};
my $mmi_str = qq{(\$key_count{$m_gram}->{$n_gram}} if $debug;
my $mmi_num = qq{($mmi} if $debug;
$mmi /= $key_total;
if ($debug) {
$mmi_str .= qq{ / \$key_total)};
$mmi_num .= qq{ / $key_total)};
}
if ($debug) {
$mmi_str .= qq{ * log2(\$mmi / (\$m_gram_count{$m_gram}/\$m_gram_total)*(\$n_gram_count{$n_gram}/\$n_gram_total))};
$mmi_num .= qq{ * log2($mmi / ($m_gram_count->{$m_gram}/$m_gram_total)*($n_gram_count->{$n_gram}/$n_gram_total))};
say { $RESULT } "$m_gram > $n_gram > ", $mmi_str;
say { $RESULT } "$m_gram > $n_gram > ", $mmi_num;
}
$mmi *= log2($mmi/(($m_gram_count->{$m_gram}/$m_gram_total)*($n_gram_count->{$n_gram}/$n_gram_total)));
printf { $RESULT } "$m_gram > $n_gram > %.15f\n", $mmi;
$key_group_position++;
}
}
$group_progress->update($group_key_count);
$group_size *= min(10, $previous_group_key_count / $group_key_count) if $previous_group_key_count;
say "";
}
else {
say "\nNo key $label";
}
$previous_group_key_count = $group_key_count if !defined $previous_group_key_count;
return int($group_size);
}
say "File size to treat: ".File::Util->line_count($in).($size_max ? " limited to $file_size" : "");
say "Partial treatment of $group_count groups of $group lines" if $group_count;
say "Starting at line $group_start" if $group_start;
say "Memory: available: " . scalar(get_available_memory) . "Mo";
open my $IN, "<", $in or die "Can't open $in for reading: $!\n";
open my $OUT, "<", $out or die "Can't open $out for reading: $!\n";
open my $RESULT, $group_start ? ">>" : ">", $result or die "Can't open $result for writing: $!\n";
# read $IN and $OUT entirely + create x-grams and count them
my ($m_gram_total, $m_grams, $m_gram_count, $n_gram_total, $n_grams, $n_gram_count) =
count_m_n_gram([$IN, $OUT], [$m, $n], [$f_m, $f_n], $file_size, "Loading x-grams ", !$save_mem);
undef $IN;
undef $OUT;
say "Total m-grams: $m_gram_total (".(keys %$m_gram_count)." unique m-gram)";
say "Total n-grams: $n_gram_total (".(keys %$n_gram_count)." unique n-gram)";
say sprintf "Memory: available: %dMo, used since last call: %dMo, total used: %dMo", get_available_memory;
my $key_total = $m_gram_total * $n_gram_total;
say "key_total = $key_total";
my ($next_update, $m_gram_position, $m_gram_lines) = (0, 0, scalar(@$m_grams));
my $current_group = $group;
while ($m_gram_position < @$m_grams && (!defined $group_count || $group_count)) {
$current_group = $group_start ? $group_start : $group;
my @m_gram_group = @{$m_grams}[ $m_gram_position .. min($#{$m_grams}, $m_gram_position+$current_group-1) ];
my $m_gram_in_group = { map { $_ => 1 } map { ref $_ ? @$_ : get_x_gram($_, $m) } @m_gram_group };
undef @m_gram_group;
my $new_group;
if (!$group_start) {
$new_group = compute_group($RESULT, $current_group,
$m_gram_in_group, $m_grams, $m, $m_gram_count, $m_gram_total,
$n_grams, $n, $n_gram_count, $n_gram_total, $key_total,
"for group $m_gram_position/$m_gram_lines (group size $group)");
$group_count-- if $group_count;
}
else {
undef $group_start;
}
delete $m_gram_count->{$_} foreach keys %$m_gram_in_group;
say sprintf "Memory: available: %dMo, used since last call: %dMo, total used: %dMo", get_available_memory;
remove_x_gram($m_grams, $m_gram_count, "\nRemoving already treated ${m}-mgrams");
$m_gram_position += $current_group;
if ($new_group) {
$group = max($group_opt, min($m_gram_lines-$m_gram_position, $new_group));
}
}
say "Last line processed: $m_gram_position";
exit; |
Partager