summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2013-11-13 18:12:10 +0100
committerPatrick Simianer <p@simianer.de>2013-11-13 18:12:10 +0100
commit062d8af12f2bcad39c47b42295b69f44f878768f (patch)
treea455fb5dd1a3c01ca3fa6e2ddd5e368040e32eaa /utils
parentb8bf706976720527b455eb665fe94f907e372b65 (diff)
parentf83186887c94b2ff8b17aefcd0b395f116c09eb6 (diff)
merge w/ upstream
Diffstat (limited to 'utils')
-rw-r--r--utils/atools.cc8
-rw-r--r--utils/hash.h12
-rw-r--r--utils/weights.cc24
-rw-r--r--utils/weights.h5
4 files changed, 43 insertions, 6 deletions
diff --git a/utils/atools.cc b/utils/atools.cc
index 1726c4ac..559eadef 100644
--- a/utils/atools.cc
+++ b/utils/atools.cc
@@ -203,14 +203,16 @@ struct RefineCommand : public Command {
bool keep_going = !p.empty();
while (keep_going) {
keep_going = false;
- for (set<pair<int, int> >::iterator pi = p.begin();
- pi != p.end(); ++pi) {
+ set<pair<int, int> > added;
+ for (set<pair<int, int> >::iterator pi = p.begin(); pi != p.end(); ++pi) {
if ((this->*pred)(pi->first, pi->second)) {
Align(pi->first, pi->second);
- p.erase(pi);
+ added.insert(make_pair(pi->first, pi->second));
keep_going = true;
}
}
+ for (set<pair<int, int> >::iterator ai = added.begin(); ai != added.end(); ++ai)
+ p.erase(*ai);
}
}
Array2D<bool> res_; // refined alignment
diff --git a/utils/hash.h b/utils/hash.h
index 189ed1ae..e1426ffb 100644
--- a/utils/hash.h
+++ b/utils/hash.h
@@ -20,11 +20,17 @@
# define HASH_MAP_RESERVED(h,empty,deleted) do { (h).set_empty_key(empty); (h).set_deleted_key(deleted); } while(0)
# define HASH_MAP_EMPTY(h,empty) do { (h).set_empty_key(empty); } while(0)
#else
+#ifndef HAVE_OLD_CPP
+# include <unordered_map>
+# include <unordered_set>
+#else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
-# define SPARSE_HASH_MAP std::tr1::unordered_map
-# define HASH_MAP std::tr1::unordered_map
-# define HASH_SET std::tr1::unordered_set
+namespace std { using std::tr1::unordered_map; using std::tr1::unordered_set; }
+#endif
+# define SPARSE_HASH_MAP std::unordered_map
+# define HASH_MAP std::unordered_map
+# define HASH_SET std::unordered_set
# define HASH_MAP_DELETED(h,deleted)
# define HASH_MAP_RESERVED(h,empty,deleted)
# define HASH_MAP_EMPTY(h,empty)
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);
+ }
+}
diff --git a/utils/weights.h b/utils/weights.h
index 30f71db0..920fdd75 100644
--- a/utils/weights.h
+++ b/utils/weights.h
@@ -23,6 +23,11 @@ class Weights {
static void SanityCheck(const std::vector<weight_t>& w);
// write weights with largest magnitude to cerr
static void ShowLargestFeatures(const std::vector<weight_t>& w);
+ static std::string GetString(const std::vector<weight_t>& w,
+ bool hide_zero_value_features = true);
+ // Assumes weights are already initialized for now
+ static void UpdateFromString(std::string& w_string,
+ std::vector<weight_t>& w);
private:
Weights();
};