Add a function to check whether a string ends with a suffix.

- Use the function in Data::InitFeatureMap().
- Add an unit test for InitFeatureMap().
- Move helper functions for Data::loadnbest() to public for unit testing.
This commit is contained in:
Tetsuo Kiso 2012-04-04 22:04:51 +09:00
parent 8782b6df38
commit 27515f5de1
4 changed files with 24 additions and 7 deletions

View File

@ -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);

View File

@ -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_

View File

@ -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());
}

View File

@ -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)
{