3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00
pbot/misc/wordmorph/wordmorph-mkdb

36 lines
844 B
Plaintext
Raw Normal View History

2022-08-26 09:33:41 +02:00
#!/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';