-
Notifications
You must be signed in to change notification settings - Fork 1
/
output_all_n_comb.pl
160 lines (139 loc) · 4.04 KB
/
output_all_n_comb.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
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
#
use strict;
use warnings;
use Math::Combinatorics;
# configuration
my $GramN = 4;
my $GRAM_TABLE_FILE = "./afp2010_ngrams/gram4.csv";
#my $GRAM_TABLE_FILE = "./afp2010_ngrams/gram5.csv";
# this is the target ...
my @SCT = (
"IATSNEDBOONANYNOOOTSPROOLEDCCUODYOUFBOAHORRFWWKTHJEHDEENHUTISYNOCTEAFAOFLCSAONRLOIEUCHNATNULAAYDSUDMEIMATADUIMSYRHVOADETMMJTPIIN",
"EOHYEYRHBOESOCTWUSSEVMHYLIDTHNOLEUSLEDAIRLOLEAEWAETEELKNBSEHTSNWNBNNETNMEELEMNHHRTTRIYIARMENCELBGSCIETEOEIEALESRAICETSLSHAELHIEL",
"FSTEDTHGDESTPSSORRINFHNCCEAEEEEUEIARGREATEAHTTIEAEOSITNDHNCEINLUODTNHSXERASTIHITTSESRINTTAWEATAVDOAOOSILIEEHHCPYTNICMTTIITUEVTHO",
"MCDAIERIBROOEDGEUDUIARRLEEENSPEEEIERENCFOETRDVRATSETRSCINLNMNEOENATOOLYBNURLAOOTSEAMEEAAEASECCEKRMCNNECRMSFPELSRRIEXTDHCSRNUDPRA",
"AFTTEEGDOCISGEAHHRKNIEVTNNASEHOHBTTSNOEHAKTOADTHAETSHDIMEICMULOERHEEINUEOAINOTCSETNDSIOEBMVAKGWYYADADNOWTTUNTNMEEBNDAYADRSANSHRN",
"EIERTDMHNMONAENIADLUDLGSALNISIHGLCTBARRNLEEHAREAPONEMTNWBSWEOLWYNHITOGOYEOEETLETBTDLEAHAOWOSTTEIOONRLOSETRINHAHHHWILSMPRYTIEWTSE",
"HLABRSEOIAERRVDTNYAGAROWTILWNOGNAIINURGBROTHGETNDHIWODHINFIGONORRTFOOEHAAGWRESLRYSFENAREIEHLEOENUNRESONRTTWRAANISHTCANITPWMWWTVD",
"STLIKNRTETDEEEIEHFTEPBCTHEAIALIWNTOROTRSIHHUIEAININNRRSEOTNNNETOODNHETSGPLTEXUIHILTIDAUSFHNTOTREITEGMHANSAHGGRRYUIEBMADFRCSEDDTB",
"ILHHHRTDFPBEGOREEIXLWKTWCEDEIABTSGOOANIUFRDHAOFDSNCNHDTWVNTLERLNERASRAAUBVSSEEUPCSTENYDMELOWFTEEBAUEOISEAJOIDIOEEMNPOLGAANIOSTPT",
"ALTANSGHIHRIESSUIONEINKIEGEMYMPTRELTOUBMEFLRHUNBUISOABTDWSSODEEAFDIAIULUDEANSEGYYANPOPREOWOCAOEARVARNHRSLELORWRYTDYTNFFKTYWUTAAO",
);
# some datas in mem.
my @SCTCHARS;
my %gram_table;
sub init_SCTCHARS
{
for(my $j=0; $j < scalar(@SCT); $j++)
{
$SCTCHARS[$j] = [];
my $s = $SCT[$j];
for(my $i = 0; $i < length ($s); $i++)
{
$SCTCHARS[$j][$i] = substr($s, $i, 1);
}
}
}
sub load_gramtable
{
print STDERR "loading gram table ...";
open CSV, "<", $GRAM_TABLE_FILE;
while(<CSV>)
{
/^([^,]+),([^,]+),/;
$gram_table{uc($1)} = $2;
#print STDERR $1 . "\t" . $2 . "\n";
}
close CSV;
print STDERR " done\n";
}
sub trial_loop;
sub calc_obs_freq;
sub take_only_best;
# main
init_SCTCHARS();
load_gramtable();
#trial_loop;
# my @n = get_char_pattern(125,43,94,75,6,25);
# print join(' ',@n);
# die;
#my $count = calc_obs_freq(3,2,1);
#print $count, "\n";
#$count = calc_obs_freq(125,43,94);
#print $count, "\n";
my $c=0; # simple count for number of permutations.
# and now, calc best permutations
{
# prepare permutation candidates.
my @num;
for (my $i=0; $i < 128; $i++)
{
$num[$i] = $i;
}
my $combinator = Math::Combinatorics->new(count => $GramN, data => [@num], );
while (my @t = $combinator->next_combination)
{
#print STDERR join('-', @t), "\n";
my @sel = permute(@t);
foreach(@sel)
{
# now we have "one candidate" here in @sel.
# get frequency count and print it on STDOUT
my $count = calc_obs_freq(@{$_});
#my $count = calc_obs_logfreq(@{$_});
print join('-', @{$_}), "\t\t", $count, "\n";
$c++;
}
}
}
print STDERR "In total $c len-n patterns have been tried\n";
# gets an array of "position num"
# and returns 10 char-patterns, accoring to the locations.
# (returns 10 strings, array).
sub get_char_pattern
{
my @pos = @_;
my @result;
for (my $i=0; $i < @SCTCHARS; $i++)
{
my $obs="";
for(@pos)
{
$obs .= $SCTCHARS[$i][$_];
}
push @result, $obs;
}
return @result;
}
sub calc_obs_logfreq
{
my @pos = @_;
my @patterns = get_char_pattern(@pos);
my $count=0;
for my $s (@patterns)
{
# print STDERR $s, "\t";
if (exists $gram_table{$s})
{
$count += log($gram_table{$s});
}
# print STDERR $gram_table{$s}, "\n";
}
return $count;
}
sub calc_obs_freq
{
my @pos = @_;
my @patterns = get_char_pattern(@pos);
my $count=0;
for my $s (@patterns)
{
# print STDERR $s, "\t";
if (exists $gram_table{$s})
{
$count += $gram_table{$s};
}
# print STDERR $gram_table{$s}, "\n";
}
return $count;
}