3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 10:59:29 +01:00
pbot/misc/wordmorph/wordmorph-mkdb
Pragmatic Software 0edfee6d15 Update WordMorph
2022-08-26 00:33:41 -07:00

36 lines
844 B
Perl
Executable File

#!/usr/bin/perl
use warnings;
use strict;
use Storable;
use Text::Levenshtein::XS 'distance';
die "usage: $0 <wordlist file>\n" unless @ARGV == 1;
#thanks to Limbic~Region, http://perlmonks.org/index.pl?node_id=180961
open (my $wordlist, '<', $ARGV[0]) or die "Unable to open `$ARGV[0]` for reading: $!\n";
my ($db, %data);
while (<$wordlist>) {
chomp;
next if $_ !~ /^[a-z]{3,7}$/;
push @{$data{length()}}, $_;
}
for my $len (keys %data) {
my $end = $#{$data{$len}};
for my $i (0 .. $end - 1) {
my $word = $data{$len}[$i];
for my $j ($i + 1 .. $end) {
my $test = $data{$len}[$j];
if (distance($word, $test) == 1) {
push @{$db->{$len}{$word}}, $test;
push @{$db->{$len}{$test}}, $word;
}
}
}
}
store $db, 'wordmorph.db';