diff options
-rw-r--r-- | mert/Data.cpp | 3 | ||||
-rw-r--r-- | mert/Data.h | 10 | ||||
-rw-r--r-- | mert/DataTest.cpp | 10 | ||||
-rw-r--r-- | mert/Util.h | 8 |
4 files changed, 24 insertions, 7 deletions
diff --git a/mert/Data.cpp b/mert/Data.cpp index b1950ea4e..b0b8f12be 100644 --- a/mert/Data.cpp +++ b/mert/Data.cpp @@ -173,14 +173,13 @@ void Data::InitFeatureMap(const string& str) { string features = ""; string tmp_name = ""; size_t tmp_index = 0; - string::size_type loc; char tmp[64]; // for snprintf(); while (!buf.empty()) { getNextPound(buf, substr); // string ending with ":" are skipped, because they are the names of the features - if ((loc = substr.find_last_of(":")) != substr.length()-1) { + if (!EndsWith(substr, ":")) { snprintf(tmp, sizeof(tmp), "%s_%lu ", tmp_name.c_str(), tmp_index); features.append(tmp); diff --git a/mert/Data.h b/mert/Data.h index 376367d4c..1b5815f77 100644 --- a/mert/Data.h +++ b/mert/Data.h @@ -35,11 +35,6 @@ private: ScoreDataHandle m_score_data; FeatureDataHandle m_feature_data; - // Helper functions for loadnbest(); - void InitFeatureMap(const std::string& str); - void AddFeatures(const std::string& str, - const std::string& sentence_index); - public: explicit Data(Scorer* scorer); Data(); @@ -97,6 +92,11 @@ public: */ void createShards(size_t shard_count, float shard_size, const std::string& scorerconfig, std::vector<Data>& shards); + + // Helper functions for loadnbest(); + void InitFeatureMap(const std::string& str); + void AddFeatures(const std::string& str, + const std::string& sentence_index); }; #endif // MERT_DATA_H_ diff --git a/mert/DataTest.cpp b/mert/DataTest.cpp index bf644fe1a..43cf9bb24 100644 --- a/mert/DataTest.cpp +++ b/mert/DataTest.cpp @@ -36,3 +36,13 @@ BOOST_AUTO_TEST_CASE(shard_basic) { BOOST_CHECK_EQUAL(shards.size(),2); BOOST_CHECK_EQUAL(shards[1].getFeatureData()->size(),2); } + +BOOST_AUTO_TEST_CASE(init_feature_map_test) { + boost::scoped_ptr<Scorer> scorer(ScorerFactory::getScorer("BLEU", "")); + Data data(scorer.get()); + + std::string s = " d: 0 -7.66174 0 0 -3.51621 0 0 lm: -41.3435 -40.3647 tm: -67.6349 -100.438 -27.6817 -23.4685 8.99907 w: -9 "; + std::string expected = "d_0 d_1 d_2 d_3 d_4 d_5 d_6 lm_0 lm_1 tm_0 tm_1 tm_2 tm_3 tm_4 w_0 "; + data.InitFeatureMap(s); + BOOST_CHECK_EQUAL(expected, data.Features()); +} diff --git a/mert/Util.h b/mert/Util.h index 415dddd33..24a86b370 100644 --- a/mert/Util.h +++ b/mert/Util.h @@ -66,6 +66,14 @@ inline T Scan(const std::string &input) return ret; } +/** + * Returns true iff "str" ends with "suffix". + * e.g., Given str = "abc:", suffix = ":", this functions returns true. + */ +bool EndsWith(const std::string& str, const char* suffix) { + return str.find_last_of(suffix) == str.size() - 1; +} + template<typename T> inline std::string stringify(T x) { |