From a3ecab05f05262deb247b007d6a031c4ade9f649 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 15 Oct 2016 17:32:56 -0400 Subject: [PATCH] Time parsing --- dictionary/NEEDED.md | 1 + src/lang/parser.rs | 50 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/dictionary/NEEDED.md b/dictionary/NEEDED.md index 8bb651633f..17e758e9da 100644 --- a/dictionary/NEEDED.md +++ b/dictionary/NEEDED.md @@ -34,5 +34,6 @@ unicode untrimmed wanker wario +wristband wwii ✓ diff --git a/src/lang/parser.rs b/src/lang/parser.rs index b95edb45f9..825f0e25df 100644 --- a/src/lang/parser.rs +++ b/src/lang/parser.rs @@ -93,12 +93,54 @@ impl Parser { Token::MaybeTimeUnit { value: ref v } => { sentence.push(v.value.to_string()); }, - Token::Time { value: ref v } => { - sentence.push(v.value.to_string()); - }, Token::DictionaryWord { value : ref v } => { sentence.push(v.value.to_string()); } + Token::Time { value: ref v } => { + // FIXME: Efficiency, cleanup + let mut valid = true; + let mut numbers = Vec::new(); + + for split in v.value.split(":").collect::>() { + match split.parse::() { + Ok(num) => { + if num == 0 { + continue; + } + numbers.push(num) + }, + Err(_) => { + valid = false; + break; + }, + }; + } + + if !valid { + sentence.push(v.value.to_string()); + continue; + } + + let mut number_words = Vec::new(); + + for num in numbers { + match number_to_words(num) { + None => { + valid = false; + break; + }, + Some(words) => { + for word in words { number_words.push(word); } + } + } + } + + if valid { + for word in number_words { sentence.push(word); } + } else { + sentence.push(v.value.to_string()); + } + }, // Integers (TODO: tokenize floats separately.) Token::Number { value: ref v } => { let num = match v.value.parse::() { @@ -334,7 +376,7 @@ mod tests { assert_eq!("a lot of people can't handle it", &p.parse(&s, "A lot of people can’t handle it.")); // Complex examples taken from real tweets. - assert_eq!("will be in atlanta georgia this friday at 5:00 pm. \ + assert_eq!("will be in atlanta georgia this friday at five pm. \ join the movement tickets available at", &p.parse(&s, r#"Will be in Atlanta, Georgia this Friday at 5:00pm. Join the MOVEMENT! Tickets available at: https://t.co/Q6APf0ZFYA… https://t.co/6WAyO9eQHN"#));