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
|
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use Lingua::KO::Hangul::Util qw(:all);
use POSIX qw(ceil floor);
my $distribution_filename = 'undef';
my $analysis_filename = 'undef';
my $count_assigner_filename = 'undef';
my $counts_only = 0;
my $number_of_distinct_tiles = 30;
my $number_of_blanks = 2;
my $help = 0;
GetOptions('distribution=s' => \$distribution_filename,
'analysis=s' => \$analysis_filename,
'count_assigner=s' => \$count_assigner_filename,
'number_of_distinct_tiles=i' => \$number_of_distinct_tiles,
'number_of_blanks=i' => \$number_of_blanks,
'counts_only' => \$counts_only,
'help|?' => \$help) or pod2usage(2);
pod2usage(1) if $help;
=pod
=head1 NAME
generate_alphabet.pl - Print out quackle alphabet file
=head1 SYNOPSIS
--distribution=<file>
output of generate_distribution.pl
--analysis=<file>
output of analyse_gcgs.pl
--count_assigner=<file>
file with one number per line. Number on nth line is how many
tiles are in bag of letter that appears nth least often.
--number_of_distinct_tiles=[1, 30]
how many tiles are in the alphabet (should be same as length
of count assigner file).
--number_of_blanks=<nonnegative integer>
how many scoreless blanks are in the bag.
--counts_only
only output the counts of letters, in same format as
count_assigner input
=cut
my @count_assigner = ();
my $has_assigned_counts = 0;
my %character_counts;
my $count_sum = 0;
my $inverse_count_sum = 0;
my %character_points_per_appearance;
my $lowest_points_per_appearance = -1;
my $highest_points_per_appearance = 0;
my $maximum_value = 10;
binmode STDOUT, ':utf8';
sub read_count_assigner {
if ($count_assigner_filename eq 'undef') {
$has_assigned_counts = 0;
return;
}
if (!(-e $count_assigner_filename)) {
print STDERR "Count assigner $count_assigner_filename does not exist; ignoring.\n";
$has_assigned_counts = 0;
return;
}
$has_assigned_counts = 1;
open (my $input, "<:encoding(utf8)", $count_assigner_filename);
my $i = 0;
while (<$input>) {
chomp;
next if (/^\#/);
my ($count, @rest) = split /\s/, $_;
$count_assigner[$i] = $count;
++$i;
}
}
sub read_analysis {
if ($analysis_filename eq 'undef') {
return;
}
if (!(-e $analysis_filename)) {
print STDERR "Analysis $analysis_filename does not exist; ignoring.\n";
return;
}
open (my $input, "<:encoding(utf8)", $analysis_filename);
my $i = 0;
while (<$input>) {
chomp;
next if (/^\#/);
my ($letter, $points_per_appearance) = split /\s/, $_;
next if (! defined $points_per_appearance);
next if ($letter =~ /\[/);
next if ($letter =~ /\]/);
$letter =~ s/\|//g;
$character_points_per_appearance{$letter} = $points_per_appearance;
}
calculate_bounding_points_per_appearance_values();
}
sub calculate_bounding_points_per_appearance_values {
my $i = 0;
for my $character (sort { $character_counts{$a} <=> $character_counts{$b} } keys %character_counts) {
if (exists $character_counts{$character} && count_of_character($i, $character) > 0) {
my $points_per_appearance = $character_points_per_appearance{$character};
if ($lowest_points_per_appearance < 0 || $points_per_appearance < $lowest_points_per_appearance) {
$lowest_points_per_appearance = $points_per_appearance;
}
if ($points_per_appearance > $highest_points_per_appearance) {
$highest_points_per_appearance = $points_per_appearance;
}
}
++$i;
}
}
sub read_characters {
open (my $input, "<:encoding(utf8)", $distribution_filename);
my $i = 0;
while (<$input>) {
last if ($i >= $number_of_distinct_tiles);
chomp;
my ($letter, $count) = split(/\s/, $_);
$character_counts{$letter} = $count;
$count_sum += $count;
$inverse_count_sum += exp(-.0005*$count);
++$i;
}
}
sub max {
return $_[0] > $_[1]? $_[0] : $_[1];
}
sub value_of_character {
my ($letter) = @_;
$letter =~ s/\|//g;
if (exists $character_points_per_appearance{$letter}) {
my $value_multiplier = $maximum_value / ($highest_points_per_appearance - $lowest_points_per_appearance);
return max(floor(($highest_points_per_appearance - $character_points_per_appearance{$letter}) * $value_multiplier), 1);
}
# This was written VERY late at night.
#print "computing value of $letter; count_sum=$count_sum; my count=$character_counts{$letter}\n";
return max(floor(187 * exp(-.0005*$character_counts{$letter}) / $inverse_count_sum), 1);
}
sub hallucinate_assigned_counts {
my $total_rounded_count = $number_of_blanks;
my $i = 0;
for my $character (sort { $character_counts{$a} <=> $character_counts{$b} } keys %character_counts) {
my $original_count = $character_counts{$character};
my $rounded_count;
my $truth = 100 * $original_count / $count_sum;
if ($truth < 0.1) {
$rounded_count = 0;
} else {
$rounded_count = max(floor($truth), 1);
}
$total_rounded_count += $rounded_count;
$count_assigner[$i] = $rounded_count;
++$i;
}
$has_assigned_counts = 1;
}
sub count_of_character {
my ($index, $count) = @_;
if ($has_assigned_counts && $index < @count_assigner) {
return $count_assigner[$index]
}
return 0;
}
sub spit_characters {
if ($counts_only) {
my $i = 0;
for my $character (sort { $character_counts{$a} <=> $character_counts{$b} } keys %character_counts) {
my $count = count_of_character($i, $character_counts{$character});
print "$count #" . ($i + 1) . ": $character\n";
++$i;
}
return;
}
my $total_score = 0;
my $total_count = $number_of_blanks;
print "blank 0 $number_of_blanks\n";
print "#char\tblank\tscore\tcount\tvowel\n";
my $i = 0;
for my $character (sort { $character_counts{$a} <=> $character_counts{$b} } keys %character_counts) {
my $blank_text = lc($character);
# TODO lower-case blanks currently broken.
my $disable_lower_case_blanks = 1;
if ($disable_lower_case_blanks || $blank_text eq $character) {
$blank_text = '['.$character.']';
$blank_text =~ s/\|//g;
}
my $score = value_of_character($character);
my $count = count_of_character($i, $character_counts{$character});
$total_count += $count;
$total_score += $score;
my $is_vowel = '0';
# TODO make work for unfilled jamo
if (isStandardForm($character)) {
my @codepoints = unpack('U*', $character);
if ($codepoints[0] == 0x115F) {
$is_vowel = '1';
}
}
++$i;
print "$character\t$blank_text\t$score\t$count\t$is_vowel\n";
}
print "# Total count: $total_count\n";
print "# Total score: $total_score\n";
}
read_characters();
read_count_assigner();
if (!$has_assigned_counts) {
hallucinate_assigned_counts();
}
read_analysis();
spit_characters();
|