-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
emojis are now replaced by images ! (except for unicode emoji)
- Loading branch information
1 parent
7994b9e
commit 5a154b5
Showing
8 changed files
with
596 additions
and
75 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use image::DynamicImage; | ||
use moka::sync::Cache; | ||
use reqwest::get; | ||
use anyhow::Result; | ||
|
||
pub struct DiscordEmojis { | ||
emojis: Cache<String, DynamicImage> | ||
} | ||
|
||
impl DiscordEmojis { | ||
pub fn new(cap: usize) -> Self { | ||
Self { | ||
emojis: Cache::new(cap as u64) | ||
} | ||
} | ||
|
||
pub async fn get(&self, id: &str) -> Result<DynamicImage> { | ||
if let Some(img) = self.emojis.get(id) { | ||
Ok(img.clone()) | ||
} else { | ||
let url = format!("https://cdn.discordapp.com/emojis/{}.webp", id); | ||
let img_bytes = get(url).await?.bytes().await?; | ||
let image = image::load_from_memory(&img_bytes)?; | ||
self.emojis.insert(id.to_string(), image.clone()); | ||
Ok(image) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod idiom; | ||
mod top_freqs; | ||
mod text_utils; | ||
pub use idiom::Idioms; | ||
pub use idiom::tokenize; | ||
pub use text_utils::tokenize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::collections::HashMap; | ||
use itertools::Itertools; | ||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
const OPENING_PUNCT: &[char] = &['(', '[', '{', '\'', '"']; | ||
const CLOSING_PUNCT: &[char] = &[':', '.', '?', '!', '`', ';', ',', ')', ']', '}', '\'', '"']; | ||
|
||
lazy_static! { | ||
static ref RE_TOKEN: Regex = Regex::new(r"\S+").unwrap(); | ||
} | ||
|
||
fn is_capitalized(token: &str) -> bool { | ||
!token.chars().skip(1).all(|c| c.is_lowercase()) | ||
} | ||
|
||
fn smart_lower(token: &str) -> String { | ||
if is_capitalized(token) { | ||
token.to_lowercase() | ||
} else { | ||
token.to_string() | ||
} | ||
} | ||
|
||
fn trim(token: &str) -> &str { | ||
token.trim_start_matches(OPENING_PUNCT) | ||
.trim_end_matches(CLOSING_PUNCT) | ||
} | ||
|
||
pub fn tokenize(text: String) -> Vec<String> { | ||
RE_TOKEN.find_iter(&text) | ||
.map(|token| smart_lower(trim(token.as_str()))) | ||
.collect_vec() | ||
} | ||
|
||
pub(crate) fn counts(tokens: Vec<String>) -> Vec<(String, f32)> { | ||
let mut counts: HashMap<String, usize> = HashMap::new(); | ||
for token in tokens { | ||
*counts.entry(token.as_str().to_string()).or_default() += 1; | ||
} | ||
counts.into_iter().map(|(k, v)| (k, v as f32)).collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod idiom; | ||
mod discord_emojis; | ||
mod handler; | ||
mod handler_util; | ||
mod handle_events; | ||
|