-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyan.pl
300 lines (260 loc) · 6.94 KB
/
myan.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
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
300
#myan.pl
#short for "my anagram"
#
#usage: myan.pl (word)
#
#this finds all possible anagrams of a word or sequence of letters, without having to rely on wordsmith.org/anagram. I've drained enough server time from them!
use strict;
use warnings;
my @wordAry = ();
my $anno = "c:/writing/dict/anagram-notes.txt";
my $anaHtm = "c:/writing/dict/anagram-htm.htm";
use Time::HiRes qw(time);
use POSIX qw(strftime);
# options
my $toFile = 1;
my $checkAfter = 1;
my $checkNames = 1;
my $htmOut = 1;
#global initializations
my @isWord;
my $myBase = "";
my $cmdToAn = "";
my $minWords = 2;
my $maxWords = 2;
my $maxAn = 1000;
my $count = 0;
my $printTimer = 1;
my $hashy;
my $calls;
my $curMax;
while ( $count <= $#ARGV ) {
$a = $ARGV[$count];
$b = $ARGV[ $count + 1 ];
for ($a) {
/^-?a$/ && do { `$anno`; exit; };
/^-?t$/ && do { noteCheck(); exit; };
/^-?ma$/ && do { $maxAn = $b; $count += 2; next; };
/^-?nn$/ && do { $checkNames = 0; $count++; next; };
/^-?cn$/ && do { $checkNames = 1; $count++; next; };
/^-?nc$/ && do { $checkAfter = 0; $count++; next; };
/^-?c$/ && do { $checkAfter = 1; $count++; next; };
/^-?np$/ && do { $printTimer = 0; $count++; next; };
/^-?p$/ && do { $printTimer = 1; $count++; next; };
/^-?f$/ && do { $toFile = 1; $count++; next; };
/^-?nf$/ && do { $toFile = 1; $count++; next; };
/^-?h$/ && do { $htmOut = 1; $count++; next; };
/^-?r$/ && do {
my @mma = split( /,/, $b );
$minWords = $mma[0];
$maxWords = $mma[1];
$count++;
next;
};
/^-?m[0-9]$/
&& do { $maxWords = $a; $maxWords =~ s/^-?m//; $count++; next; };
/^-?[0-9]$/ && do { $maxWords = $a; $maxWords =~ s/^-?//; $count++; next; };
/^-?m$/ && do { $maxWords = $b; $count += 2; next; };
/^-?mm$/ && do { $minWords = $maxWords = $b; $count += 2; next; };
/^[a-z]/i && do {
if ($cmdToAn) { die("2 possible words, bailing.\n"); }
else { $cmdToAn = lc($a); $count++; next; }
};
usage();
}
}
if ( !$cmdToAn ) { die("I need a word to anagram."); }
if ( ( length($cmdToAn) > 16 ) && ( $cmdToAn !~ /,/ ) ) {
die("16 chars or fewer please.");
}
if ( $cmdToAn =~ /[^,a-z=\+]/i ) {
print "Wiping out non-letter characters.\n";
$cmdToAn =~ s/[^,a-z]//gi;
}
$cmdToAn = lc($cmdToAn);
if ( $cmdToAn !~ /,/ ) {
$myBase = $cmdToAn;
@wordAry = ($myBase);
}
else {
@wordAry = split( /,/, $cmdToAn );
}
my $lastOne;
for my $inList (@wordAry) {
$myBase = $inList;
if ( $inList =~ /=/ ) {
$myBase =~ s/=//g;
$inList =~ s/=//g;
print "= indicates plurals, trying both.\n";
anfind($myBase);
$myBase = "s$inList";
anfind( $myBase . "s" );
}
if ( $inList =~ /\+/ ) {
$myBase = $inList;
$myBase =~ s/\+/$lastOne/g;
print "Tacking on $inList.\n";
anfind($myBase);
}
else { anfind($myBase); }
$lastOne = $myBase;
}
sub noteCheck {
open( A, "$anno" );
my $toGet = 0;
while ( $a = <A> ) {
if ( $a =~ /^=/ ) { $toGet++; }
}
close(A);
print "TEST RESULTS:10,$toGet,0,<a href=\"$anno\">results</a>\n";
}
sub anfind {
open( A, "c:/writing/dict/brit-1word.txt" ) || die("No dictionary file.");
my $hashy = 0;
print "Trying $_[0]...$myBase\n";
open( C, ">>$anno" );
if ($htmOut) {
open( D, ">$anaHtm" );
print D "<html>\n<body>\n<table border=1>\n<tr>";
}
while ( $a = <A> ) {
$a = lc($a);
chomp($a);
if ( contains( $myBase, $a ) ) {
$isWord[ length($a) ]{$a} = alf($a);
$hashy++;
}
}
if ($printTimer) { print "$hashy total hashes.\n"; }
open( B, ">c:/writing/dict/latest-anagram.txt" );
print B "========$myBase========\n";
my $b4 = time();
my $af;
$curMax = $minWords;
while ( $curMax <= $maxWords ) {
my $prev = time();
findAna( $myBase, "", "", 0 );
$af = time() - $prev;
if ($printTimer) { printf( "\n$curMax Took %.3f seconds.\n", $af ); }
$curMax++;
if ($toFile) {
if ($checkAfter) {
# my $searchIt = `anin.pl $myBase`;
# print C "\n$searchIt";
}
else {
print "\n";
}
if ($checkNames) {
my $getNames = `anan.pl $myBase=`;
print C "\n$getNames";
}
print C "\n=================$myBase\n";
}
}
close(C);
if ($htmOut) {
print D "\n</tr>\n</table>\n</body>\n</html>";
close(D);
`$anaHtm`;
}
if ($printTimer) {
$af = time() - $b4;
printf( "\nTook %.3f seconds.\n", $af );
}
}
sub printUp {
if ($toFile) { print C $_[0]; }
if ( $anaHtm && ( length( $_[0] ) > 1 ) ) {
print D "<td>$_[0]</td>";
print D "</tr>\n<tr>" if ( ( $count > 1 ) && ( $count % 15 == 1 ) );
}
print $_[0];
}
sub findAna
{ # $_[0] = what's left, $_[1] = current, latest word, # of words so far
my $x;
#print B "Parameters $_[0],$_[1],$_[2],$_[3] vs $curMax\n";
if ( $count >= $maxAn ) { return; }
my $anag;
if ( ( !$_[0] ) && ( $_[3] == $curMax ) ) {
$anag = $_[1];
$anag =~ s/^-//g;
if ( ( $count % 10 != 1 ) && ( $count > 1 ) ) { printUp(" "); }
elsif ( $count > 1 ) { printUp("\n"); }
$count++;
printUp("$anag, $_[3]");
print B "$anag\n";
return;
}
if ( $_[3] >= $curMax ) { return; }
my $maxlength;
$maxlength = length( $_[0] );
my $lo2 = length( $_[2] );
if ( ($lo2) && ( $lo2 < $maxlength ) ) {
$maxlength = $lo2;
} # this makes sure that we don't search for cowpies-cow-pies and cowpies-pies-cow
if ( $_[3] == $curMax ) {
for my $x ( keys %{ $isWord[$lo2] } ) {
print B "Poking $x with parameters $_[0],$_[1],$_[2],$_[3]:\n";
if ( alf($x) eq alf( $_[0] ) ) {
$anag = $_[1];
$anag =~ s/^-//g;
if ( $count % 10 > 0 ) { printUp(" "); }
elsif ($count) { printUp("\n"); }
$count++;
printUp("$anag, $_[3]");
print B "FORCE $anag\n";
return;
}
}
return;
}
for ( my $l = $maxlength ; $l >= 1 ; $l-- ) {
for my $x ( sort keys %{ $isWord[$l] } ) {
$calls++;
if ( ( $l != $lo2 )
|| ( $x le $_[2] )
) # this makes sure that we don't search for coward-war-doc and coward-doc-war
{
if ( contains( $_[0], $x ) ) {
findAna( wd( $_[0], $x ), "$_[1]-$x", $x, $_[3] + 1 );
}
}
}
}
}
sub contains {
if ( length( $_[0] ) < length( $_[1] ) ) { return 0; }
my @g = split( //, $_[1] );
my $retVal;
for my $x (@g) {
#print "$_[0] vs $_[1]\n";
my $a = () = $_[0] =~ /$x/g;
my $b = () = $_[1] =~ /$x/g;
#print "$a $x in $_[0], $b $x in $_[1].\n";
if ( $a < $b ) { return 0; }
$retVal += ( $a - $b );
}
return 1;
return $retVal;
}
sub alf {
return lc( $_[0] );
my @x = sort( split( //, $_[0] ) );
return join(@x);
}
sub wd {
my $temp = $_[0];
my @g = split( //, $_[1] );
for my $x (@g) { $temp =~ s/$x//; }
return $temp;
}
sub usage {
print <<EOT;
You need to have a CSV of anagrams.
+ = tack on to previous
= = do plural as well
EOT
exit;
}