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