2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_LVoc_h
|
|
|
|
#define moses_LVoc_h
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
#include<map>
|
|
|
|
#include<vector>
|
|
|
|
#include<iostream>
|
|
|
|
#include<fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
typedef unsigned LabelId;
|
|
|
|
extern const LabelId InvalidLabelId;
|
|
|
|
extern const LabelId Epsilon;
|
|
|
|
|
|
|
|
typedef std::vector<LabelId> IPhrase;
|
|
|
|
|
2012-06-27 03:45:02 +04:00
|
|
|
/** class used in phrase-based binary phrase-table.
|
|
|
|
* @todo vocab?
|
|
|
|
* A = type of things to numberize, ie, std::string
|
|
|
|
* B = map type to use, might consider using hash_map for better performance
|
|
|
|
*/
|
2008-06-11 14:52:57 +04:00
|
|
|
template<typename A,typename B=std::map<A,LabelId> >
|
2011-02-24 16:14:42 +03:00
|
|
|
class LVoc
|
|
|
|
{
|
2008-06-11 14:52:57 +04:00
|
|
|
typedef A Key;
|
|
|
|
typedef B M;
|
|
|
|
typedef std::vector<Key> V;
|
|
|
|
M m;
|
|
|
|
V data;
|
|
|
|
public:
|
|
|
|
LVoc() {}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
bool isKnown(const Key& k) const {
|
|
|
|
return m.find(k)!=m.end();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
LabelId index(const Key& k) const {
|
|
|
|
typename M::const_iterator i=m.find(k);
|
2011-02-24 16:14:42 +03:00
|
|
|
return i!=m.end()? i->second : InvalidLabelId;
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
LabelId add(const Key& k) {
|
|
|
|
std::pair<typename M::iterator,bool> p
|
2013-07-08 23:52:14 +04:00
|
|
|
=m.insert(std::make_pair(k,data.size()));
|
2008-06-11 14:52:57 +04:00
|
|
|
if(p.second) data.push_back(k);
|
2013-11-19 22:52:15 +04:00
|
|
|
assert(static_cast<size_t>(p.first->second)<data.size());
|
2008-06-11 14:52:57 +04:00
|
|
|
return p.first->second;
|
|
|
|
}
|
|
|
|
Key const& symbol(LabelId i) const {
|
2013-11-19 22:52:15 +04:00
|
|
|
assert(static_cast<size_t>(i)<data.size());
|
2011-02-24 16:14:42 +03:00
|
|
|
return data[i];
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
typedef typename V::const_iterator const_iterator;
|
2011-02-24 16:14:42 +03:00
|
|
|
const_iterator begin() const {
|
|
|
|
return data.begin();
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return data.end();
|
|
|
|
}
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
void Write(const std::string& fname) const {
|
2011-02-24 16:14:42 +03:00
|
|
|
std::ofstream out(fname.c_str());
|
|
|
|
Write(out);
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
void Write(std::ostream& out) const {
|
2011-02-24 16:14:42 +03:00
|
|
|
for(int i=data.size()-1; i>=0; --i)
|
|
|
|
out<<i<<' '<<data[i]<<'\n';
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
void Read(const std::string& fname) {
|
2011-02-24 16:14:42 +03:00
|
|
|
std::ifstream in(fname.c_str());
|
|
|
|
Read(in);
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
void Read(std::istream& in) {
|
2011-02-24 16:14:42 +03:00
|
|
|
Key k;
|
|
|
|
size_t i;
|
|
|
|
std::string line;
|
|
|
|
while(getline(in,line)) {
|
|
|
|
std::istringstream is(line);
|
|
|
|
if(is>>i>>k) {
|
|
|
|
if(i>=data.size()) data.resize(i+1);
|
|
|
|
data[i]=k;
|
|
|
|
m[k]=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
};
|
|
|
|
|
2010-02-24 14:15:44 +03:00
|
|
|
#endif
|