vChewing-macOS/Source/Engine/Gramambular/GramambularTest.cpp

248 lines
7.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) 2022 and onwards Lukhnos Liu
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include "Gramambular.h"
#include "gtest/gtest.h"
namespace Formosa {
namespace Gramambular {
const char* SampleData = R"(
#
# The sample is from libtabe (http://sourceforge.net/projects/libtabe/)
# last updated in 2002. The project was originally initiated by
# Pai-Hsiang Hsiao in 1999.
#
# Libtabe is a frequency table of Taiwanese Mandarin words. The database
# itself is, according to the tar file, released under the BSD License.
#
-9.495858
-9.006414
-99.000000
-8.091803
-99.000000
-13.513987
-12.259095
-7.171551
-10.574273
-11.504072
-10.450457
-7.171052
-99.000000
-11.928720
-13.624335
-12.390804
˙ -3.516024
ˊ -3.516024
ˋ -3.516024
-5.809297
˙ -7.427179
-8.381971
-8.501463
ˋ -99.000000
-8.034095
-8.858181
ˋ -7.608341
ˋ -99.000000
-7.290109
ˋ -10.939895
-99.000000
ˋ -99.000000
ˋ -99.000000
-99.000000
ˋ -9.715317
ˋ -7.926683
ˋ -8.373022
-9.877580
-10.711079
-7.877973
-7.822167
-99.000000
-99.000000
-99.000000
-9.685671
ˋ -10.425662
-99.000000
-99.000000
ˋ -8.888722
ˋ -10.204425
-11.378321
-99.000000
ˋ -99.000000
ˋ -8.450826
-11.074890
-99.000000
ˋ -12.045357
-99.000000
ˋ -99.000000
ˋ -9.517568
ˋ -12.021587
-99.000000
-12.784206
ˊ -6.086515
ˇ -9.164384
ˇ -8.690941
ˇ -10.127828
ˊ -11.336864
ˊ -11.285740
ˇ -12.492933
-6.299461
ˋ -6.736613
ˋ -13.336653
ˇ -10.344678
ˊ -11.668947
ˊ -11.373044
ˋ -9.842421
)";
class SimpleLM : public LanguageModel {
public:
SimpleLM(const char* input, bool swapKeyValue = false) {
std::stringstream sstream(input);
while (sstream.good()) {
std::string line;
getline(sstream, line);
if (!line.size() || (line.size() && line[0] == '#')) {
continue;
}
std::stringstream linestream(line);
std::string col0;
std::string col1;
std::string col2;
linestream >> col0;
linestream >> col1;
linestream >> col2;
Unigram u;
if (swapKeyValue) {
u.keyValue.key = col1;
u.keyValue.value = col0;
} else {
u.keyValue.key = col0;
u.keyValue.value = col1;
}
u.score = atof(col2.c_str());
m_db[u.keyValue.key].push_back(u);
}
}
const std::vector<Bigram> bigramsForKeys(const std::string& preceedingKey,
const std::string& key) override {
return std::vector<Bigram>();
}
const std::vector<Unigram> unigramsForKey(const std::string& key) override {
std::map<std::string, std::vector<Unigram> >::const_iterator f =
m_db.find(key);
return f == m_db.end() ? std::vector<Unigram>() : (*f).second;
}
bool hasUnigramsForKey(const std::string& key) override {
std::map<std::string, std::vector<Unigram> >::const_iterator f =
m_db.find(key);
return f != m_db.end();
}
protected:
std::map<std::string, std::vector<Unigram> > m_db;
};
TEST(GramambularTest, InputTest) {
SimpleLM lm(SampleData);
BlockReadingBuilder builder(&lm);
builder.insertReadingAtCursor("ㄍㄠ");
builder.insertReadingAtCursor("ㄐㄧˋ");
builder.setCursorIndex(1);
builder.insertReadingAtCursor("ㄎㄜ");
builder.setCursorIndex(0);
builder.deleteReadingAfterCursor();
builder.insertReadingAtCursor("ㄍㄠ");
builder.setCursorIndex(builder.length());
builder.insertReadingAtCursor("ㄍㄨㄥ");
builder.insertReadingAtCursor("");
builder.insertReadingAtCursor("ㄉㄜ˙");
builder.insertReadingAtCursor("ㄋㄧㄢˊ");
builder.insertReadingAtCursor("ㄓㄨㄥ");
builder.insertReadingAtCursor("ㄐㄧㄤˇ");
builder.insertReadingAtCursor("ㄐㄧㄣ");
Walker walker(&builder.grid());
std::vector<NodeAnchor> walked =
walker.reverseWalk(builder.grid().width(), 0.0);
reverse(walked.begin(), walked.end());
std::vector<std::string> composed;
for (std::vector<NodeAnchor>::iterator wi = walked.begin();
wi != walked.end(); ++wi) {
composed.push_back((*wi).node->currentKeyValue().value);
}
ASSERT_EQ(composed,
(std::vector<std::string>{"高科技", "公司", "", "年中", "獎金"}));
}
TEST(GramambularTest, WordSegmentationTest) {
SimpleLM lm2(SampleData, true);
BlockReadingBuilder builder2(&lm2);
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
builder2.insertReadingAtCursor("");
Walker walker2(&builder2.grid());
std::vector<NodeAnchor> walked =
walker2.reverseWalk(builder2.grid().width(), 0.0);
reverse(walked.begin(), walked.end());
std::vector<std::string> segmented;
for (std::vector<NodeAnchor>::iterator wi = walked.begin();
wi != walked.end(); ++wi) {
segmented.push_back((*wi).node->currentKeyValue().key);
}
ASSERT_EQ(segmented,
(std::vector<std::string>{"高科技", "公司", "", "年終", "獎金"}));
}
} // namespace Gramambular
} // namespace Formosa