Skip to content

Instantly share code, notes, and snippets.

@lukasvermeer
Last active December 28, 2015 21:19
Show Gist options
  • Save lukasvermeer/7563457 to your computer and use it in GitHub Desktop.
Save lukasvermeer/7563457 to your computer and use it in GitHub Desktop.
Quick and dirty hack to find alternative sentences that can be created by adding a single letter to an input sentence. Probably only works on Mac (because it expects a dictionary file in a specific place).
use strict;
use warnings;
die "Malfunction. Need input." unless $ARGV[0];
open(W, "/usr/share/dict/words");
my @w;
while (my $line = <W>) {
chomp($line);
push(@w, lc($line));
}
close(W);
print("*** searching ***\n");
my $before = "";
for my $i (0..scalar @ARGV-1) {
for my $j (0..length($ARGV[$i])) {
my $r = lc($ARGV[$i]);
substr($r, $j, 0) = '.';
my @w = ( grep { /^${r}$/ } @w );
if (scalar @w > 0) {
my $after = lc(join(" ", @ARGV[$i+1..scalar @ARGV-1]));
for my $k (0..scalar @w-1) {
print($before . $w[$k] . " " . $after . "\n");
}
}
}
$before .= lc($ARGV[$i]) . " ";
}
@creaktive
Copy link

aspell-based wordlists can be expanded to very big word lists that encompass many variations of the same word:

$ unmunch en_US.dic en_US.aff | sort -u > ~/words
$ wc -l /usr/share/dict/words ~/words
  235886 /usr/share/dict/words
 1583425 /Users/stas/words

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment