-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateAmbiguities.pl
executable file
·134 lines (117 loc) · 3.74 KB
/
calculateAmbiguities.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
#!/usr/bin/perl -sw
#####################
#
# ©2013 Autodesk Development Sàrl
#
# Created on 04 Jun 2013 by Ventsislav Zhechev
#
# Changelog
# v0.2.1 Modified by Ventsislav Zhechev on 05 Jul 2013
# The JSON output is now cleared before writing.
#
# v0.2 Modified by Ventsislav Zhechev on 24 Jun 2013
# Now the data is output in JSON format.
#
# v0.1 Modified by Ventsislav Zhechev on 04 Jun 2013
# First version.
#
#####################
use strict;
use utf8;
use Encode qw/encode decode/;
use Storable qw/dclone/;
use Data::Serializer::Raw;
my $oldSource = my $oldTarget = my $oldProduct = "";
my $oldProbability = my $maxProbability = -1;
my $probabilityThreshold = 10E-2;
my @statistics;
my $currentSource = my $currentTarget = 0;
my %products;
our $output;
my %data;
my $serialiser = Data::Serializer::Raw->new(serializer => "JSON")
or die "Could not create serialiser: $!\n";
sub processTarget {
my ($source, $target, $product, $probability, $maxProbability) = @_;
# print encode "utf-8", "$source → $target; Old probability is $oldProbability; max probability is $maxProbability\n";
if ($maxProbability > .75 && 1.*$oldProbability / $maxProbability > $probabilityThreshold) {
++$currentTarget;
push @statistics, {} unless defined $statistics[$currentTarget];
$statistics[$currentTarget]->{$target} = [] unless defined $statistics[$currentTarget]->{$target};
push @{$statistics[$currentTarget]->{$target}}, [$product, $probability];
}
}
sub processProduct {
&processTarget(@_);
++$products{$_[2]};
}
sub processSource {
&processProduct(@_);
if (1 < keys %{$statistics[1]}) {
print encode "utf-8", "Source ".(++$currentSource).": “$_[0]”:\n";
$data{$_[0]} = [];
# ++$currentSource;
for (my $rank = 1; $rank < @statistics; ++$rank) {
print encode "utf-8", "\tRank $rank:\n";
# $data{$_[0]}->[$rank-1] = dclone($statistics[$rank]);
foreach my $target (keys %{$statistics[$rank]}) {
foreach my $prodProb (@{$statistics[$rank]->{$target}}) {
$data{$_[0]}->[$rank-1]->{$target}->{$prodProb->[0]} = $prodProb->[1];
}
}
my $currentTarget = 0;
foreach my $target (keys %{$statistics[$rank]}) {
print encode "utf-8", "\t\tTarget ".(++$currentTarget).": “$target”: ";
print encode "utf-8", join ", ", map {join "→", @$_} @{$statistics[$rank]->{$target}};
print encode "utf-8", "\n";
}
}
print encode "utf-8", "\n";
}
}
while (<>) {
unless ($.%100000) {
print STDERR ".";
print STDERR "$." unless $.%1000000
}
chomp;
my ($source, $target, $probability, undef, $product) = split / ?\| ?/, decode "utf-8", $_;
$maxProbability = $probability unless $maxProbability > 0;
if ($source ne $oldSource) {
if ($oldSource ne "") {
&processSource($oldSource, $oldTarget, $oldProduct, $oldProbability, $maxProbability);
}
$oldSource = $source;
$oldTarget = $target;
$oldProduct = $product;
$oldProbability = $probability;
$maxProbability = $probability;
@statistics = ();
$currentTarget = 0;
next;
}
if ($product ne $oldProduct) {
if ($oldProduct ne "") {
&processProduct($oldSource, $oldTarget, $oldProduct, $oldProbability, $maxProbability);
}
$oldTarget = $target;
$oldProduct = $product;
$oldProbability = $probability;
$maxProbability = $probability;
$currentTarget = 0;
next;
}
if ($probability != $oldProbability) {
if ($oldProbability > 0) {
&processTarget($oldSource, $oldTarget, $oldProduct, $oldProbability, $maxProbability);
}
$oldProbability = $probability;
$oldTarget = $target;
next;
}
}
print STDERR encode "utf-8", "\nEncountered products: ".(join ", ", sort {$a cmp $b} keys %products)."\n";
print STDERR encode "utf-8", "Number of potentially ambiguous source phrases: $currentSource\n";
unlink $output;
$serialiser->store(\%data, $output);
1;