diff options
author | Patrick Simianer <p@simianer.de> | 2013-11-13 18:12:10 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2013-11-13 18:12:10 +0100 |
commit | d6e6babf2cfe49fed040b651624b7e34d1a9b507 (patch) | |
tree | 2a00ab18f10a7f93e7e172551c01b48cc9f20b8c /utils/weights.cc | |
parent | 2d2d5eced93d58bc77894d8c328195cd9950b96d (diff) | |
parent | 8a24bb77bc2e9fd17a6f6529a2942cde96a6af49 (diff) |
merge w/ upstream
Diffstat (limited to 'utils/weights.cc')
-rw-r--r-- | utils/weights.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/utils/weights.cc b/utils/weights.cc index 575877b6..effdfc5e 100644 --- a/utils/weights.cc +++ b/utils/weights.cc @@ -4,6 +4,7 @@ #include "fdict.h" #include "filelib.h" +#include "stringlib.h" #include "verbose.h" using namespace std; @@ -156,4 +157,27 @@ void Weights::ShowLargestFeatures(const vector<weight_t>& w) { cerr << endl; } +string Weights::GetString(const vector<weight_t>& w, + bool hide_zero_value_features) { + ostringstream os; + os.precision(17); + int nf = FD::NumFeats(); + for (unsigned i = 1; i < nf; i++) { + weight_t val = (i < w.size() ? w[i] : 0.0); + if (hide_zero_value_features && val == 0.0) { + continue; + } + os << ' ' << FD::Convert(i) << '=' << val; + } + return os.str().substr(1); +} +void Weights::UpdateFromString(string& w_string, + vector<weight_t>& w) { + vector<string> tok = SplitOnWhitespace(w_string); + for (vector<string>::iterator i = tok.begin(); i != tok.end(); i++) { + int delim = i->find('='); + int fid = FD::Convert(i->substr(0, delim)); + w[fid] = strtod(i->substr(delim + 1).c_str(), NULL); + } +} |