use pos_tagger::Lexer;
fn lex(input: &str) -> Vec<String> {
Lexer::new().lex(input)
}
#[test]
fn tokenizes_documented_cases() {
let cases: &[(&str, &[&str])] = &[
("I made $5.42 today.", &["I", "made", "$", "5.42", "today", "."]),
("I made $some today.", &["I", "made", "$", "some", "today", "."]),
(
"I made $5.42 today (but it should have been more).",
&[
"I", "made", "$", "5.42", "today", "(", "but", "it", "should", "have", "been",
"more", ")", ".",
],
),
("I'm fixing issue #6.", &["I", "'", "m", "fixing", "issue", "#", "6", "."]),
("I'm fixing issue #tags.", &["I", "'", "m", "fixing", "issue", "#", "tags", "."]),
("It is now 10:00am", &["It", "is", "now", "10:00am"]),
("It is now 10:00 am", &["It", "is", "now", "10:00 am"]),
("It is now 10:00pm", &["It", "is", "now", "10:00pm"]),
("It is now 10:00 pm", &["It", "is", "now", "10:00 pm"]),
("It is now 10:00AM", &["It", "is", "now", "10:00AM"]),
("It is now 10:00PM", &["It", "is", "now", "10:00PM"]),
(
"my email is me@exampleabc.com, http://example.com and 19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE",
&[
"my",
"email",
"is",
"me@exampleabc.com",
",",
"http://example.com",
"and",
"19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE",
],
),
(
"my share url is https://docs.google.com/spreadsheets/d/19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE/edit?usp=sharing",
&[
"my",
"share",
"url",
"is",
"https://docs.google.com/spreadsheets/d/19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE/edit?usp=sharing",
],
),
];
for (input, expected) in cases {
assert_eq!(lex(input), *expected, "input: {input}");
}
}
#[test]
fn empty_and_whitespace_yield_no_tokens() {
assert_eq!(lex(""), Vec::<String>::new());
assert_eq!(lex(" "), Vec::<String>::new());
assert_eq!(lex("\t\n "), Vec::<String>::new());
}
#[test]
fn single_word_stays_whole() {
assert_eq!(lex("Hello"), vec!["Hello"]);
}
#[test]
fn hyphenated_word_stays_whole() {
assert_eq!(lex("so-called"), vec!["so-called"]);
}
#[test]
fn eight_char_token_stays_whole() {
assert_eq!(lex("abcdefgh"), vec!["abcdefgh"]);
}
#[test]
fn email_with_digit_local_part_stays_whole() {
assert_eq!(lex("me2@exampleabc.com"), vec!["me2@exampleabc.com"]);
}
#[test]
fn url_query_ampersand_stays_whole() {
assert_eq!(
lex("https://example.com/search?a=1&b=2"),
vec!["https://example.com/search?a=1&b=2"]
);
}
#[test]
fn url_percent_path_stays_whole() {
assert_eq!(
lex("https://example.com/a%20b"),
vec!["https://example.com/a%20b"]
);
}
#[test]
fn long_id_before_comma_keeps_id_then_comma() {
assert_eq!(
lex("19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE,"),
vec!["19bdnznUXdHEOlp0Pnp9JY0rug6VuA2R3zK4AACdFzhE", ","]
);
}
#[test]
fn email_with_long_local_part_stays_whole() {
assert_eq!(lex("johnsmith@example.com"), vec!["johnsmith@example.com"]);
}
#[test]
fn long_decimal_stays_whole() {
assert_eq!(lex("12345678.5"), vec!["12345678.5"]);
}
#[test]
fn id_ending_with_hyphen_before_comma_stays_whole() {
assert_eq!(lex("abcdefgh-,"), vec!["abcdefgh-", ","]);
}
#[test]
fn long_hyphenated_token_does_not_split_at_prefix() {
assert_eq!(
lex("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS-TAIL"),
vec!["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS-TAIL"]
);
}
#[test]
fn dotted_abbreviation_splits_into_letters() {
assert_eq!(
lex("The E.M.T.'s were on time"),
vec!["The", "E", ".", "M", ".", "T", ".", "'", "s", "were", "on", "time"]
);
}
#[test]
fn whitespace_class_matches_js_on_nel_and_bom() {
assert_eq!(lex("alpha\u{0085}beta"), vec!["alpha\u{0085}beta"]);
assert_eq!(lex("alpha\u{FEFF}beta"), vec!["alpha", "beta"]);
}
#[test]
fn mixed_sentence_recursive_split() {
assert_eq!(
lex("I made $5.60 today in 1 hour of work. The E.M.T.'s were on time, but only barely."),
vec![
"I", "made", "$", "5.60", "today", "in", "1", "hour", "of", "work", ".", "The", "E",
".", "M", ".", "T", ".", "'", "s", "were", "on", "time", ",", "but", "only", "barely",
".",
]
);
}