diff --git a/cpanfile b/cpanfile index bce7dd5..734a267 100644 --- a/cpanfile +++ b/cpanfile @@ -20,4 +20,5 @@ requires 'Log::Any::For::Std'; requires 'Mojo::Base'; requires 'Mojo::IOLoop'; requires 'Mojo::UserAgent'; +requires 'Text::Fuzzy'; requires 'URI::URL'; diff --git a/lib/botlib.pm b/lib/botlib.pm index 16996cc..8c4cf45 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -13,6 +13,7 @@ use URI::URL; use JSON::XS qw(decode_json encode_json); use Digest::MD5 qw(md5_base64); use DB_File; +use Text::Fuzzy qw(distance_edits); use conf qw(loadConf); use lat qw(latAnswer); use karma qw(karmaSet karmaGet); @@ -22,7 +23,7 @@ use archeologist qw (dig); use vars qw/$VERSION/; use Exporter qw(import); -our @EXPORT_OK = qw(weather trim randomCommonPhrase command highlight botsleep); +our @EXPORT_OK = qw(weather trim randomCommonPhrase command highlight botsleep fmatch); $VERSION = '1.0'; my $c = loadConf(); @@ -362,6 +363,21 @@ sub botsleep { return; } +sub fmatch { + my $srcphrase = shift; + my $answer = shift; + + my ($distance, undef) = distance_edits ($srcphrase, $answer); + my $srcphraselen = length $srcphrase; + my $distance_max = int ($srcphraselen - ($srcphraselen * (100 - (90 / ($srcphraselen ** 0.5))) * 0.01)); + + if ($distance >= $distance_max) { + return 0; + } else { + return 1; + } +} + 1; # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 74c405d..09c4ade 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -12,7 +12,7 @@ use File::Path qw(mkpath); use Hailo; use Mojo::Base 'Teapot::Bot::Brain'; use conf qw(loadConf); -use botlib qw(weather trim randomCommonPhrase command highlight botsleep); +use botlib qw(weather trim randomCommonPhrase command highlight botsleep fmatch); use karma qw(karmaSet); use fortune qw(fortune fortune_toggle_list); @@ -287,12 +287,7 @@ sub __on_msg { $phrase = lc ($phrase); - if (lc ($reply) eq $phrase) { - $reply = randomCommonPhrase (); - } elsif (lc ($reply) eq substr ($phrase, 0, -1)) { - # in case of trailing dot - $reply = randomCommonPhrase (); - } elsif (substr (lc ($reply), 0, -1) eq $phrase) { + if (fmatch (lc ($reply), $phrase)) { $reply = randomCommonPhrase (); }