Improved tokenization by taking dictionary into account

This commit is contained in:
Daniel Wolf
2016-06-25 21:52:04 +02:00
parent 8502256241
commit c9b17e1937
4 changed files with 60 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
#include <gmock/gmock.h>
#include "tokenization.h"
#include <regex>
#include <unordered_set>
using namespace testing;
using std::string;
@@ -8,48 +9,51 @@ using std::u32string;
using std::vector;
using std::regex;
bool returnTrue(const string&) {
return true;
}
TEST(tokenizeText, simpleCases) {
EXPECT_THAT(tokenizeText(U""), IsEmpty());
EXPECT_THAT(tokenizeText(U" \t\n\r\n "), IsEmpty());
EXPECT_THAT(tokenizeText(U"", returnTrue), IsEmpty());
EXPECT_THAT(tokenizeText(U" \t\n\r\n ", returnTrue), IsEmpty());
EXPECT_THAT(
tokenizeText(U"Wit is educated insolence."),
tokenizeText(U"Wit is educated insolence.", returnTrue),
ElementsAre("wit", "is", "educated", "insolence")
);
}
TEST(tokenizeText, numbers) {
EXPECT_THAT(
tokenizeText(U"Henry V died at 36."),
tokenizeText(U"Henry V died at 36.", returnTrue),
ElementsAre("henry", "the", "fifth", "died", "at", "thirty", "six")
);
EXPECT_THAT(
tokenizeText(U"I spent $4.50 on gum."),
tokenizeText(U"I spent $4.50 on gum.", returnTrue),
ElementsAre("i", "spent", "four", "dollars", "fifty", "cents", "on", "gum")
);
EXPECT_THAT(
tokenizeText(U"I was born in 1982."),
tokenizeText(U"I was born in 1982.", returnTrue),
ElementsAre("i", "was", "born", "in", "nineteen", "eighty", "two")
);
}
TEST(tokenizeText, abbreviations) {
EXPECT_THAT(
tokenizeText(U"I live on Dr. Dolittle Dr."),
ElementsAre("i", "live", "on", "doctor", "dolittle", "drive")
tokenizeText(U"Prof. Foo lives on Dr. Dolittle Dr.", [](const string& word) { return word == "prof."; }),
ElementsAre("prof.", "foo", "lives", "on", "doctor", "dolittle", "drive")
);
}
TEST(tokenizeText, apostrophes) {
// HACK: "wouldn't" really should not become "wouldnt"!
EXPECT_THAT(
tokenizeText(U"'Tis said he'd wish'd for a 'bus 'cause he wouldn't walk."),
ElementsAreArray(vector<string>{ "tis", "said", "he'd", "wish'd", "for", "a", "bus", "cause", "he", "wouldnt", "walk" })
tokenizeText(U"'Tis said he'd wish'd for a 'bus 'cause he wouldn't walk.", [](const string& word) { return word == "wouldn't"; }),
ElementsAreArray(vector<string>{ "tis", "said", "he'd", "wish'd", "for", "a", "bus", "cause", "he", "wouldn't", "walk" })
);
}
TEST(tokenizeText, math) {
EXPECT_THAT(
tokenizeText(U"'1+2*3=7"),
tokenizeText(U"'1+2*3=7", returnTrue),
ElementsAre("one", "plus", "two", "times", "three", "equals", "seven")
);
}
@@ -64,7 +68,7 @@ TEST(tokenizeText, wordsUseLimitedCharacters) {
}
regex legal("^[a-z']+$");
auto words = tokenizeText(input);
auto words = tokenizeText(input, returnTrue);
for (const string& word : words) {
EXPECT_TRUE(std::regex_match(word, legal)) << word;
}