Skip to content

Commit

Permalink
better formula for uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed Jan 21, 2023
1 parent 9309bf6 commit ae6b93d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
48 changes: 31 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/handle_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl EventHandler for Handler {

async fn message(&self, _ctx: Context, message: Message) {
if let Some(guild_id) = message.guild_id {
trace!(target: "Wordy", "Read a new message from {}", message.author.name);
self.message(guild_id, message.channel_id, message.author.id, message.content);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serenity::{
use futures::future::join_all;
use lazy_static::lazy_static;
use wordcloud_rs::{Token, WordCloud, Colors};
use crate::{idiom::{Idioms, tokenize}, discord_emojis::DiscordEmojis, handler_util::read_past};
use crate::{idiom::{Idioms, tokenize, self}, discord_emojis::DiscordEmojis, handler_util::read_past};
const READ_PAST: u64 = 1000;
const DAYS: i64 = 100;

Expand Down Expand Up @@ -49,7 +49,11 @@ impl Handler {
}

pub fn message(&self, guild_id: GuildId, channel_id: ChannelId, member_id: UserId, message: String) {
self.idioms.get_mut(&guild_id).unwrap().update(channel_id, member_id, tokenize(message));
if let Some(mut idiom) = self.idioms.get_mut(&guild_id) {
idiom.update(channel_id, member_id, tokenize(message));
} else {
warn!(target: "Wordy", "Guild {} isn't registered yet.", guild_id);
}
}

async fn to_wc_tokens(&self, tokens: Vec<(String, f32)>) -> Vec<(Token, f32)> {
Expand Down
5 changes: 1 addition & 4 deletions src/idiom/idiom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use super::top_freqs::TopFreqs;
use super::text_utils::counts;
const PLACE_VOC_LEN: usize = 500;
const PERSON_VOC_LEN: usize = 200;
const UNIQUENESS: f32 = PERSON_VOC_LEN as f32;
// computed so that (INV-1.)*UNIQUENESS = 1.
const INV: f32 = 1.+1./UNIQUENESS;

pub struct Idioms<P: Hash+Eq, U: Hash+Eq> {
places: HashMap<P, TopFreqs<PLACE_VOC_LEN>>,
Expand Down Expand Up @@ -41,7 +38,7 @@ impl<P: Hash+Eq, U: Hash+Eq> Idioms<P, U> {
}
};
place_voc.add(idx, value);
let inctx_value = (INV-place_voc.get(&idx))*UNIQUENESS;
let inctx_value = (-place_voc.get(&idx)).exp()*100.;
user_voc.add(idx, inctx_value);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/idiom/top_freqs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const AGING: f32 = 0.999;

pub struct TopFreqs<const S: usize, T: Eq + Default = usize> {
pub data: [(T, f32); S],
max: f32,
pub max: f32,
}

impl<const S: usize, T: Eq + Default> TopFreqs<S, T> {
Expand All @@ -14,11 +14,8 @@ impl<const S: usize, T: Eq + Default> TopFreqs<S, T> {
}

pub fn get(&self, entry: &T) -> f32 {
if self.max == 0. {
return 0.;
}
if let Some((_, v)) = self.data.iter().find(|(key, _)| key == entry) {
*v/self.max
*v
} else {
0.
}
Expand Down

0 comments on commit ae6b93d

Please sign in to comment.