Improve omedeto. Nouns.

This commit is contained in:
AB
2020-12-30 14:30:53 +03:00
parent f111f54606
commit 0812c9e371
2 changed files with 25 additions and 21 deletions

View File

@@ -2,16 +2,16 @@ use crate::db;
use crate::errors::Error; use crate::errors::Error;
use html_escape::encode_text; use html_escape::encode_text;
use markov::Chain; use markov::Chain;
use mystem::Case::Nominative;
use mystem::Gender::Feminine; use mystem::Gender::Feminine;
use mystem::{MyStem, VerbPerson};
use mystem::Tense::{Inpresent, Past}; use mystem::Tense::{Inpresent, Past};
use mystem::VerbPerson::First;
use mystem::{MyStem, VerbPerson};
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use rand::Rng; use rand::Rng;
use regex::Regex; use regex::Regex;
use telegram_bot::prelude::*; use telegram_bot::prelude::*;
use telegram_bot::{Api, Message, ParseMode}; use telegram_bot::{Api, Message, ParseMode};
use mystem::VerbPerson::First;
use mystem::Case::Nominative;
pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> { pub(crate) async fn here(api: Api, message: Message) -> Result<(), Error> {
let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap(); let members: Vec<telegram_bot::User> = db::get_members(message.chat.id()).unwrap();
@@ -138,7 +138,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
nouns.sort(); nouns.sort();
nouns.dedup(); nouns.dedup();
nouns.shuffle(&mut rand::thread_rng()); nouns.shuffle(&mut rand::thread_rng());
debug!("Found {} nouns.", nouns.len()); debug!("Found {} nouns. {:#?}", nouns.len(), nouns);
let mut verbs_p: Vec<String> = all_msg let mut verbs_p: Vec<String> = all_msg
.clone() .clone()
@@ -166,6 +166,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
verbs_p.sort(); verbs_p.sort();
verbs_p.dedup(); verbs_p.dedup();
verbs_p.shuffle(&mut rand::thread_rng()); verbs_p.shuffle(&mut rand::thread_rng());
debug!("Found {} nouns. {:#?}", verbs_p.len(), verbs_p);
let mut verbs_i: Vec<String> = all_msg let mut verbs_i: Vec<String> = all_msg
.clone() .clone()
@@ -180,13 +181,16 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
false false
} else { } else {
match stem[0].lex[0].grammem.part_of_speech { match stem[0].lex[0].grammem.part_of_speech {
mystem::PartOfSpeech::Verb => stem[0].lex[0] mystem::PartOfSpeech::Verb => {
.grammem stem[0].lex[0]
.facts .grammem
.contains(&mystem::Fact::Tense(Inpresent)) && stem[0].lex[0] .facts
.grammem .contains(&mystem::Fact::Tense(Inpresent))
.facts && stem[0].lex[0]
.contains(&mystem::Fact::Person(First)), .grammem
.facts
.contains(&mystem::Fact::Person(First))
}
_ => false, _ => false,
} }
} }
@@ -196,6 +200,7 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
verbs_i.sort(); verbs_i.sort();
verbs_i.dedup(); verbs_i.dedup();
verbs_i.shuffle(&mut rand::thread_rng()); verbs_i.shuffle(&mut rand::thread_rng());
debug!("Found {} nouns. {:#?}", verbs_i.len(), verbs_i);
if nouns.is_empty() { if nouns.is_empty() {
nouns.push(message.from.first_name.to_string()); nouns.push(message.from.first_name.to_string());
@@ -220,21 +225,16 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
let z = mystem let z = mystem
.stemming(message.from.first_name.to_string()) .stemming(message.from.first_name.to_string())
.unwrap(); .unwrap();
debug!("{:#?}", z);
if z.is_empty() { if z.is_empty() {
false false
} else if z[0].lex.is_empty() { } else if z[0].lex.is_empty() {
false false
} else { } else {
if z[0].lex[0] z[0].lex[0]
.grammem .grammem
.facts .facts
.contains(&mystem::Fact::Gender(Feminine)) .contains(&mystem::Fact::Gender(Feminine))
{
true
} else {
false
}
} }
}; };
let result = format!( let result = format!(
@@ -267,7 +267,5 @@ pub(crate) async fn omedeto(api: Api, message: Message, mystem: &mut MyStem) ->
Ok(_) => debug!("/omedeto command sent to {}", message.chat.id()), Ok(_) => debug!("/omedeto command sent to {}", message.chat.id()),
Err(_) => warn!("/omedeto command sent failed to {}", message.chat.id()), Err(_) => warn!("/omedeto command sent failed to {}", message.chat.id()),
} }
// '^я [а-яА-Я]+(-[а-яА-Я]+(_[а-яА-Я]+)*)*$'
Ok(()) Ok(())
} }

View File

@@ -5,6 +5,8 @@ use crate::utils;
use mystem::MyStem; use mystem::MyStem;
use telegram_bot::*; use telegram_bot::*;
//async fn detector()
pub async fn handler( pub async fn handler(
api: Api, api: Api,
message: Message, message: Message,
@@ -24,7 +26,11 @@ pub async fn handler(
data data
); );
db::add_sentence(&message, mystem).await?; db::add_sentence(&message, mystem).await?;
match data.as_str() { let cleaned_message = data
.to_string()
.replace(&format!("@{}", me.username.unwrap()), "");
debug!("{}", cleaned_message);
match cleaned_message.as_str() {
"/here" => commands::here(api, message).await?, "/here" => commands::here(api, message).await?,
"/top" => commands::top(api, message).await?, "/top" => commands::top(api, message).await?,
"/stat" => commands::top(api, message).await?, "/stat" => commands::top(api, message).await?,