2022-08-24 23:37:02 +02:00
|
|
|
# File: WordMorph.pm
|
|
|
|
#
|
|
|
|
# Purpose: Word morph game. Solve a path between two words by changing one
|
|
|
|
# letter at a time. love > shot = love > lose > lost > loot > soot > shot.
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Pragmatic Software <pragma78@gmail.com>
|
2022-08-24 23:37:02 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package PBot::Plugin::WordMorph;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
|
|
|
|
use Storable;
|
|
|
|
use Text::Levenshtein::XS 'distance';
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub initialize($self, %conf) {
|
2022-08-24 23:37:02 +02:00
|
|
|
$self->{pbot}->{commands}->add(
|
|
|
|
name => 'wordmorph',
|
2022-08-25 08:11:47 +02:00
|
|
|
help => 'Word Morph game! Solve a path between two words by changing one letter at a time: love > shot = love > lose > lost > loot > soot > shot.',
|
2022-08-24 23:37:02 +02:00
|
|
|
subref => sub { $self->wordmorph(@_) },
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->{db_path} = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/wordmorph.db';
|
|
|
|
|
|
|
|
$self->{db} = eval { $self->load_db() }
|
|
|
|
or $self->{pbot}->{logger}->log($@);
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub unload($self) {
|
2022-08-24 23:37:02 +02:00
|
|
|
$self->{pbot}->{commands}->remove('wordmorph');
|
|
|
|
}
|
|
|
|
|
|
|
|
use constant {
|
2024-02-23 03:56:52 +01:00
|
|
|
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> (<word2> | <integer steps>) | solve <solution> | hint [from direction] | check <word> | neighbors <word> | search <regex> | show | giveup',
|
2022-08-24 23:37:02 +02:00
|
|
|
NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.",
|
|
|
|
DB_UNAVAILABLE => "Word morph database not available.",
|
2022-09-01 07:54:54 +02:00
|
|
|
LEFT => 0,
|
|
|
|
RIGHT => 1,
|
2022-10-01 19:17:27 +02:00
|
|
|
MIN_STEPS => 2,
|
|
|
|
MAX_STEPS => 8,
|
2024-01-27 04:01:42 +01:00
|
|
|
DEFAULT_STEPS => 3,
|
2022-10-01 19:17:27 +02:00
|
|
|
MIN_WORD_LENGTH => 3,
|
|
|
|
MAX_WORD_LENGTH => 7,
|
2022-08-24 23:37:02 +02:00
|
|
|
};
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub wordmorph($self, $context) {
|
2022-08-24 23:37:02 +02:00
|
|
|
my @args = $self->{pbot}->{interpreter}->split_line($context->{arguments});
|
|
|
|
|
|
|
|
my $command = shift @args;
|
|
|
|
|
|
|
|
if (not length $command) {
|
|
|
|
return USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $channel = $context->{from};
|
|
|
|
|
|
|
|
given ($command) {
|
2022-09-08 02:52:48 +02:00
|
|
|
when ('neighbors') {
|
|
|
|
if (!@args || @args > 1) {
|
|
|
|
return 'Usage: wordmorph neighbors <word>; list the neighbors of a given word';
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
2022-10-02 06:32:25 +02:00
|
|
|
if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) {
|
|
|
|
return $err;
|
2022-09-08 02:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my @neighbors = @{$self->{db}->{length $args[0]}->{$args[0]}};
|
|
|
|
my $count = @neighbors;
|
|
|
|
return "`$args[0]` has $count neighbor" . ($count != 1 ? 's' : '') . ": " . join(', ', sort @neighbors);
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:02:07 +02:00
|
|
|
when ('check') {
|
|
|
|
if (!@args || @args > 1) {
|
|
|
|
return 'Usage: wordmorph check <word>; check if a word exists in the Word Morph database';
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
2022-10-02 06:32:25 +02:00
|
|
|
if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) {
|
|
|
|
return $err;
|
2022-09-06 17:02:07 +02:00
|
|
|
}
|
2022-10-02 06:32:25 +02:00
|
|
|
|
|
|
|
return "Yes, `$args[0]` is a word I know.";
|
2022-09-06 17:02:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 23:37:02 +02:00
|
|
|
when ('hint') {
|
|
|
|
if (not defined $self->{$channel}->{morph}) {
|
|
|
|
return NO_MORPH_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:27:31 +02:00
|
|
|
if (@args > 1) {
|
|
|
|
return 'Usage: wordmorph hint [from direction]; from direction can be `left` or `right`';
|
|
|
|
}
|
|
|
|
|
|
|
|
my $direction;
|
2022-08-24 23:37:02 +02:00
|
|
|
|
2022-09-01 03:27:31 +02:00
|
|
|
if (@args == 0) {
|
2022-09-01 07:54:54 +02:00
|
|
|
$direction = LEFT;
|
2022-09-01 03:27:31 +02:00
|
|
|
} elsif ($args[0] eq 'left' || $args[0] eq 'l') {
|
2022-09-01 07:54:54 +02:00
|
|
|
$direction = LEFT;
|
2022-09-01 03:27:31 +02:00
|
|
|
} elsif ($args[0] eq 'right' || $args[0] eq 'r') {
|
2022-09-01 07:54:54 +02:00
|
|
|
$direction = RIGHT;
|
2022-09-01 03:27:31 +02:00
|
|
|
} else {
|
|
|
|
return "Unknown direction `$args[0]`; usage: wordmorph hint [from direction]; from direction can be `left` or `right`";
|
|
|
|
}
|
|
|
|
|
2022-09-05 01:47:43 +02:00
|
|
|
my $morph = $self->{$channel}->{morph};
|
|
|
|
my $end = $#$morph;
|
2022-09-01 08:09:10 +02:00
|
|
|
|
2022-09-01 07:54:54 +02:00
|
|
|
if ($direction == LEFT) {
|
2022-09-01 03:27:31 +02:00
|
|
|
$self->{$channel}->{hintL}++;
|
|
|
|
|
2022-09-01 08:09:10 +02:00
|
|
|
if ($self->{$channel}->{hintL} > $end) {
|
|
|
|
$self->{$channel}->{hintL} = $end;
|
2022-09-01 03:27:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$self->{$channel}->{hintR}--;
|
|
|
|
|
|
|
|
if ($self->{$channel}->{hintR} < 0) {
|
|
|
|
$self->{$channel}->{hintR} = 0;
|
|
|
|
}
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my @hints;
|
2022-09-01 03:27:31 +02:00
|
|
|
|
2022-09-05 01:47:43 +02:00
|
|
|
$hints[0] = $morph->[0];
|
|
|
|
$hints[$end] = $morph->[$end];
|
2022-09-01 03:27:31 +02:00
|
|
|
|
|
|
|
for (my $i = 1; $i < $self->{$channel}->{hintL}; $i++) {
|
2022-09-05 01:47:43 +02:00
|
|
|
my $word1 = $morph->[$i - 1];
|
|
|
|
my $word2 = $morph->[$i];
|
2022-09-08 16:19:43 +02:00
|
|
|
$hints[$i] = form_hint($word1, $word2);
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 01:47:43 +02:00
|
|
|
my $blank_hint = '_' x length $morph->[0];
|
2022-09-01 03:27:31 +02:00
|
|
|
for (my $i = $self->{$channel}->{hintL}; $i < $self->{$channel}->{hintR} + 1; $i++) {
|
|
|
|
$hints[$i] = $blank_hint;
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 08:09:10 +02:00
|
|
|
for (my $i = $end - 1; $i > $self->{$channel}->{hintR}; $i--) {
|
2022-09-05 01:47:43 +02:00
|
|
|
my $word1 = $morph->[$i];
|
|
|
|
my $word2 = $morph->[$i + 1];
|
2022-09-08 16:19:43 +02:00
|
|
|
$hints[$i] = form_hint($word1, $word2);
|
2022-09-01 03:27:31 +02:00
|
|
|
}
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
return "Hint: " . join(' > ', @hints);
|
|
|
|
}
|
|
|
|
|
|
|
|
when ('show') {
|
|
|
|
if (not defined $self->{$channel}->{morph}) {
|
|
|
|
return NO_MORPH_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Current word morph: " . $self->show_morph_with_blanks($channel) . " (Fill in the blanks)";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ('giveup') {
|
|
|
|
if (not defined $self->{$channel}->{morph}) {
|
|
|
|
return NO_MORPH_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $solution = join ' > ', @{$self->{$channel}->{morph}};
|
|
|
|
$self->{$channel}->{morph} = undef;
|
|
|
|
return "The solution was $solution. Better luck next time.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ('start') {
|
|
|
|
if (@args > 2) {
|
|
|
|
return "Invalid arguments; Usage: wordmorph start [steps to solve [word length]]";
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:17:27 +02:00
|
|
|
my $steps = DEFAULT_STEPS;
|
2022-08-24 23:37:02 +02:00
|
|
|
my $length = undef;
|
|
|
|
|
|
|
|
if (defined $args[0]) {
|
2022-10-01 19:17:27 +02:00
|
|
|
if ($args[0] !~ m/^[0-9]+$/ || $args[0] < MIN_STEPS || $args[0] > MAX_STEPS) {
|
|
|
|
return "Invalid number of steps `$args[0]`; must be integer >= ".MIN_STEPS." and <= ".MAX_STEPS.".";
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$steps = $args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $args[1]) {
|
2022-10-01 19:17:27 +02:00
|
|
|
if ($args[1] !~ m/^[0-9]+$/ || $args[1] < MIN_WORD_LENGTH || $args[1] > MAX_WORD_LENGTH) {
|
|
|
|
return "Invalid word length `$args[1]`; must be integer >= ".MIN_WORD_LENGTH." and <= ".MAX_WORD_LENGTH.".";
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$length = $args[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
2022-10-01 19:17:27 +02:00
|
|
|
my $attempts = 1000;
|
2022-09-08 16:19:43 +02:00
|
|
|
my $morph;
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
while (--$attempts > 0) {
|
2022-09-08 16:19:43 +02:00
|
|
|
$morph = eval {
|
2022-08-24 23:37:02 +02:00
|
|
|
$self->make_morph_by_steps($self->{db}, $steps + 2, $length)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (my $err = $@) {
|
|
|
|
next if $err eq "Too many attempts\n";
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:19:43 +02:00
|
|
|
last if @$morph;
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 19:17:27 +02:00
|
|
|
if (not @$morph) {
|
|
|
|
return "Failed to create Word Morph with given parameters, in reasonable time. Try again.";
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:19:43 +02:00
|
|
|
$self->set_up_new_morph($morph, $channel);
|
2022-08-24 23:37:02 +02:00
|
|
|
return "New word morph: " . $self->show_morph_with_blanks($channel) . " (Fill in the blanks)";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ('custom') {
|
2024-01-27 04:01:42 +01:00
|
|
|
return "Usage: wordmorph custom <word1> (<word2> | <integer steps>)" if @args != 2;
|
2022-08-24 23:37:02 +02:00
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
2022-10-01 19:17:27 +02:00
|
|
|
|
2022-10-02 06:32:25 +02:00
|
|
|
if (my $err = $self->validate_word($args[0], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) {
|
|
|
|
return $err;
|
2022-10-01 19:17:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-27 04:01:42 +01:00
|
|
|
my $morph;
|
|
|
|
|
|
|
|
if ($args[1] =~ /^\d+$/) {
|
|
|
|
my $steps = DEFAULT_STEPS;
|
|
|
|
my $length = length $args[0];
|
|
|
|
|
|
|
|
if ($args[1] < MIN_STEPS || $args[1] > MAX_STEPS) {
|
|
|
|
return "Invalid number of steps `$args[1]`; must be integer >= ".MIN_STEPS." and <= ".MAX_STEPS.".";
|
|
|
|
}
|
|
|
|
|
|
|
|
$steps = $args[1];
|
|
|
|
|
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
|
|
|
my $attempts = 100;
|
|
|
|
|
|
|
|
while (--$attempts > 0) {
|
|
|
|
$morph = eval {
|
|
|
|
$self->make_morph_by_steps($self->{db}, $steps + 2, $length, $args[0])
|
|
|
|
};
|
|
|
|
|
|
|
|
if (my $err = $@) {
|
|
|
|
next if $err eq "Too many attempts\n";
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
|
|
|
last if @$morph;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$morph || !@$morph) {
|
|
|
|
return "Failed to create Word Morph with given parameters, in reasonable time. Try again.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (my $err = $self->validate_word($args[1], MIN_WORD_LENGTH, MAX_WORD_LENGTH)) {
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
|
|
|
|
$morph = eval { makemorph($self->{db}, $args[0], $args[1]) } or return $@;
|
|
|
|
return "Failed to find a path between `$args[0]` and `$args[1]`." if !$morph || !@$morph;
|
2022-10-01 19:17:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 16:19:43 +02:00
|
|
|
$self->set_up_new_morph($morph, $channel);
|
2022-08-24 23:37:02 +02:00
|
|
|
return "New word morph: " . $self->show_morph_with_blanks($channel) . " (Fill in the blanks)";
|
|
|
|
}
|
2024-02-23 03:56:52 +01:00
|
|
|
|
|
|
|
when ('search') {
|
|
|
|
if (not @args) {
|
|
|
|
return "Usage: wordmorph search <regex>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
|
|
|
my @words;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
foreach my $length (keys $self->{db}->%*) {
|
|
|
|
foreach my $word (keys $self->{db}->{$length}->%*) {
|
|
|
|
|
|
|
|
if ($word =~ m/$args[0]/) {
|
|
|
|
push @words, $word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (my $except = $@) {
|
|
|
|
$except =~ s/ at \/home.*$//;
|
|
|
|
return "Error: $except";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not @words) {
|
|
|
|
return "No matching words found.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return scalar @words . (@words == 1 ? ' word' : ' words') . ': ' . join(' ', @words);
|
|
|
|
}
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
when ('solve') {
|
|
|
|
if (not @args) {
|
|
|
|
return "Usage: wordmorph solve <solution>";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $self->{$channel}->{morph}) {
|
|
|
|
return NO_MORPH_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:54 +02:00
|
|
|
return DB_UNAVAILABLE if not $self->{db};
|
|
|
|
|
2022-08-24 23:37:02 +02:00
|
|
|
my @solution = grep { length > 0 } split /\W/, join(' ', @args);
|
|
|
|
|
|
|
|
if ($solution[0] ne $self->{$channel}->{word1}) {
|
|
|
|
unshift @solution, $self->{$channel}->{word1};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($solution[$#solution] ne $self->{$channel}->{word2}) {
|
|
|
|
push @solution, $self->{$channel}->{word2};
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:27:31 +02:00
|
|
|
my $last_word = $solution[0];
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
if (not exists $self->{db}->{length $last_word}->{$last_word}) {
|
|
|
|
return "I do not know this word `$last_word`.";
|
|
|
|
}
|
|
|
|
|
2022-09-01 03:27:31 +02:00
|
|
|
my $length = length $last_word;
|
|
|
|
|
|
|
|
for (my $i = 1; $i < @solution; $i++) {
|
2022-09-01 07:54:54 +02:00
|
|
|
my $word = $solution[$i];
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
if (not exists $self->{db}->{length $word}->{$word}) {
|
|
|
|
return "I do not know this word `$word`.";
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:54 +02:00
|
|
|
if (length($word) != $length || distance($word, $last_word) != 1) {
|
2022-08-24 23:37:02 +02:00
|
|
|
return "Wrong. `$word` does not follow from `$last_word`.";
|
|
|
|
}
|
|
|
|
|
|
|
|
$last_word = $word;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $expected_steps = @{$self->{$channel}->{morph}};
|
|
|
|
|
|
|
|
if (@solution > $expected_steps) {
|
|
|
|
return "Almost! " . join(' > ', @solution) . " is too long.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (@solution == $expected_steps) {
|
|
|
|
return "Correct! " . join(' > ', @solution);
|
|
|
|
}
|
|
|
|
|
2022-09-01 07:54:54 +02:00
|
|
|
# this should never happen ... but just in case
|
|
|
|
return "Correct! " . join(' > ', @solution) . " is shorter than the expected solution. Congratulations!";
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default {
|
|
|
|
return "Unknown command `$command`; " . USAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub load_db($self) {
|
2022-08-24 23:37:02 +02:00
|
|
|
if (not -e $self->{db_path}) {
|
2022-09-05 01:47:43 +02:00
|
|
|
die "Word morph database not available; run `/misc/wordmorph/wordmorph-mkdb` to create it.\n";
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return retrieve($self->{db_path});
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub show_morph_with_blanks($self, $channel) {
|
2022-08-24 23:37:02 +02:00
|
|
|
my @middle;
|
|
|
|
for (1 .. @{$self->{$channel}->{morph}} - 2) {
|
|
|
|
push @middle, '_' x length $self->{$channel}->{word1};
|
|
|
|
}
|
|
|
|
|
|
|
|
return "$self->{$channel}->{word1} > " . join(' > ', @middle) . " > $self->{$channel}->{word2}";
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub set_up_new_morph($self, $morph, $channel) {
|
2022-09-08 16:19:43 +02:00
|
|
|
$self->{$channel}->{morph} = $morph;
|
|
|
|
$self->{$channel}->{word1} = $morph->[0];
|
|
|
|
$self->{$channel}->{word2} = $morph->[$#$morph];
|
2022-09-01 03:27:31 +02:00
|
|
|
$self->{$channel}->{hintL} = 1;
|
2022-09-08 16:19:43 +02:00
|
|
|
$self->{$channel}->{hintR} = $#$morph - 1;
|
2022-08-24 23:37:02 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub form_hint($word1, $word2) {
|
2022-09-01 07:54:54 +02:00
|
|
|
my $hint = '';
|
|
|
|
|
|
|
|
for (0 .. length $word1) {
|
|
|
|
if (substr($word1, $_, 1) eq substr($word2, $_, 1)) {
|
|
|
|
$hint .= substr($word1, $_, 1);
|
|
|
|
} else {
|
|
|
|
$hint .= "?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hint;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub validate_word($self, $word, $min, $max) {
|
2022-10-02 06:32:25 +02:00
|
|
|
my $len = length $word;
|
|
|
|
|
|
|
|
if ($len < $min) {
|
|
|
|
return "`$word` is too short; minimum word length is $min.";
|
|
|
|
} elsif ($len > $max) {
|
|
|
|
return "`$word` is too long, maximum word length is $max.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not exists $self->{db}->{$len}->{$word}) {
|
|
|
|
return "I do not know this word `$word`.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub compare_suffix($word1, $word2) {
|
2022-09-05 01:47:43 +02:00
|
|
|
my $length = 0;
|
|
|
|
|
|
|
|
for (my $i = length($word1) - 1; $i >= 0; --$i) {
|
|
|
|
if (substr($word1, $i, 1) eq substr($word2, $i, 1)) {
|
|
|
|
$length++;
|
|
|
|
} else {
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $length;
|
|
|
|
}
|
|
|
|
|
2024-01-27 04:01:42 +01:00
|
|
|
sub make_morph_by_steps($self, $db, $steps, $length, $word1 = undef) {
|
2022-09-05 01:47:43 +02:00
|
|
|
$length //= int(rand(3)) + 5;
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
my @words = keys %{$db->{$length}};
|
2024-01-27 04:01:42 +01:00
|
|
|
my $word = $word1 // $words[rand $#words];
|
2022-08-24 23:37:02 +02:00
|
|
|
my $morph = [];
|
|
|
|
|
|
|
|
push @$morph, $word;
|
|
|
|
|
2024-01-27 04:01:42 +01:00
|
|
|
my $attempts = 100;
|
2022-08-24 23:37:02 +02:00
|
|
|
|
|
|
|
while (--$attempts > 0) {
|
|
|
|
my @list = @{$db->{$length}->{$word}};
|
|
|
|
|
|
|
|
$word = $list[rand $#list];
|
|
|
|
|
|
|
|
if (grep { $_ eq $word } @$morph) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $try = eval {
|
|
|
|
my $left = $morph->[0];
|
2022-09-05 01:47:43 +02:00
|
|
|
die if compare_suffix($left, $word) >= 2;
|
2022-08-24 23:37:02 +02:00
|
|
|
[transform($left, $word, $db->{length $left})]
|
|
|
|
} or next;
|
|
|
|
|
|
|
|
$morph = [];
|
|
|
|
my $curr_steps = $steps;
|
|
|
|
|
|
|
|
foreach my $word (@$try) {
|
|
|
|
push @$morph, $word;
|
|
|
|
|
|
|
|
if (--$curr_steps <= 0) {
|
|
|
|
return $morph;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
die "Too many attempts\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# the following subs are based on https://www.perlmonks.org/?node_id=558123
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub makemorph($db, $left, $right) {
|
2022-08-24 23:37:02 +02:00
|
|
|
die "The length of given words are not equal.\n" if length($left) != length($right);
|
|
|
|
my $list = $db->{length $left};
|
2022-09-05 01:47:43 +02:00
|
|
|
my $morph = eval { [transform(lc $left, lc $right, $list)] } or die $@;
|
2022-08-24 23:37:02 +02:00
|
|
|
return $morph;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub transform($left, $right, $list) {
|
2022-08-24 23:37:02 +02:00
|
|
|
my (@left, %left, @right, %right); # @left and @right- arrays containing word relation trees: ([foo], [0, foe], [0, fou], [0, 1, fie] ...)
|
|
|
|
# %left and %right - indices containing word offsets in arrays @left and @right
|
|
|
|
|
|
|
|
$left[0] = [$left];
|
|
|
|
$right[0] = [$right];
|
|
|
|
$left{$left} = 0;
|
|
|
|
$right{$right} = 0;
|
|
|
|
|
|
|
|
my $leftstart = 0;
|
|
|
|
my $rightstart = 0;
|
|
|
|
|
|
|
|
my @path;
|
|
|
|
my (%leftstarts, %rightstarts);
|
|
|
|
|
2022-09-08 02:52:48 +02:00
|
|
|
die "I do not know this word `$left`.\n" if not exists $list->{$left};
|
|
|
|
die "I do not know this word `$right`.\n" if not exists $list->{$right};
|
|
|
|
|
2022-08-24 23:37:02 +02:00
|
|
|
SEARCH:
|
|
|
|
for (;;) {
|
|
|
|
my @left_ids = $leftstart..$#left; # choose array of indices of new words
|
|
|
|
$leftstart = $#left;
|
2022-09-08 02:52:48 +02:00
|
|
|
die "Cannot find a path from `$left` to `$right`.\n" if $leftstarts{$leftstart}++ > 2; # finish search if the path could not be found
|
2022-08-24 23:37:02 +02:00
|
|
|
for my $id (@left_ids) { # come through all new words
|
|
|
|
my @prefix = @{$left[$id]};
|
|
|
|
my $searched = pop @prefix;
|
|
|
|
push @prefix, $id;
|
|
|
|
foreach my $word (@{$list->{$searched}}) {
|
|
|
|
next if $left{$word}; # skip words which are already in the tree
|
|
|
|
push @left, [@prefix, $word];
|
|
|
|
$left{$word} = $#left; # add new word to array and index
|
|
|
|
if ( defined(my $r_id = $right{$word}) ) { # and check if the word appears in right index. if yes...
|
|
|
|
my @end = reverse(print_rel($r_id, \@right));
|
|
|
|
shift @end;
|
|
|
|
@path = (print_rel($#left, \@left), @end); # build the path between the words
|
|
|
|
last SEARCH; # and finish the search
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my @right_ids = $rightstart..$#right; # all the same :) the tree is build from both ends to speed up the process
|
|
|
|
$rightstart = $#right;
|
2022-09-08 02:52:48 +02:00
|
|
|
die "Cannot find a path from `$left` to `$right`.\n" if $rightstarts{$rightstart}++ > 2; # finish search if the path could not be found
|
2022-08-24 23:37:02 +02:00
|
|
|
for my $id (@right_ids) { # build right relational table
|
|
|
|
my @prefix = @{$right[$id]};
|
|
|
|
my $searched = pop @prefix;
|
|
|
|
push @prefix, $id;
|
|
|
|
foreach my $word (@{$list->{$searched}}) {
|
|
|
|
next if $right{$word};
|
|
|
|
push @right, [@prefix, $word];
|
|
|
|
$right{$word} = $#right;
|
|
|
|
if ( defined(my $l_id = $left{$word}) ) {
|
|
|
|
my @end = reverse print_rel($#right, \@right);
|
|
|
|
shift @end;
|
|
|
|
@path = (print_rel($l_id, \@left), @end);
|
|
|
|
last SEARCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return @path;
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:01:23 +02:00
|
|
|
sub print_rel($id, $ary) {
|
2022-08-24 23:37:02 +02:00
|
|
|
my @rel = @{$ary->[$id]};
|
|
|
|
my @line;
|
|
|
|
|
|
|
|
push @line, (pop @rel);
|
|
|
|
|
|
|
|
foreach my $ref_id (reverse @rel) {
|
|
|
|
unshift @line, $ary->[$ref_id]->[-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return wantarray ? @line : join "\n", @line, "";
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|