Skip to content

Commit

Permalink
hunspell/fix: not check emoji or vulgar fractions
Browse files Browse the repository at this point in the history
  • Loading branch information
laysauchoa committed Feb 21, 2021
1 parent 6bf3802 commit 5257966
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ toml = "0.5"
# for the config file
walkdir = "2"
unic-emoji-char = "0.9.0"
unicode_names2 = "0.4.0"

# config parsing, must be independent of features

Expand Down
83 changes: 70 additions & 13 deletions src/checker/hunspell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,31 @@ pub struct HunspellWrapper(pub Arc<Hunspell>);
unsafe impl Send for HunspellWrapper {}
unsafe impl Sync for HunspellWrapper {}

// The value is `true` if all characters are emoji
pub fn is_emoji(word: String) -> bool {
if !word.is_empty() {
return word.clone().chars().all(unic_emoji_char::is_emoji);
// The value is `true` if string is made of emoji's
// or unicode `VULGAR FRACTION`
pub fn vulgar_fraction_or_emoji(word: String) -> bool {
if word.is_empty() {
return false;
}
for c in word.clone().chars() {
if is_vulgar_fraction(c) || unic_emoji_char::is_emoji(c) {
continue;
} else {
return false;
}
}
return true;
}

// The value is `true` if given character is a vulgar fraction
pub fn is_vulgar_fraction(character: char) -> bool {
let unicode_name = unicode_names2::name(character).map(|n| n.to_string());
let vulgar = "VULGAR FRACTION";
match unicode_name.as_deref() {
Some(s) if s.starts_with(vulgar) => true,
_ => false,
None => false,
}
false
}

pub struct HunspellChecker;
Expand Down Expand Up @@ -251,9 +270,9 @@ fn obtain_suggestions<'s>(
.filter(|x| x.len() > 1) // single char suggestions tend to be useless
.collect::<Vec<_>>();

// strings made of emojis
if is_emoji(word.clone()) {
trace!(target: "quirks", "Found emoji character, treating {} as ok", &word);
// strings made of vulgar fraction or emoji
if vulgar_fraction_or_emoji(word.clone()) {
trace!(target: "quirks", "Found emoji or vulgar fraction character, treating {} as ok", &word);
return;
}
let chars: Vec<char> = word.clone().chars().collect();
Expand Down Expand Up @@ -289,23 +308,61 @@ fn obtain_suggestions<'s>(
}
}

macro_rules! parametrized_is_emoji {
macro_rules! parametrized_vulgar_fraction_or_emoji {
($($name:ident: $value:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $value;
assert_eq!(expected, is_emoji(input));
assert_eq!(expected, vulgar_fraction_or_emoji(input));
}
)*
}
}

parametrized_is_emoji! {
parametrized_vulgar_fraction_or_emoji! {
empty: ("".to_string(), false),
emojis: ("🐍🤗🦀".to_string(), true),
contains_emojis: ("contains emoji 🦀".to_string(), false),
contains_only_unicode: ("⅔".to_string(), false),
contains_emoji_and_unicode: ("🐍🤗🦀⅔".to_string(), false),
contains_only_unicode: ("⅔⅔⅔".to_string(), true),
contains_emoji_and_unicode: ("🐍🤗🦀⅔".to_string(), true),
no_emojis: ("no emoji string".to_string(), false),
}

macro_rules! parametrized_is_vulgar_fraction {
($($name:ident: $value:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $value;
assert_eq!(expected, is_vulgar_fraction(input));
}
)*
}
}

parametrized_is_vulgar_fraction! {
is_emoji: ('🦀', false),
is_latin_letter: ('a', false),
vulgar_fraction_one_quarter: ('¼', true),
vulgar_fraction_one_half: ('½', true),
vulgar_fraction_three_quarters: ('¾', true),
vulgar_fraction_one_seventh: ('⅐', true),
vulgar_fraction_one_ninth: ('⅑', true),
vulgar_fraction_one_tenth: ('⅒', true),
vulgar_fraction_one_third: ('⅓', true),
vulgar_fraction_two_thirds: ('⅔', true),
vulgar_fraction_one_fifth: ('⅕', true),
vulgar_fraction_two_fifth: ('⅖', true),
vulgar_fraction_three_fifths: ('⅗', true),
vulgar_fraction_four_fifths: ( '⅘', true),
vulgar_fraction_one_sixth: ('⅙', true),
vulgar_fraction_five_sixths: ('⅚', true),
vulgar_fraction_one_eighth: ('⅛', true),
vulgar_fraction_three_eighths: ('⅜', true),
vulgar_fraction_five_eights: ('⅝', true),
vulgar_fraction_five_eighths: ('⅝', true),
vulgar_fraction_seven_eighths: ('⅞', true),
vulgar_fraction_zero_thirds: ('↉', true),

}

0 comments on commit 5257966

Please sign in to comment.