Remove all the "inFoo" in-param style
We don't use out-params in Gramambular, but even for them the best practices have been converging on using "T& param" for required output param and "T* param" for optional output param. At any rate the prefix was never necessary, and hence the removal.
This commit is contained in:
parent
26ad5fd5ea
commit
e892628492
|
@ -42,53 +42,53 @@ class Bigram {
|
||||||
KeyValuePair keyValue;
|
KeyValuePair keyValue;
|
||||||
double score;
|
double score;
|
||||||
|
|
||||||
bool operator==(const Bigram& inAnother) const;
|
bool operator==(const Bigram& another) const;
|
||||||
bool operator<(const Bigram& inAnother) const;
|
bool operator<(const Bigram& another) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream, const Bigram& inGram) {
|
inline std::ostream& operator<<(std::ostream& stream, const Bigram& gram) {
|
||||||
std::streamsize p = inStream.precision();
|
std::streamsize p = stream.precision();
|
||||||
inStream.precision(6);
|
stream.precision(6);
|
||||||
inStream << "(" << inGram.keyValue << "|" << inGram.preceedingKeyValue << ","
|
stream << "(" << gram.keyValue << "|" << gram.preceedingKeyValue << ","
|
||||||
<< inGram.score << ")";
|
<< gram.score << ")";
|
||||||
inStream.precision(p);
|
stream.precision(p);
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream,
|
inline std::ostream& operator<<(std::ostream& stream,
|
||||||
const std::vector<Bigram>& inGrams) {
|
const std::vector<Bigram>& grams) {
|
||||||
inStream << "[" << inGrams.size() << "]=>{";
|
stream << "[" << grams.size() << "]=>{";
|
||||||
|
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
for (std::vector<Bigram>::const_iterator gi = inGrams.begin();
|
for (std::vector<Bigram>::const_iterator gi = grams.begin();
|
||||||
gi != inGrams.end(); ++gi, ++index) {
|
gi != grams.end(); ++gi, ++index) {
|
||||||
inStream << index << "=>";
|
stream << index << "=>";
|
||||||
inStream << *gi;
|
stream << *gi;
|
||||||
if (gi + 1 != inGrams.end()) {
|
if (gi + 1 != grams.end()) {
|
||||||
inStream << ",";
|
stream << ",";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inStream << "}";
|
stream << "}";
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bigram::Bigram() : score(0.0) {}
|
inline Bigram::Bigram() : score(0.0) {}
|
||||||
|
|
||||||
inline bool Bigram::operator==(const Bigram& inAnother) const {
|
inline bool Bigram::operator==(const Bigram& another) const {
|
||||||
return preceedingKeyValue == inAnother.preceedingKeyValue &&
|
return preceedingKeyValue == another.preceedingKeyValue &&
|
||||||
keyValue == inAnother.keyValue && score == inAnother.score;
|
keyValue == another.keyValue && score == another.score;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Bigram::operator<(const Bigram& inAnother) const {
|
inline bool Bigram::operator<(const Bigram& another) const {
|
||||||
if (preceedingKeyValue < inAnother.preceedingKeyValue) {
|
if (preceedingKeyValue < another.preceedingKeyValue) {
|
||||||
return true;
|
return true;
|
||||||
} else if (preceedingKeyValue == inAnother.preceedingKeyValue) {
|
} else if (preceedingKeyValue == another.preceedingKeyValue) {
|
||||||
if (keyValue < inAnother.keyValue) {
|
if (keyValue < another.keyValue) {
|
||||||
return true;
|
return true;
|
||||||
} else if (keyValue == inAnother.keyValue) {
|
} else if (keyValue == another.keyValue) {
|
||||||
return score < inAnother.score;
|
return score < another.score;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,13 +38,13 @@ namespace Gramambular {
|
||||||
|
|
||||||
class BlockReadingBuilder {
|
class BlockReadingBuilder {
|
||||||
public:
|
public:
|
||||||
BlockReadingBuilder(LanguageModel* inLM);
|
BlockReadingBuilder(LanguageModel* lm);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
size_t length() const;
|
size_t length() const;
|
||||||
size_t cursorIndex() const;
|
size_t cursorIndex() const;
|
||||||
void setCursorIndex(size_t inNewIndex);
|
void setCursorIndex(size_t newIndex);
|
||||||
void insertReadingAtCursor(const std::string& inReading);
|
void insertReadingAtCursor(const std::string& reading);
|
||||||
bool deleteReadingBeforeCursor(); // backspace
|
bool deleteReadingBeforeCursor(); // backspace
|
||||||
bool deleteReadingAfterCursor(); // delete
|
bool deleteReadingAfterCursor(); // delete
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ class BlockReadingBuilder {
|
||||||
std::vector<std::string>::const_iterator end,
|
std::vector<std::string>::const_iterator end,
|
||||||
const std::string& separator);
|
const std::string& separator);
|
||||||
|
|
||||||
//最多使用六個字組成一個詞
|
// 最多使用六個字組成一個詞
|
||||||
static const size_t MaximumBuildSpanLength = 6;
|
static const size_t MaximumBuildSpanLength = 6;
|
||||||
|
|
||||||
size_t m_cursorIndex;
|
size_t m_cursorIndex;
|
||||||
|
@ -75,8 +75,8 @@ class BlockReadingBuilder {
|
||||||
std::string m_joinSeparator;
|
std::string m_joinSeparator;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline BlockReadingBuilder::BlockReadingBuilder(LanguageModel* inLM)
|
inline BlockReadingBuilder::BlockReadingBuilder(LanguageModel* lm)
|
||||||
: m_LM(inLM), m_cursorIndex(0) {}
|
: m_LM(lm), m_cursorIndex(0) {}
|
||||||
|
|
||||||
inline void BlockReadingBuilder::clear() {
|
inline void BlockReadingBuilder::clear() {
|
||||||
m_cursorIndex = 0;
|
m_cursorIndex = 0;
|
||||||
|
@ -88,14 +88,13 @@ inline size_t BlockReadingBuilder::length() const { return m_readings.size(); }
|
||||||
|
|
||||||
inline size_t BlockReadingBuilder::cursorIndex() const { return m_cursorIndex; }
|
inline size_t BlockReadingBuilder::cursorIndex() const { return m_cursorIndex; }
|
||||||
|
|
||||||
inline void BlockReadingBuilder::setCursorIndex(size_t inNewIndex) {
|
inline void BlockReadingBuilder::setCursorIndex(size_t newIndex) {
|
||||||
m_cursorIndex =
|
m_cursorIndex = newIndex > m_readings.size() ? m_readings.size() : newIndex;
|
||||||
inNewIndex > m_readings.size() ? m_readings.size() : inNewIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void BlockReadingBuilder::insertReadingAtCursor(
|
inline void BlockReadingBuilder::insertReadingAtCursor(
|
||||||
const std::string& inReading) {
|
const std::string& reading) {
|
||||||
m_readings.insert(m_readings.begin() + m_cursorIndex, inReading);
|
m_readings.insert(m_readings.begin() + m_cursorIndex, reading);
|
||||||
|
|
||||||
m_grid.expandGridByOneAtLocation(m_cursorIndex);
|
m_grid.expandGridByOneAtLocation(m_cursorIndex);
|
||||||
build();
|
build();
|
||||||
|
|
|
@ -39,18 +39,17 @@ namespace Gramambular {
|
||||||
class Grid {
|
class Grid {
|
||||||
public:
|
public:
|
||||||
void clear();
|
void clear();
|
||||||
void insertNode(const Node& inNode, size_t inLocation,
|
void insertNode(const Node& node, size_t location, size_t spanningLength);
|
||||||
size_t inSpanningLength);
|
bool hasNodeAtLocationSpanningLengthMatchingKey(size_t location,
|
||||||
bool hasNodeAtLocationSpanningLengthMatchingKey(size_t inLocation,
|
size_t spanningLength,
|
||||||
size_t inSpanningLength,
|
const std::string& key);
|
||||||
const std::string& inKey);
|
|
||||||
|
|
||||||
void expandGridByOneAtLocation(size_t inLocation);
|
void expandGridByOneAtLocation(size_t location);
|
||||||
void shrinkGridByOneAtLocation(size_t inLocation);
|
void shrinkGridByOneAtLocation(size_t location);
|
||||||
|
|
||||||
size_t width() const;
|
size_t width() const;
|
||||||
std::vector<NodeAnchor> nodesEndingAt(size_t inLocation);
|
std::vector<NodeAnchor> nodesEndingAt(size_t location);
|
||||||
std::vector<NodeAnchor> nodesCrossingOrEndingAt(size_t inLocation);
|
std::vector<NodeAnchor> nodesCrossingOrEndingAt(size_t location);
|
||||||
|
|
||||||
// "Freeze" the node with the unigram that represents the selected candidate
|
// "Freeze" the node with the unigram that represents the selected candidate
|
||||||
// value. After this, the node that contains the unigram will always be
|
// value. After this, the node that contains the unigram will always be
|
||||||
|
@ -76,72 +75,72 @@ class Grid {
|
||||||
|
|
||||||
inline void Grid::clear() { m_spans.clear(); }
|
inline void Grid::clear() { m_spans.clear(); }
|
||||||
|
|
||||||
inline void Grid::insertNode(const Node& inNode, size_t inLocation,
|
inline void Grid::insertNode(const Node& node, size_t location,
|
||||||
size_t inSpanningLength) {
|
size_t spanningLength) {
|
||||||
if (inLocation >= m_spans.size()) {
|
if (location >= m_spans.size()) {
|
||||||
size_t diff = inLocation - m_spans.size() + 1;
|
size_t diff = location - m_spans.size() + 1;
|
||||||
|
|
||||||
for (size_t i = 0; i < diff; i++) {
|
for (size_t i = 0; i < diff; i++) {
|
||||||
m_spans.push_back(Span());
|
m_spans.push_back(Span());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_spans[inLocation].insertNodeOfLength(inNode, inSpanningLength);
|
m_spans[location].insertNodeOfLength(node, spanningLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Grid::hasNodeAtLocationSpanningLengthMatchingKey(
|
inline bool Grid::hasNodeAtLocationSpanningLengthMatchingKey(
|
||||||
size_t inLocation, size_t inSpanningLength, const std::string& inKey) {
|
size_t location, size_t spanningLength, const std::string& key) {
|
||||||
if (inLocation > m_spans.size()) {
|
if (location > m_spans.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Node* n = m_spans[inLocation].nodeOfLength(inSpanningLength);
|
const Node* n = m_spans[location].nodeOfLength(spanningLength);
|
||||||
if (!n) {
|
if (!n) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return inKey == n->key();
|
return key == n->key();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Grid::expandGridByOneAtLocation(size_t inLocation) {
|
inline void Grid::expandGridByOneAtLocation(size_t location) {
|
||||||
if (!inLocation || inLocation == m_spans.size()) {
|
if (!location || location == m_spans.size()) {
|
||||||
m_spans.insert(m_spans.begin() + inLocation, Span());
|
m_spans.insert(m_spans.begin() + location, Span());
|
||||||
} else {
|
} else {
|
||||||
m_spans.insert(m_spans.begin() + inLocation, Span());
|
m_spans.insert(m_spans.begin() + location, Span());
|
||||||
for (size_t i = 0; i < inLocation; i++) {
|
for (size_t i = 0; i < location; i++) {
|
||||||
// zaps overlapping spans
|
// zaps overlapping spans
|
||||||
m_spans[i].removeNodeOfLengthGreaterThan(inLocation - i);
|
m_spans[i].removeNodeOfLengthGreaterThan(location - i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Grid::shrinkGridByOneAtLocation(size_t inLocation) {
|
inline void Grid::shrinkGridByOneAtLocation(size_t location) {
|
||||||
if (inLocation >= m_spans.size()) {
|
if (location >= m_spans.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_spans.erase(m_spans.begin() + inLocation);
|
m_spans.erase(m_spans.begin() + location);
|
||||||
for (size_t i = 0; i < inLocation; i++) {
|
for (size_t i = 0; i < location; i++) {
|
||||||
// zaps overlapping spans
|
// zaps overlapping spans
|
||||||
m_spans[i].removeNodeOfLengthGreaterThan(inLocation - i);
|
m_spans[i].removeNodeOfLengthGreaterThan(location - i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t Grid::width() const { return m_spans.size(); }
|
inline size_t Grid::width() const { return m_spans.size(); }
|
||||||
|
|
||||||
inline std::vector<NodeAnchor> Grid::nodesEndingAt(size_t inLocation) {
|
inline std::vector<NodeAnchor> Grid::nodesEndingAt(size_t location) {
|
||||||
std::vector<NodeAnchor> result;
|
std::vector<NodeAnchor> result;
|
||||||
|
|
||||||
if (m_spans.size() && inLocation <= m_spans.size()) {
|
if (m_spans.size() && location <= m_spans.size()) {
|
||||||
for (size_t i = 0; i < inLocation; i++) {
|
for (size_t i = 0; i < location; i++) {
|
||||||
Span& span = m_spans[i];
|
Span& span = m_spans[i];
|
||||||
if (i + span.maximumLength() >= inLocation) {
|
if (i + span.maximumLength() >= location) {
|
||||||
Node* np = span.nodeOfLength(inLocation - i);
|
Node* np = span.nodeOfLength(location - i);
|
||||||
if (np) {
|
if (np) {
|
||||||
NodeAnchor na;
|
NodeAnchor na;
|
||||||
na.node = np;
|
na.node = np;
|
||||||
na.location = i;
|
na.location = i;
|
||||||
na.spanningLength = inLocation - i;
|
na.spanningLength = location - i;
|
||||||
|
|
||||||
result.push_back(na);
|
result.push_back(na);
|
||||||
}
|
}
|
||||||
|
@ -152,17 +151,16 @@ inline std::vector<NodeAnchor> Grid::nodesEndingAt(size_t inLocation) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(
|
inline std::vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(size_t location) {
|
||||||
size_t inLocation) {
|
|
||||||
std::vector<NodeAnchor> result;
|
std::vector<NodeAnchor> result;
|
||||||
|
|
||||||
if (m_spans.size() && inLocation <= m_spans.size()) {
|
if (m_spans.size() && location <= m_spans.size()) {
|
||||||
for (size_t i = 0; i < inLocation; i++) {
|
for (size_t i = 0; i < location; i++) {
|
||||||
Span& span = m_spans[i];
|
Span& span = m_spans[i];
|
||||||
|
|
||||||
if (i + span.maximumLength() >= inLocation) {
|
if (i + span.maximumLength() >= location) {
|
||||||
for (size_t j = 1, m = span.maximumLength(); j <= m; j++) {
|
for (size_t j = 1, m = span.maximumLength(); j <= m; j++) {
|
||||||
if (i + j < inLocation) {
|
if (i + j < location) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +169,7 @@ inline std::vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(
|
||||||
NodeAnchor na;
|
NodeAnchor na;
|
||||||
na.node = np;
|
na.node = np;
|
||||||
na.location = i;
|
na.location = i;
|
||||||
na.spanningLength = inLocation - i;
|
na.spanningLength = location - i;
|
||||||
|
|
||||||
result.push_back(na);
|
result.push_back(na);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,25 +39,25 @@ class KeyValuePair {
|
||||||
std::string key;
|
std::string key;
|
||||||
std::string value;
|
std::string value;
|
||||||
|
|
||||||
bool operator==(const KeyValuePair& inAnother) const;
|
bool operator==(const KeyValuePair& another) const;
|
||||||
bool operator<(const KeyValuePair& inAnother) const;
|
bool operator<(const KeyValuePair& another) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream,
|
inline std::ostream& operator<<(std::ostream& stream,
|
||||||
const KeyValuePair& inPair) {
|
const KeyValuePair& pair) {
|
||||||
inStream << "(" << inPair.key << "," << inPair.value << ")";
|
stream << "(" << pair.key << "," << pair.value << ")";
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool KeyValuePair::operator==(const KeyValuePair& inAnother) const {
|
inline bool KeyValuePair::operator==(const KeyValuePair& another) const {
|
||||||
return key == inAnother.key && value == inAnother.value;
|
return key == another.key && value == another.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool KeyValuePair::operator<(const KeyValuePair& inAnother) const {
|
inline bool KeyValuePair::operator<(const KeyValuePair& another) const {
|
||||||
if (key < inAnother.key) {
|
if (key < another.key) {
|
||||||
return true;
|
return true;
|
||||||
} else if (key == inAnother.key) {
|
} else if (key == another.key) {
|
||||||
return value < inAnother.value;
|
return value < another.value;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,15 +39,15 @@ namespace Gramambular {
|
||||||
class Node {
|
class Node {
|
||||||
public:
|
public:
|
||||||
Node();
|
Node();
|
||||||
Node(const std::string& inKey, const std::vector<Unigram>& inUnigrams,
|
Node(const std::string& key, const std::vector<Unigram>& unigrams,
|
||||||
const std::vector<Bigram>& inBigrams);
|
const std::vector<Bigram>& bigrams);
|
||||||
|
|
||||||
void primeNodeWithPreceedingKeyValues(
|
void primeNodeWithPreceedingKeyValues(
|
||||||
const std::vector<KeyValuePair>& inKeyValues);
|
const std::vector<KeyValuePair>& keyValues);
|
||||||
|
|
||||||
bool isCandidateFixed() const;
|
bool isCandidateFixed() const;
|
||||||
const std::vector<KeyValuePair>& candidates() const;
|
const std::vector<KeyValuePair>& candidates() const;
|
||||||
void selectCandidateAtIndex(size_t inIndex = 0, bool inFix = true);
|
void selectCandidateAtIndex(size_t index = 0, bool fix = true);
|
||||||
void resetCandidate();
|
void resetCandidate();
|
||||||
void selectFloatingCandidateAtIndex(size_t index, double score);
|
void selectFloatingCandidateAtIndex(size_t index, double score);
|
||||||
|
|
||||||
|
@ -71,25 +71,24 @@ class Node {
|
||||||
bool m_candidateFixed;
|
bool m_candidateFixed;
|
||||||
size_t m_selectedUnigramIndex;
|
size_t m_selectedUnigramIndex;
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& inStream, const Node& inNode);
|
friend std::ostream& operator<<(std::ostream& stream, const Node& node);
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream, const Node& inNode) {
|
inline std::ostream& operator<<(std::ostream& stream, const Node& node) {
|
||||||
inStream << "(node,key:" << inNode.m_key
|
stream << "(node,key:" << node.m_key
|
||||||
<< ",fixed:" << (inNode.m_candidateFixed ? "true" : "false")
|
<< ",fixed:" << (node.m_candidateFixed ? "true" : "false")
|
||||||
<< ",selected:" << inNode.m_selectedUnigramIndex << ","
|
<< ",selected:" << node.m_selectedUnigramIndex << ","
|
||||||
<< inNode.m_unigrams << ")";
|
<< node.m_unigrams << ")";
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Node::Node()
|
inline Node::Node()
|
||||||
: m_candidateFixed(false), m_selectedUnigramIndex(0), m_score(0.0) {}
|
: m_candidateFixed(false), m_selectedUnigramIndex(0), m_score(0.0) {}
|
||||||
|
|
||||||
inline Node::Node(const std::string& inKey,
|
inline Node::Node(const std::string& key, const std::vector<Unigram>& unigrams,
|
||||||
const std::vector<Unigram>& inUnigrams,
|
const std::vector<Bigram>& bigrams)
|
||||||
const std::vector<Bigram>& inBigrams)
|
: m_key(key),
|
||||||
: m_key(inKey),
|
m_unigrams(unigrams),
|
||||||
m_unigrams(inUnigrams),
|
|
||||||
m_candidateFixed(false),
|
m_candidateFixed(false),
|
||||||
m_selectedUnigramIndex(0),
|
m_selectedUnigramIndex(0),
|
||||||
m_score(0.0) {
|
m_score(0.0) {
|
||||||
|
@ -108,20 +107,20 @@ inline Node::Node(const std::string& inKey,
|
||||||
m_candidates.push_back((*ui).keyValue);
|
m_candidates.push_back((*ui).keyValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::vector<Bigram>::const_iterator bi = inBigrams.begin();
|
for (std::vector<Bigram>::const_iterator bi = bigrams.begin();
|
||||||
bi != inBigrams.end(); ++bi) {
|
bi != bigrams.end(); ++bi) {
|
||||||
m_preceedingGramBigramMap[(*bi).preceedingKeyValue].push_back(*bi);
|
m_preceedingGramBigramMap[(*bi).preceedingKeyValue].push_back(*bi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Node::primeNodeWithPreceedingKeyValues(
|
inline void Node::primeNodeWithPreceedingKeyValues(
|
||||||
const std::vector<KeyValuePair>& inKeyValues) {
|
const std::vector<KeyValuePair>& keyValues) {
|
||||||
size_t newIndex = m_selectedUnigramIndex;
|
size_t newIndex = m_selectedUnigramIndex;
|
||||||
double max = m_score;
|
double max = m_score;
|
||||||
|
|
||||||
if (!isCandidateFixed()) {
|
if (!isCandidateFixed()) {
|
||||||
for (std::vector<KeyValuePair>::const_iterator kvi = inKeyValues.begin();
|
for (std::vector<KeyValuePair>::const_iterator kvi = keyValues.begin();
|
||||||
kvi != inKeyValues.end(); ++kvi) {
|
kvi != keyValues.end(); ++kvi) {
|
||||||
std::map<KeyValuePair, std::vector<Bigram> >::const_iterator f =
|
std::map<KeyValuePair, std::vector<Bigram> >::const_iterator f =
|
||||||
m_preceedingGramBigramMap.find(*kvi);
|
m_preceedingGramBigramMap.find(*kvi);
|
||||||
if (f != m_preceedingGramBigramMap.end()) {
|
if (f != m_preceedingGramBigramMap.end()) {
|
||||||
|
@ -158,14 +157,14 @@ inline const std::vector<KeyValuePair>& Node::candidates() const {
|
||||||
return m_candidates;
|
return m_candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Node::selectCandidateAtIndex(size_t inIndex, bool inFix) {
|
inline void Node::selectCandidateAtIndex(size_t index, bool fix) {
|
||||||
if (inIndex >= m_unigrams.size()) {
|
if (index >= m_unigrams.size()) {
|
||||||
m_selectedUnigramIndex = 0;
|
m_selectedUnigramIndex = 0;
|
||||||
} else {
|
} else {
|
||||||
m_selectedUnigramIndex = inIndex;
|
m_selectedUnigramIndex = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_candidateFixed = inFix;
|
m_candidateFixed = fix;
|
||||||
m_score = 99;
|
m_score = 99;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,42 +32,37 @@
|
||||||
|
|
||||||
namespace Formosa {
|
namespace Formosa {
|
||||||
namespace Gramambular {
|
namespace Gramambular {
|
||||||
class NodeAnchor {
|
|
||||||
public:
|
struct NodeAnchor {
|
||||||
NodeAnchor();
|
const Node* node = nullptr;
|
||||||
const Node* node;
|
size_t location = 0;
|
||||||
size_t location;
|
size_t spanningLength = 0;
|
||||||
size_t spanningLength;
|
double accumulatedScore = 0.0;
|
||||||
double accumulatedScore;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline NodeAnchor::NodeAnchor()
|
inline std::ostream& operator<<(std::ostream& stream,
|
||||||
: node(0), location(0), spanningLength(0), accumulatedScore(0.0) {}
|
const NodeAnchor& anchor) {
|
||||||
|
stream << "{@(" << anchor.location << "," << anchor.spanningLength << "),";
|
||||||
inline std::ostream& operator<<(std::ostream& inStream,
|
if (anchor.node) {
|
||||||
const NodeAnchor& inAnchor) {
|
stream << *(anchor.node);
|
||||||
inStream << "{@(" << inAnchor.location << "," << inAnchor.spanningLength
|
|
||||||
<< "),";
|
|
||||||
if (inAnchor.node) {
|
|
||||||
inStream << *(inAnchor.node);
|
|
||||||
} else {
|
} else {
|
||||||
inStream << "null";
|
stream << "null";
|
||||||
}
|
}
|
||||||
inStream << "}";
|
stream << "}";
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream,
|
inline std::ostream& operator<<(std::ostream& stream,
|
||||||
const std::vector<NodeAnchor>& inAnchor) {
|
const std::vector<NodeAnchor>& anchor) {
|
||||||
for (std::vector<NodeAnchor>::const_iterator i = inAnchor.begin();
|
for (std::vector<NodeAnchor>::const_iterator i = anchor.begin();
|
||||||
i != inAnchor.end(); ++i) {
|
i != anchor.end(); ++i) {
|
||||||
inStream << *i;
|
stream << *i;
|
||||||
if (i + 1 != inAnchor.end()) {
|
if (i + 1 != anchor.end()) {
|
||||||
inStream << "<-";
|
stream << "<-";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
} // namespace Gramambular
|
} // namespace Gramambular
|
||||||
} // namespace Formosa
|
} // namespace Formosa
|
||||||
|
|
|
@ -38,36 +38,32 @@ namespace Formosa {
|
||||||
namespace Gramambular {
|
namespace Gramambular {
|
||||||
class Span {
|
class Span {
|
||||||
public:
|
public:
|
||||||
Span();
|
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
void insertNodeOfLength(const Node& inNode, size_t inLength);
|
void insertNodeOfLength(const Node& node, size_t length);
|
||||||
void removeNodeOfLengthGreaterThan(size_t inLength);
|
void removeNodeOfLengthGreaterThan(size_t length);
|
||||||
|
|
||||||
Node* nodeOfLength(size_t inLength);
|
Node* nodeOfLength(size_t length);
|
||||||
size_t maximumLength() const;
|
size_t maximumLength() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::map<size_t, Node> m_lengthNodeMap;
|
std::map<size_t, Node> m_lengthNodeMap;
|
||||||
size_t m_maximumLength;
|
size_t m_maximumLength = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Span::Span() : m_maximumLength(0) {}
|
|
||||||
|
|
||||||
inline void Span::clear() {
|
inline void Span::clear() {
|
||||||
m_lengthNodeMap.clear();
|
m_lengthNodeMap.clear();
|
||||||
m_maximumLength = 0;
|
m_maximumLength = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Span::insertNodeOfLength(const Node& inNode, size_t inLength) {
|
inline void Span::insertNodeOfLength(const Node& node, size_t length) {
|
||||||
m_lengthNodeMap[inLength] = inNode;
|
m_lengthNodeMap[length] = node;
|
||||||
if (inLength > m_maximumLength) {
|
if (length > m_maximumLength) {
|
||||||
m_maximumLength = inLength;
|
m_maximumLength = length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
|
inline void Span::removeNodeOfLengthGreaterThan(size_t length) {
|
||||||
if (inLength > m_maximumLength) {
|
if (length > m_maximumLength) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +72,7 @@ inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
|
||||||
for (std::map<size_t, Node>::iterator i = m_lengthNodeMap.begin(),
|
for (std::map<size_t, Node>::iterator i = m_lengthNodeMap.begin(),
|
||||||
e = m_lengthNodeMap.end();
|
e = m_lengthNodeMap.end();
|
||||||
i != e; ++i) {
|
i != e; ++i) {
|
||||||
if ((*i).first > inLength) {
|
if ((*i).first > length) {
|
||||||
removeSet.insert((*i).first);
|
removeSet.insert((*i).first);
|
||||||
} else {
|
} else {
|
||||||
if ((*i).first > max) {
|
if ((*i).first > max) {
|
||||||
|
@ -93,8 +89,8 @@ inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
|
||||||
m_maximumLength = max;
|
m_maximumLength = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Node* Span::nodeOfLength(size_t inLength) {
|
inline Node* Span::nodeOfLength(size_t length) {
|
||||||
std::map<size_t, Node>::iterator f = m_lengthNodeMap.find(inLength);
|
std::map<size_t, Node>::iterator f = m_lengthNodeMap.find(length);
|
||||||
return f == m_lengthNodeMap.end() ? 0 : &(*f).second;
|
return f == m_lengthNodeMap.end() ? 0 : &(*f).second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
namespace Formosa {
|
namespace Formosa {
|
||||||
namespace Gramambular {
|
namespace Gramambular {
|
||||||
|
|
||||||
class Unigram {
|
class Unigram {
|
||||||
public:
|
public:
|
||||||
Unigram();
|
Unigram();
|
||||||
|
@ -41,50 +42,50 @@ class Unigram {
|
||||||
KeyValuePair keyValue;
|
KeyValuePair keyValue;
|
||||||
double score;
|
double score;
|
||||||
|
|
||||||
bool operator==(const Unigram& inAnother) const;
|
bool operator==(const Unigram& another) const;
|
||||||
bool operator<(const Unigram& inAnother) const;
|
bool operator<(const Unigram& another) const;
|
||||||
|
|
||||||
static bool ScoreCompare(const Unigram& a, const Unigram& b);
|
static bool ScoreCompare(const Unigram& a, const Unigram& b);
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream, const Unigram& inGram) {
|
inline std::ostream& operator<<(std::ostream& stream, const Unigram& gram) {
|
||||||
std::streamsize p = inStream.precision();
|
std::streamsize p = stream.precision();
|
||||||
inStream.precision(6);
|
stream.precision(6);
|
||||||
inStream << "(" << inGram.keyValue << "," << inGram.score << ")";
|
stream << "(" << gram.keyValue << "," << gram.score << ")";
|
||||||
inStream.precision(p);
|
stream.precision(p);
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& inStream,
|
inline std::ostream& operator<<(std::ostream& stream,
|
||||||
const std::vector<Unigram>& inGrams) {
|
const std::vector<Unigram>& grams) {
|
||||||
inStream << "[" << inGrams.size() << "]=>{";
|
stream << "[" << grams.size() << "]=>{";
|
||||||
|
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
for (std::vector<Unigram>::const_iterator gi = inGrams.begin();
|
for (std::vector<Unigram>::const_iterator gi = grams.begin();
|
||||||
gi != inGrams.end(); ++gi, ++index) {
|
gi != grams.end(); ++gi, ++index) {
|
||||||
inStream << index << "=>";
|
stream << index << "=>";
|
||||||
inStream << *gi;
|
stream << *gi;
|
||||||
if (gi + 1 != inGrams.end()) {
|
if (gi + 1 != grams.end()) {
|
||||||
inStream << ",";
|
stream << ",";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inStream << "}";
|
stream << "}";
|
||||||
return inStream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Unigram::Unigram() : score(0.0) {}
|
inline Unigram::Unigram() : score(0.0) {}
|
||||||
|
|
||||||
inline bool Unigram::operator==(const Unigram& inAnother) const {
|
inline bool Unigram::operator==(const Unigram& another) const {
|
||||||
return keyValue == inAnother.keyValue && score == inAnother.score;
|
return keyValue == another.keyValue && score == another.score;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Unigram::operator<(const Unigram& inAnother) const {
|
inline bool Unigram::operator<(const Unigram& another) const {
|
||||||
if (keyValue < inAnother.keyValue) {
|
if (keyValue < another.keyValue) {
|
||||||
return true;
|
return true;
|
||||||
} else if (keyValue == inAnother.keyValue) {
|
} else if (keyValue == another.keyValue) {
|
||||||
return score < inAnother.score;
|
return score < another.score;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,8 +38,8 @@ namespace Gramambular {
|
||||||
class Walker {
|
class Walker {
|
||||||
public:
|
public:
|
||||||
Walker(Grid* inGrid);
|
Walker(Grid* inGrid);
|
||||||
const std::vector<NodeAnchor> reverseWalk(size_t inLocation,
|
const std::vector<NodeAnchor> reverseWalk(size_t location,
|
||||||
double inAccumulatedScore = 0.0);
|
double accumulatedScore = 0.0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Grid* m_grid;
|
Grid* m_grid;
|
||||||
|
@ -48,14 +48,14 @@ class Walker {
|
||||||
inline Walker::Walker(Grid* inGrid) : m_grid(inGrid) {}
|
inline Walker::Walker(Grid* inGrid) : m_grid(inGrid) {}
|
||||||
|
|
||||||
inline const std::vector<NodeAnchor> Walker::reverseWalk(
|
inline const std::vector<NodeAnchor> Walker::reverseWalk(
|
||||||
size_t inLocation, double inAccumulatedScore) {
|
size_t location, double accumulatedScore) {
|
||||||
if (!inLocation || inLocation > m_grid->width()) {
|
if (!location || location > m_grid->width()) {
|
||||||
return std::vector<NodeAnchor>();
|
return std::vector<NodeAnchor>();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::vector<NodeAnchor> > paths;
|
std::vector<std::vector<NodeAnchor> > paths;
|
||||||
|
|
||||||
std::vector<NodeAnchor> nodes = m_grid->nodesEndingAt(inLocation);
|
std::vector<NodeAnchor> nodes = m_grid->nodesEndingAt(location);
|
||||||
|
|
||||||
for (std::vector<NodeAnchor>::iterator ni = nodes.begin(); ni != nodes.end();
|
for (std::vector<NodeAnchor>::iterator ni = nodes.begin(); ni != nodes.end();
|
||||||
++ni) {
|
++ni) {
|
||||||
|
@ -63,10 +63,10 @@ inline const std::vector<NodeAnchor> Walker::reverseWalk(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*ni).accumulatedScore = inAccumulatedScore + (*ni).node->score();
|
(*ni).accumulatedScore = accumulatedScore + (*ni).node->score();
|
||||||
|
|
||||||
std::vector<NodeAnchor> path =
|
std::vector<NodeAnchor> path =
|
||||||
reverseWalk(inLocation - (*ni).spanningLength, (*ni).accumulatedScore);
|
reverseWalk(location - (*ni).spanningLength, (*ni).accumulatedScore);
|
||||||
path.insert(path.begin(), *ni);
|
path.insert(path.begin(), *ni);
|
||||||
|
|
||||||
paths.push_back(path);
|
paths.push_back(path);
|
||||||
|
|
Loading…
Reference in New Issue