<commit_before>package RoboBot::Plugin::Factoid;

use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

extends 'RoboBot::Plugin';

has '+name' => (
    default => 'Factoid',
);

has '+description' => (
    default => 'Allows for saving and retrieving small snippets of keyword-based knowledge.',
);

has '+before_hook' => (
    default => 'check_factoids',
);

has '+commands' => (
    default => sub {{
        'add-factoid' => { method      => 'add_factoid',
                           description => 'Adds snippets of information (replacing any that already exist for the given name).',
                           usage       => '"<name>" <... facts ...>' },

        'update-factoid' => { method      => 'add_factoid',
                              description => 'Updates an existing factoid, or creates a new one if there is no existing factoid by the same name.',
                              usage       => '"<name>" <... facts ...>' },

        'remove-factoid' => { method      => 'remove_factoid',
                              description => 'Removes the factoids stored under the given name.',
                              usage       => '"<name>"' },
    }},
);

sub check_factoids {
    my ($self, $message) = @_;

    return if $message->has_expression;

    return unless $message->raw =~ m{^\s*(?:what|who|where|when|why|how)\s+(.+)\?\s*$}i;

    my $query = lc($1);

    my $res = $self->bot->config->db->do(q{
        select id, name, factoid, ts_rank_cd(terms, query) as rank
        from factoids,
            plainto_tsquery(?) query
        where network_id = ?
            and query @@ terms
        order by rank desc
    }, $query, $message->network->id);

    # Silently return the pre-hook if there wasn't at least one matching factoid.
    return unless $res && $res->next;

    $message->response->push(<commit_msg>keywords for factoid lookup<commit_after>package RoboBot::Plugin::Factoid;

use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

extends 'RoboBot::Plugin';

has '+name' => (
    default => 'Factoid',
);

has '+description' => (
    default => 'Allows for saving and retrieving small snippets of keyword-based knowledge.',
);

has '+before_hook' => (
    default => 'check_factoids',
);

has '+commands' => (
    default => sub {{
        'add-factoid' => { method      => 'add_factoid',
                           description => 'Adds snippets of information (replacing any that already exist for the given name).',
                           usage       => '"<name>" <... facts ...>' },

        'update-factoid' => { method      => 'add_factoid',
                              description => 'Updates an existing factoid, or creates a new one if there is no existing factoid by the same name.',
                              usage       => '"<name>" <... facts ...>' },

        'remove-factoid' => { method      => 'remove_factoid',
                              description => 'Removes the factoids stored under the given name.',
                              usage       => '"<name>"' },
    }},
);

sub check_factoids {
    my ($self, $message) = @_;

    return if $message->has_expression;

    return unless $message->raw =~ m{^\s*(?:what|who|where|when|why|how|is|are)\s+(.+)\?\s*$}i;

    my $query = lc($1);

    my $res = $self->bot->config->db->do(q{
        select id, name, factoid, ts_rank_cd(terms, query) as rank
        from factoids,
            plainto_tsquery(?) query
        where network_id = ?
            and query @@ terms
        order by rank desc
    }, $query, $message->network->id);

    # Silently return the pre-hook if there wasn't at least one matching factoid.
    return unless $res && $res->next;

    $message->response->push(
{'fraction_non_alphanumeric': 0.15126498002663116, 'fraction_numerical': 0.0021304926764314247, 'mean_word_length': 2.0939044481054365, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4901744', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1081, 'n_words': 312}