Remove all the "using namespace" usage

This commit is contained in:
Lukhnos Liu 2022-02-19 09:35:48 -08:00
parent d3302ef70a
commit 13609f41f5
11 changed files with 158 additions and 148 deletions

View File

@ -46,8 +46,8 @@ class Bigram {
bool operator<(const Bigram& inAnother) const; bool operator<(const Bigram& inAnother) const;
}; };
inline ostream& operator<<(ostream& inStream, const Bigram& inGram) { inline std::ostream& operator<<(std::ostream& inStream, const Bigram& inGram) {
streamsize p = inStream.precision(); std::streamsize p = inStream.precision();
inStream.precision(6); inStream.precision(6);
inStream << "(" << inGram.keyValue << "|" << inGram.preceedingKeyValue << "," inStream << "(" << inGram.keyValue << "|" << inGram.preceedingKeyValue << ","
<< inGram.score << ")"; << inGram.score << ")";
@ -55,13 +55,14 @@ inline ostream& operator<<(ostream& inStream, const Bigram& inGram) {
return inStream; return inStream;
} }
inline ostream& operator<<(ostream& inStream, const vector<Bigram>& inGrams) { inline std::ostream& operator<<(std::ostream& inStream,
const std::vector<Bigram>& inGrams) {
inStream << "[" << inGrams.size() << "]=>{"; inStream << "[" << inGrams.size() << "]=>{";
size_t index = 0; size_t index = 0;
for (vector<Bigram>::const_iterator gi = inGrams.begin(); gi != inGrams.end(); for (std::vector<Bigram>::const_iterator gi = inGrams.begin();
++gi, ++index) { gi != inGrams.end(); ++gi, ++index) {
inStream << index << "=>"; inStream << index << "=>";
inStream << *gi; inStream << *gi;
if (gi + 1 != inGrams.end()) { if (gi + 1 != inGrams.end()) {

View File

@ -35,7 +35,6 @@
namespace Formosa { namespace Formosa {
namespace Gramambular { namespace Gramambular {
using namespace std;
class BlockReadingBuilder { class BlockReadingBuilder {
public: public:
@ -45,35 +44,35 @@ class BlockReadingBuilder {
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 inNewIndex);
void insertReadingAtCursor(const string& inReading); void insertReadingAtCursor(const std::string& inReading);
bool deleteReadingBeforeCursor(); // backspace bool deleteReadingBeforeCursor(); // backspace
bool deleteReadingAfterCursor(); // delete bool deleteReadingAfterCursor(); // delete
bool removeHeadReadings(size_t count); bool removeHeadReadings(size_t count);
void setJoinSeparator(const string& separator); void setJoinSeparator(const std::string& separator);
const string joinSeparator() const; const std::string joinSeparator() const;
vector<string> readings() const; std::vector<std::string> readings() const;
Grid& grid(); Grid& grid();
protected: protected:
void build(); void build();
static const string Join(vector<string>::const_iterator begin, static const std::string Join(std::vector<std::string>::const_iterator begin,
vector<string>::const_iterator end, std::vector<std::string>::const_iterator end,
const 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;
vector<string> m_readings; std::vector<std::string> m_readings;
Grid m_grid; Grid m_grid;
LanguageModel* m_LM; LanguageModel* m_LM;
string m_joinSeparator; std::string m_joinSeparator;
}; };
inline BlockReadingBuilder::BlockReadingBuilder(LanguageModel* inLM) inline BlockReadingBuilder::BlockReadingBuilder(LanguageModel* inLM)
@ -95,7 +94,7 @@ inline void BlockReadingBuilder::setCursorIndex(size_t inNewIndex) {
} }
inline void BlockReadingBuilder::insertReadingAtCursor( inline void BlockReadingBuilder::insertReadingAtCursor(
const string& inReading) { const std::string& inReading) {
m_readings.insert(m_readings.begin() + m_cursorIndex, inReading); m_readings.insert(m_readings.begin() + m_cursorIndex, inReading);
m_grid.expandGridByOneAtLocation(m_cursorIndex); m_grid.expandGridByOneAtLocation(m_cursorIndex);
@ -103,7 +102,7 @@ inline void BlockReadingBuilder::insertReadingAtCursor(
m_cursorIndex++; m_cursorIndex++;
} }
inline vector<string> BlockReadingBuilder::readings() const { inline std::vector<std::string> BlockReadingBuilder::readings() const {
return m_readings; return m_readings;
} }
@ -149,11 +148,12 @@ inline bool BlockReadingBuilder::removeHeadReadings(size_t count) {
return true; return true;
} }
inline void BlockReadingBuilder::setJoinSeparator(const string& separator) { inline void BlockReadingBuilder::setJoinSeparator(
const std::string& separator) {
m_joinSeparator = separator; m_joinSeparator = separator;
} }
inline const string BlockReadingBuilder::joinSeparator() const { inline const std::string BlockReadingBuilder::joinSeparator() const {
return m_joinSeparator; return m_joinSeparator;
} }
@ -179,14 +179,14 @@ inline void BlockReadingBuilder::build() {
for (size_t p = begin; p < end; p++) { for (size_t p = begin; p < end; p++) {
for (size_t q = 1; q <= MaximumBuildSpanLength && p + q <= end; q++) { for (size_t q = 1; q <= MaximumBuildSpanLength && p + q <= end; q++) {
string combinedReading = Join( std::string combinedReading = Join(
m_readings.begin() + p, m_readings.begin() + p + q, m_joinSeparator); m_readings.begin() + p, m_readings.begin() + p + q, m_joinSeparator);
if (!m_grid.hasNodeAtLocationSpanningLengthMatchingKey(p, q, if (!m_grid.hasNodeAtLocationSpanningLengthMatchingKey(p, q,
combinedReading)) { combinedReading)) {
vector<Unigram> unigrams = m_LM->unigramsForKey(combinedReading); std::vector<Unigram> unigrams = m_LM->unigramsForKey(combinedReading);
if (unigrams.size() > 0) { if (unigrams.size() > 0) {
Node n(combinedReading, unigrams, vector<Bigram>()); Node n(combinedReading, unigrams, std::vector<Bigram>());
m_grid.insertNode(n, p, q); m_grid.insertNode(n, p, q);
} }
} }
@ -194,11 +194,12 @@ inline void BlockReadingBuilder::build() {
} }
} }
inline const string BlockReadingBuilder::Join( inline const std::string BlockReadingBuilder::Join(
vector<string>::const_iterator begin, vector<string>::const_iterator end, std::vector<std::string>::const_iterator begin,
const string& separator) { std::vector<std::string>::const_iterator end,
string result; const std::string& separator) {
for (vector<string>::const_iterator iter = begin; iter != end;) { std::string result;
for (std::vector<std::string>::const_iterator iter = begin; iter != end;) {
result += *iter; result += *iter;
++iter; ++iter;
if (iter != end) { if (iter != end) {

View File

@ -31,6 +31,9 @@
#include "Gramambular.h" #include "Gramambular.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace Formosa {
namespace Gramambular {
const char* SampleData = R"( const char* SampleData = R"(
# #
# The sample is from libtabe (http://sourceforge.net/projects/libtabe/) # The sample is from libtabe (http://sourceforge.net/projects/libtabe/)
@ -120,25 +123,22 @@ const char* SampleData = R"(
ˋ -9.842421 ˋ -9.842421
)"; )";
using namespace std;
using namespace Formosa::Gramambular;
class SimpleLM : public LanguageModel { class SimpleLM : public LanguageModel {
public: public:
SimpleLM(const char* input, bool swapKeyValue = false) { SimpleLM(const char* input, bool swapKeyValue = false) {
stringstream sstream(input); std::stringstream sstream(input);
while (sstream.good()) { while (sstream.good()) {
string line; std::string line;
getline(sstream, line); getline(sstream, line);
if (!line.size() || (line.size() && line[0] == '#')) { if (!line.size() || (line.size() && line[0] == '#')) {
continue; continue;
} }
stringstream linestream(line); std::stringstream linestream(line);
string col0; std::string col0;
string col1; std::string col1;
string col2; std::string col2;
linestream >> col0; linestream >> col0;
linestream >> col1; linestream >> col1;
linestream >> col2; linestream >> col2;
@ -159,23 +159,25 @@ class SimpleLM : public LanguageModel {
} }
} }
const vector<Bigram> bigramsForKeys(const string& preceedingKey, const std::vector<Bigram> bigramsForKeys(const std::string& preceedingKey,
const string& key) override { const std::string& key) override {
return vector<Bigram>(); return std::vector<Bigram>();
} }
const vector<Unigram> unigramsForKey(const string& key) override { const std::vector<Unigram> unigramsForKey(const std::string& key) override {
map<string, vector<Unigram> >::const_iterator f = m_db.find(key); std::map<std::string, std::vector<Unigram> >::const_iterator f =
return f == m_db.end() ? vector<Unigram>() : (*f).second; m_db.find(key);
return f == m_db.end() ? std::vector<Unigram>() : (*f).second;
} }
bool hasUnigramsForKey(const string& key) override { bool hasUnigramsForKey(const std::string& key) override {
map<string, vector<Unigram> >::const_iterator f = m_db.find(key); std::map<std::string, std::vector<Unigram> >::const_iterator f =
m_db.find(key);
return f != m_db.end(); return f != m_db.end();
} }
protected: protected:
map<string, vector<Unigram> > m_db; std::map<std::string, std::vector<Unigram> > m_db;
}; };
TEST(GramambularTest, InputTest) { TEST(GramambularTest, InputTest) {
@ -200,15 +202,17 @@ TEST(GramambularTest, InputTest) {
Walker walker(&builder.grid()); Walker walker(&builder.grid());
vector<NodeAnchor> walked = walker.reverseWalk(builder.grid().width(), 0.0); std::vector<NodeAnchor> walked =
walker.reverseWalk(builder.grid().width(), 0.0);
reverse(walked.begin(), walked.end()); reverse(walked.begin(), walked.end());
vector<string> composed; std::vector<std::string> composed;
for (vector<NodeAnchor>::iterator wi = walked.begin(); wi != walked.end(); for (std::vector<NodeAnchor>::iterator wi = walked.begin();
++wi) { wi != walked.end(); ++wi) {
composed.push_back((*wi).node->currentKeyValue().value); composed.push_back((*wi).node->currentKeyValue().value);
} }
ASSERT_EQ(composed, (vector<string>{"高科技", "公司", "", "年中", "獎金"})); ASSERT_EQ(composed,
(std::vector<std::string>{"高科技", "公司", "", "年中", "獎金"}));
} }
TEST(GramambularTest, WordSegmentationTest) { TEST(GramambularTest, WordSegmentationTest) {
@ -226,14 +230,18 @@ TEST(GramambularTest, WordSegmentationTest) {
builder2.insertReadingAtCursor(""); builder2.insertReadingAtCursor("");
Walker walker2(&builder2.grid()); Walker walker2(&builder2.grid());
vector<NodeAnchor> walked = walker2.reverseWalk(builder2.grid().width(), 0.0); std::vector<NodeAnchor> walked =
walker2.reverseWalk(builder2.grid().width(), 0.0);
reverse(walked.begin(), walked.end()); reverse(walked.begin(), walked.end());
vector<string> segmented; std::vector<std::string> segmented;
for (vector<NodeAnchor>::iterator wi = walked.begin(); wi != walked.end(); for (std::vector<NodeAnchor>::iterator wi = walked.begin();
++wi) { wi != walked.end(); ++wi) {
segmented.push_back((*wi).node->currentKeyValue().key); segmented.push_back((*wi).node->currentKeyValue().key);
} }
ASSERT_EQ(segmented, ASSERT_EQ(segmented,
(vector<string>{"高科技", "公司", "", "年終", "獎金"})); (std::vector<std::string>{"高科技", "公司", "", "年終", "獎金"}));
} }
} // namespace Gramambular
} // namespace Formosa

View File

@ -43,34 +43,35 @@ class Grid {
size_t inSpanningLength); size_t inSpanningLength);
bool hasNodeAtLocationSpanningLengthMatchingKey(size_t inLocation, bool hasNodeAtLocationSpanningLengthMatchingKey(size_t inLocation,
size_t inSpanningLength, size_t inSpanningLength,
const string& inKey); const std::string& inKey);
void expandGridByOneAtLocation(size_t inLocation); void expandGridByOneAtLocation(size_t inLocation);
void shrinkGridByOneAtLocation(size_t inLocation); void shrinkGridByOneAtLocation(size_t inLocation);
size_t width() const; size_t width() const;
vector<NodeAnchor> nodesEndingAt(size_t inLocation); std::vector<NodeAnchor> nodesEndingAt(size_t inLocation);
vector<NodeAnchor> nodesCrossingOrEndingAt(size_t inLocation); std::vector<NodeAnchor> nodesCrossingOrEndingAt(size_t inLocation);
// "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
// evaluated to that unigram, while all other overlapping nodes will be reset // evaluated to that unigram, while all other overlapping nodes will be reset
// to their initial state (that is, if any of those nodes were "frozen" or // to their initial state (that is, if any of those nodes were "frozen" or
// fixed, they will be unfrozen.) // fixed, they will be unfrozen.)
NodeAnchor fixNodeSelectedCandidate(size_t location, const string& value); NodeAnchor fixNodeSelectedCandidate(size_t location,
const std::string& value);
// Similar to fixNodeSelectedCandidate, but instead of "freezing" the node, // Similar to fixNodeSelectedCandidate, but instead of "freezing" the node,
// only boost the unigram that represents the value with an overriding score. // only boost the unigram that represents the value with an overriding score.
// This has the same side effect as fixNodeSelectedCandidate, which is that // This has the same side effect as fixNodeSelectedCandidate, which is that
// all other overlapping nodes will be reset to their initial state. // all other overlapping nodes will be reset to their initial state.
void overrideNodeScoreForSelectedCandidate(size_t location, void overrideNodeScoreForSelectedCandidate(size_t location,
const string& value, const std::string& value,
float overridingScore); float overridingScore);
const string dumpDOT(); const std::string dumpDOT();
protected: protected:
vector<Span> m_spans; std::vector<Span> m_spans;
}; };
inline void Grid::clear() { m_spans.clear(); } inline void Grid::clear() { m_spans.clear(); }
@ -89,7 +90,7 @@ inline void Grid::insertNode(const Node& inNode, size_t inLocation,
} }
inline bool Grid::hasNodeAtLocationSpanningLengthMatchingKey( inline bool Grid::hasNodeAtLocationSpanningLengthMatchingKey(
size_t inLocation, size_t inSpanningLength, const string& inKey) { size_t inLocation, size_t inSpanningLength, const std::string& inKey) {
if (inLocation > m_spans.size()) { if (inLocation > m_spans.size()) {
return false; return false;
} }
@ -128,8 +129,8 @@ inline void Grid::shrinkGridByOneAtLocation(size_t inLocation) {
inline size_t Grid::width() const { return m_spans.size(); } inline size_t Grid::width() const { return m_spans.size(); }
inline vector<NodeAnchor> Grid::nodesEndingAt(size_t inLocation) { inline std::vector<NodeAnchor> Grid::nodesEndingAt(size_t inLocation) {
vector<NodeAnchor> result; std::vector<NodeAnchor> result;
if (m_spans.size() && inLocation <= m_spans.size()) { if (m_spans.size() && inLocation <= m_spans.size()) {
for (size_t i = 0; i < inLocation; i++) { for (size_t i = 0; i < inLocation; i++) {
@ -151,8 +152,9 @@ inline vector<NodeAnchor> Grid::nodesEndingAt(size_t inLocation) {
return result; return result;
} }
inline vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(size_t inLocation) { inline std::vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(
vector<NodeAnchor> result; size_t inLocation) {
std::vector<NodeAnchor> result;
if (m_spans.size() && inLocation <= m_spans.size()) { if (m_spans.size() && inLocation <= m_spans.size()) {
for (size_t i = 0; i < inLocation; i++) { for (size_t i = 0; i < inLocation; i++) {
@ -184,8 +186,8 @@ inline vector<NodeAnchor> Grid::nodesCrossingOrEndingAt(size_t inLocation) {
// For nodes found at the location, fix their currently-selected candidate using // For nodes found at the location, fix their currently-selected candidate using
// the supplied string value. // the supplied string value.
inline NodeAnchor Grid::fixNodeSelectedCandidate(size_t location, inline NodeAnchor Grid::fixNodeSelectedCandidate(size_t location,
const string& value) { const std::string& value) {
vector<NodeAnchor> nodes = nodesCrossingOrEndingAt(location); std::vector<NodeAnchor> nodes = nodesCrossingOrEndingAt(location);
NodeAnchor node; NodeAnchor node;
for (auto nodeAnchor : nodes) { for (auto nodeAnchor : nodes) {
auto candidates = nodeAnchor.node->candidates(); auto candidates = nodeAnchor.node->candidates();
@ -205,10 +207,9 @@ inline NodeAnchor Grid::fixNodeSelectedCandidate(size_t location,
return node; return node;
} }
inline void Grid::overrideNodeScoreForSelectedCandidate(size_t location, inline void Grid::overrideNodeScoreForSelectedCandidate(
const string& value, size_t location, const std::string& value, float overridingScore) {
float overridingScore) { std::vector<NodeAnchor> nodes = nodesCrossingOrEndingAt(location);
vector<NodeAnchor> nodes = nodesCrossingOrEndingAt(location);
for (auto nodeAnchor : nodes) { for (auto nodeAnchor : nodes) {
auto candidates = nodeAnchor.node->candidates(); auto candidates = nodeAnchor.node->candidates();
@ -225,11 +226,11 @@ inline void Grid::overrideNodeScoreForSelectedCandidate(size_t location,
} }
} }
inline const string Grid::dumpDOT() { inline const std::string Grid::dumpDOT() {
stringstream sst; std::stringstream sst;
sst << "digraph {" << endl; sst << "digraph {" << std::endl;
sst << "graph [ rankdir=LR ];" << endl; sst << "graph [ rankdir=LR ];" << std::endl;
sst << "BOS;" << endl; sst << "BOS;" << std::endl;
for (size_t p = 0; p < m_spans.size(); p++) { for (size_t p = 0; p < m_spans.size(); p++) {
Span& span = m_spans[p]; Span& span = m_spans[p];
@ -237,10 +238,10 @@ inline const string Grid::dumpDOT() {
Node* np = span.nodeOfLength(ni); Node* np = span.nodeOfLength(ni);
if (np) { if (np) {
if (!p) { if (!p) {
sst << "BOS -> " << np->currentKeyValue().value << ";" << endl; sst << "BOS -> " << np->currentKeyValue().value << ";" << std::endl;
} }
sst << np->currentKeyValue().value << ";" << endl; sst << np->currentKeyValue().value << ";" << std::endl;
if (p + ni < m_spans.size()) { if (p + ni < m_spans.size()) {
Span& dstSpan = m_spans[p + ni]; Span& dstSpan = m_spans[p + ni];
@ -248,20 +249,20 @@ inline const string Grid::dumpDOT() {
Node* dn = dstSpan.nodeOfLength(q); Node* dn = dstSpan.nodeOfLength(q);
if (dn) { if (dn) {
sst << np->currentKeyValue().value << " -> " sst << np->currentKeyValue().value << " -> "
<< dn->currentKeyValue().value << ";" << endl; << dn->currentKeyValue().value << ";" << std::endl;
} }
} }
} }
if (p + ni == m_spans.size()) { if (p + ni == m_spans.size()) {
sst << np->currentKeyValue().value << " -> " sst << np->currentKeyValue().value << " -> "
<< "EOS;" << endl; << "EOS;" << std::endl;
} }
} }
} }
} }
sst << "EOS;" << endl; sst << "EOS;" << std::endl;
sst << "}"; sst << "}";
return sst.str(); return sst.str();
} }

View File

@ -33,18 +33,18 @@
namespace Formosa { namespace Formosa {
namespace Gramambular { namespace Gramambular {
using namespace std;
class KeyValuePair { class KeyValuePair {
public: public:
string key; std::string key;
string value; std::string value;
bool operator==(const KeyValuePair& inAnother) const; bool operator==(const KeyValuePair& inAnother) const;
bool operator<(const KeyValuePair& inAnother) const; bool operator<(const KeyValuePair& inAnother) const;
}; };
inline ostream& operator<<(ostream& inStream, const KeyValuePair& inPair) { inline std::ostream& operator<<(std::ostream& inStream,
const KeyValuePair& inPair) {
inStream << "(" << inPair.key << "," << inPair.value << ")"; inStream << "(" << inPair.key << "," << inPair.value << ")";
return inStream; return inStream;
} }

View File

@ -36,16 +36,14 @@
namespace Formosa { namespace Formosa {
namespace Gramambular { namespace Gramambular {
using namespace std;
class LanguageModel { class LanguageModel {
public: public:
virtual ~LanguageModel() {} virtual ~LanguageModel() {}
virtual const vector<Bigram> bigramsForKeys(const string& preceedingKey, virtual const std::vector<Bigram> bigramsForKeys(
const string& key) = 0; const std::string& preceedingKey, const std::string& key) = 0;
virtual const vector<Unigram> unigramsForKey(const string& key) = 0; virtual const std::vector<Unigram> unigramsForKey(const std::string& key) = 0;
virtual bool hasUnigramsForKey(const string& key) = 0; virtual bool hasUnigramsForKey(const std::string& key) = 0;
}; };
} // namespace Gramambular } // namespace Gramambular
} // namespace Formosa } // namespace Formosa

View File

@ -35,47 +35,46 @@
namespace Formosa { namespace Formosa {
namespace Gramambular { namespace Gramambular {
using namespace std;
class Node { class Node {
public: public:
Node(); Node();
Node(const string& inKey, const vector<Unigram>& inUnigrams, Node(const std::string& inKey, const std::vector<Unigram>& inUnigrams,
const vector<Bigram>& inBigrams); const std::vector<Bigram>& inBigrams);
void primeNodeWithPreceedingKeyValues( void primeNodeWithPreceedingKeyValues(
const vector<KeyValuePair>& inKeyValues); const std::vector<KeyValuePair>& inKeyValues);
bool isCandidateFixed() const; bool isCandidateFixed() const;
const vector<KeyValuePair>& candidates() const; const std::vector<KeyValuePair>& candidates() const;
void selectCandidateAtIndex(size_t inIndex = 0, bool inFix = true); void selectCandidateAtIndex(size_t inIndex = 0, bool inFix = true);
void resetCandidate(); void resetCandidate();
void selectFloatingCandidateAtIndex(size_t index, double score); void selectFloatingCandidateAtIndex(size_t index, double score);
const string& key() const; const std::string& key() const;
double score() const; double score() const;
double scoreForCandidate(string& candidate) const; double scoreForCandidate(std::string& candidate) const;
const KeyValuePair currentKeyValue() const; const KeyValuePair currentKeyValue() const;
double highestUnigramScore() const; double highestUnigramScore() const;
protected: protected:
const LanguageModel* m_LM; const LanguageModel* m_LM;
string m_key; std::string m_key;
double m_score; double m_score;
vector<Unigram> m_unigrams; std::vector<Unigram> m_unigrams;
vector<KeyValuePair> m_candidates; std::vector<KeyValuePair> m_candidates;
map<string, size_t> m_valueUnigramIndexMap; std::map<std::string, size_t> m_valueUnigramIndexMap;
map<KeyValuePair, vector<Bigram> > m_preceedingGramBigramMap; std::map<KeyValuePair, std::vector<Bigram> > m_preceedingGramBigramMap;
bool m_candidateFixed; bool m_candidateFixed;
size_t m_selectedUnigramIndex; size_t m_selectedUnigramIndex;
friend ostream& operator<<(ostream& inStream, const Node& inNode); friend std::ostream& operator<<(std::ostream& inStream, const Node& inNode);
}; };
inline ostream& operator<<(ostream& inStream, const Node& inNode) { inline std::ostream& operator<<(std::ostream& inStream, const Node& inNode) {
inStream << "(node,key:" << inNode.m_key inStream << "(node,key:" << inNode.m_key
<< ",fixed:" << (inNode.m_candidateFixed ? "true" : "false") << ",fixed:" << (inNode.m_candidateFixed ? "true" : "false")
<< ",selected:" << inNode.m_selectedUnigramIndex << "," << ",selected:" << inNode.m_selectedUnigramIndex << ","
@ -86,8 +85,9 @@ inline ostream& operator<<(ostream& inStream, const Node& inNode) {
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 string& inKey, const vector<Unigram>& inUnigrams, inline Node::Node(const std::string& inKey,
const vector<Bigram>& inBigrams) const std::vector<Unigram>& inUnigrams,
const std::vector<Bigram>& inBigrams)
: m_key(inKey), : m_key(inKey),
m_unigrams(inUnigrams), m_unigrams(inUnigrams),
m_candidateFixed(false), m_candidateFixed(false),
@ -100,7 +100,7 @@ inline Node::Node(const string& inKey, const vector<Unigram>& inUnigrams,
} }
size_t i = 0; size_t i = 0;
for (vector<Unigram>::const_iterator ui = m_unigrams.begin(); for (std::vector<Unigram>::const_iterator ui = m_unigrams.begin();
ui != m_unigrams.end(); ++ui) { ui != m_unigrams.end(); ++ui) {
m_valueUnigramIndexMap[(*ui).keyValue.value] = i; m_valueUnigramIndexMap[(*ui).keyValue.value] = i;
i++; i++;
@ -108,30 +108,30 @@ inline Node::Node(const string& inKey, const vector<Unigram>& inUnigrams,
m_candidates.push_back((*ui).keyValue); m_candidates.push_back((*ui).keyValue);
} }
for (vector<Bigram>::const_iterator bi = inBigrams.begin(); for (std::vector<Bigram>::const_iterator bi = inBigrams.begin();
bi != inBigrams.end(); ++bi) { bi != inBigrams.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 vector<KeyValuePair>& inKeyValues) { const std::vector<KeyValuePair>& inKeyValues) {
size_t newIndex = m_selectedUnigramIndex; size_t newIndex = m_selectedUnigramIndex;
double max = m_score; double max = m_score;
if (!isCandidateFixed()) { if (!isCandidateFixed()) {
for (vector<KeyValuePair>::const_iterator kvi = inKeyValues.begin(); for (std::vector<KeyValuePair>::const_iterator kvi = inKeyValues.begin();
kvi != inKeyValues.end(); ++kvi) { kvi != inKeyValues.end(); ++kvi) {
map<KeyValuePair, 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()) {
const vector<Bigram>& bigrams = (*f).second; const std::vector<Bigram>& bigrams = (*f).second;
for (vector<Bigram>::const_iterator bi = bigrams.begin(); for (std::vector<Bigram>::const_iterator bi = bigrams.begin();
bi != bigrams.end(); ++bi) { bi != bigrams.end(); ++bi) {
const Bigram& bigram = *bi; const Bigram& bigram = *bi;
if (bigram.score > max) { if (bigram.score > max) {
map<string, size_t>::const_iterator uf = std::map<std::string, size_t>::const_iterator uf =
m_valueUnigramIndexMap.find((*bi).keyValue.value); m_valueUnigramIndexMap.find((*bi).keyValue.value);
if (uf != m_valueUnigramIndexMap.end()) { if (uf != m_valueUnigramIndexMap.end()) {
newIndex = (*uf).second; newIndex = (*uf).second;
@ -154,7 +154,7 @@ inline void Node::primeNodeWithPreceedingKeyValues(
inline bool Node::isCandidateFixed() const { return m_candidateFixed; } inline bool Node::isCandidateFixed() const { return m_candidateFixed; }
inline const vector<KeyValuePair>& Node::candidates() const { inline const std::vector<KeyValuePair>& Node::candidates() const {
return m_candidates; return m_candidates;
} }
@ -187,11 +187,11 @@ inline void Node::selectFloatingCandidateAtIndex(size_t index, double score) {
m_score = score; m_score = score;
} }
inline const string& Node::key() const { return m_key; } inline const std::string& Node::key() const { return m_key; }
inline double Node::score() const { return m_score; } inline double Node::score() const { return m_score; }
inline double Node::scoreForCandidate(string& candidate) const { inline double Node::scoreForCandidate(std::string& candidate) const {
for (auto unigram : m_unigrams) { for (auto unigram : m_unigrams) {
if (unigram.keyValue.value == candidate) { if (unigram.keyValue.value == candidate) {
return unigram.score; return unigram.score;

View File

@ -44,7 +44,8 @@ class NodeAnchor {
inline NodeAnchor::NodeAnchor() inline NodeAnchor::NodeAnchor()
: node(0), location(0), spanningLength(0), accumulatedScore(0.0) {} : node(0), location(0), spanningLength(0), accumulatedScore(0.0) {}
inline ostream& operator<<(ostream& inStream, const NodeAnchor& inAnchor) { inline std::ostream& operator<<(std::ostream& inStream,
const NodeAnchor& inAnchor) {
inStream << "{@(" << inAnchor.location << "," << inAnchor.spanningLength inStream << "{@(" << inAnchor.location << "," << inAnchor.spanningLength
<< "),"; << "),";
if (inAnchor.node) { if (inAnchor.node) {
@ -56,9 +57,9 @@ inline ostream& operator<<(ostream& inStream, const NodeAnchor& inAnchor) {
return inStream; return inStream;
} }
inline ostream& operator<<(ostream& inStream, inline std::ostream& operator<<(std::ostream& inStream,
const vector<NodeAnchor>& inAnchor) { const std::vector<NodeAnchor>& inAnchor) {
for (vector<NodeAnchor>::const_iterator i = inAnchor.begin(); for (std::vector<NodeAnchor>::const_iterator i = inAnchor.begin();
i != inAnchor.end(); ++i) { i != inAnchor.end(); ++i) {
inStream << *i; inStream << *i;
if (i + 1 != inAnchor.end()) { if (i + 1 != inAnchor.end()) {

View File

@ -48,7 +48,7 @@ class Span {
size_t maximumLength() const; size_t maximumLength() const;
protected: protected:
map<size_t, Node> m_lengthNodeMap; std::map<size_t, Node> m_lengthNodeMap;
size_t m_maximumLength; size_t m_maximumLength;
}; };
@ -72,9 +72,9 @@ inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
} }
size_t max = 0; size_t max = 0;
set<size_t> removeSet; std::set<size_t> removeSet;
for (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 > inLength) {
removeSet.insert((*i).first); removeSet.insert((*i).first);
@ -85,8 +85,8 @@ inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
} }
} }
for (set<size_t>::iterator i = removeSet.begin(), e = removeSet.end(); i != e; for (std::set<size_t>::iterator i = removeSet.begin(), e = removeSet.end();
++i) { i != e; ++i) {
m_lengthNodeMap.erase(*i); m_lengthNodeMap.erase(*i);
} }
@ -94,7 +94,7 @@ inline void Span::removeNodeOfLengthGreaterThan(size_t inLength) {
} }
inline Node* Span::nodeOfLength(size_t inLength) { inline Node* Span::nodeOfLength(size_t inLength) {
map<size_t, Node>::iterator f = m_lengthNodeMap.find(inLength); std::map<size_t, Node>::iterator f = m_lengthNodeMap.find(inLength);
return f == m_lengthNodeMap.end() ? 0 : &(*f).second; return f == m_lengthNodeMap.end() ? 0 : &(*f).second;
} }

View File

@ -47,20 +47,21 @@ class Unigram {
static bool ScoreCompare(const Unigram& a, const Unigram& b); static bool ScoreCompare(const Unigram& a, const Unigram& b);
}; };
inline ostream& operator<<(ostream& inStream, const Unigram& inGram) { inline std::ostream& operator<<(std::ostream& inStream, const Unigram& inGram) {
streamsize p = inStream.precision(); std::streamsize p = inStream.precision();
inStream.precision(6); inStream.precision(6);
inStream << "(" << inGram.keyValue << "," << inGram.score << ")"; inStream << "(" << inGram.keyValue << "," << inGram.score << ")";
inStream.precision(p); inStream.precision(p);
return inStream; return inStream;
} }
inline ostream& operator<<(ostream& inStream, const vector<Unigram>& inGrams) { inline std::ostream& operator<<(std::ostream& inStream,
const std::vector<Unigram>& inGrams) {
inStream << "[" << inGrams.size() << "]=>{"; inStream << "[" << inGrams.size() << "]=>{";
size_t index = 0; size_t index = 0;
for (vector<Unigram>::const_iterator gi = inGrams.begin(); for (std::vector<Unigram>::const_iterator gi = inGrams.begin();
gi != inGrams.end(); ++gi, ++index) { gi != inGrams.end(); ++gi, ++index) {
inStream << index << "=>"; inStream << index << "=>";
inStream << *gi; inStream << *gi;

View File

@ -34,13 +34,12 @@
namespace Formosa { namespace Formosa {
namespace Gramambular { namespace Gramambular {
using namespace std;
class Walker { class Walker {
public: public:
Walker(Grid* inGrid); Walker(Grid* inGrid);
const vector<NodeAnchor> reverseWalk(size_t inLocation, const std::vector<NodeAnchor> reverseWalk(size_t inLocation,
double inAccumulatedScore = 0.0); double inAccumulatedScore = 0.0);
protected: protected:
Grid* m_grid; Grid* m_grid;
@ -48,17 +47,17 @@ class Walker {
inline Walker::Walker(Grid* inGrid) : m_grid(inGrid) {} inline Walker::Walker(Grid* inGrid) : m_grid(inGrid) {}
inline const vector<NodeAnchor> Walker::reverseWalk(size_t inLocation, inline const std::vector<NodeAnchor> Walker::reverseWalk(
double inAccumulatedScore) { size_t inLocation, double inAccumulatedScore) {
if (!inLocation || inLocation > m_grid->width()) { if (!inLocation || inLocation > m_grid->width()) {
return vector<NodeAnchor>(); return std::vector<NodeAnchor>();
} }
vector<vector<NodeAnchor> > paths; std::vector<std::vector<NodeAnchor> > paths;
vector<NodeAnchor> nodes = m_grid->nodesEndingAt(inLocation); std::vector<NodeAnchor> nodes = m_grid->nodesEndingAt(inLocation);
for (vector<NodeAnchor>::iterator ni = nodes.begin(); ni != nodes.end(); for (std::vector<NodeAnchor>::iterator ni = nodes.begin(); ni != nodes.end();
++ni) { ++ni) {
if (!(*ni).node) { if (!(*ni).node) {
continue; continue;
@ -66,7 +65,7 @@ inline const vector<NodeAnchor> Walker::reverseWalk(size_t inLocation,
(*ni).accumulatedScore = inAccumulatedScore + (*ni).node->score(); (*ni).accumulatedScore = inAccumulatedScore + (*ni).node->score();
vector<NodeAnchor> path = std::vector<NodeAnchor> path =
reverseWalk(inLocation - (*ni).spanningLength, (*ni).accumulatedScore); reverseWalk(inLocation - (*ni).spanningLength, (*ni).accumulatedScore);
path.insert(path.begin(), *ni); path.insert(path.begin(), *ni);
@ -74,11 +73,11 @@ inline const vector<NodeAnchor> Walker::reverseWalk(size_t inLocation,
} }
if (!paths.size()) { if (!paths.size()) {
return vector<NodeAnchor>(); return std::vector<NodeAnchor>();
} }
vector<NodeAnchor>* result = &*(paths.begin()); std::vector<NodeAnchor>* result = &*(paths.begin());
for (vector<vector<NodeAnchor> >::iterator pi = paths.begin(); for (std::vector<std::vector<NodeAnchor> >::iterator pi = paths.begin();
pi != paths.end(); ++pi) { pi != paths.end(); ++pi) {
if ((*pi).back().accumulatedScore > result->back().accumulatedScore) { if ((*pi).back().accumulatedScore > result->back().accumulatedScore) {
result = &*pi; result = &*pi;