Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Remove unsued code blocks, functions and consts
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Jun 10, 2024
1 parent c7d2d81 commit 40e91de
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions fry_normalize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
Expand Down Expand Up @@ -133,12 +132,22 @@ macro_rules! regex_m {
}

#[derive(Debug, PartialEq)]
pub enum TaggedWord {
enum TaggedWord {
Word(String),
Number(String),
Symbol(String),
Abbr(String),
}
impl Into<String> for TaggedWord {
fn into(self) -> String {
match self {
TaggedWord::Word(word) => word,
TaggedWord::Number(word) => word,
TaggedWord::Symbol(word) => word,
TaggedWord::Abbr(word) => word,
}
}
}
impl TaggedWord {
fn from_str<S: AsRef<str>>(s: S) -> Self {
let s: &str = s.as_ref();
Expand All @@ -149,21 +158,14 @@ impl TaggedWord {
}
TaggedWord::Word(s.to_string())
}
fn into_plain_word(self) -> Self {
match self {
Self::Word(word) => Self::Word(word),
Self::Number(word) => Self::Word(word),
Self::Symbol(word) => Self::Word(word),
Self::Abbr(word) => Self::Word(word),
}
}
fn normalize(self) -> Self {
match self {
Self::Word(word) => Self::Word(word),
Self::Number(word) => normalize_number(&word).unwrap_or(Self::Word(word)),
Self::Symbol(word) => normalize_symbol(&word).unwrap_or(Self::Word(word)),
Self::Abbr(word) => normalize_abbr(&word).unwrap_or(Self::Word(word)),
Self::Word(ref word) => normalize_word(&word),
Self::Number(ref word) => normalize_number(&word),
Self::Symbol(ref word) => normalize_symbol(&word),
Self::Abbr(ref word) => normalize_abbr(&word),
}
.unwrap_or(Self::Word(self.into()))
}
fn to_string(self) -> String {
match self {
Expand All @@ -177,7 +179,6 @@ impl TaggedWord {

const NUMBER_REGEX_STR: &str = "\\$?[0-9,]+((st)|(nd)|(th))?";
const NUMBER_REGEX: LazyCell<Regex> = LazyCell::new(|| Regex::new(NUMBER_REGEX_STR).unwrap());
const WORD_REGEX: &str = "[a-zA-Z]?[a-z']+";
// All uppercasae words are symbols and are spoken letter by letter
const SYMBOL_REGEX_STR: &str = "[A-Z.]{2,}";
const SYMBOL_REGEX: LazyCell<Regex> = LazyCell::new(|| Regex::new(SYMBOL_REGEX_STR).unwrap());
Expand All @@ -191,11 +192,14 @@ const ABBR_DICT: LazyCell<BTreeMap<&'static str, &'static str>> = LazyCell::new(
abbr_dict
});

fn tag_words(input: &str) -> Vec<TaggedWord> {
pub fn normalize(input: &str) -> String {
input
.split_whitespace()
.map(|word| TaggedWord::from_str(word))
.collect::<Vec<TaggedWord>>()
.map(TaggedWord::from_str)
.map(|s| s.normalize())
.map(|n| n.to_string())
.collect::<Vec<String>>()
.join(" ")
}

#[cfg(test)]
Expand Down

0 comments on commit 40e91de

Please sign in to comment.