2012-03-14 17:14:11 +04:00
|
|
|
#ifndef MERT_NGRAM_H_
|
|
|
|
#define MERT_NGRAM_H_
|
|
|
|
|
|
|
|
#include <vector>
|
2012-03-19 18:21:02 +04:00
|
|
|
#include <string>
|
|
|
|
|
2012-12-06 17:08:33 +04:00
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2012-12-06 17:08:33 +04:00
|
|
|
|
2012-03-14 17:14:11 +04:00
|
|
|
/** A simple STL-std::map based n-gram counts. Basically, we provide
|
|
|
|
* typical accessors and mutaors, but we intentionally does not allow
|
|
|
|
* erasing elements.
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
class NgramCounts
|
|
|
|
{
|
|
|
|
public:
|
2012-03-14 17:14:11 +04:00
|
|
|
// Used to construct the ngram map
|
|
|
|
struct NgramComparator {
|
|
|
|
bool operator()(const std::vector<int>& a, const std::vector<int>& b) const {
|
2012-03-19 21:32:55 +04:00
|
|
|
std::size_t i;
|
|
|
|
const std::size_t as = a.size();
|
|
|
|
const std::size_t bs = b.size();
|
2012-03-14 17:14:11 +04:00
|
|
|
for (i = 0; i < as && i < bs; ++i) {
|
|
|
|
if (a[i] < b[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a[i] > b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// entries are equal, shortest wins
|
|
|
|
return as < bs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<int> Key;
|
|
|
|
typedef int Value;
|
2012-12-06 17:08:33 +04:00
|
|
|
typedef boost::unordered_map<Key, Value>::iterator iterator;
|
|
|
|
typedef boost::unordered_map<Key, Value>::const_iterator const_iterator;
|
2012-03-14 17:14:11 +04:00
|
|
|
|
|
|
|
NgramCounts() : kDefaultCount(1) { }
|
|
|
|
virtual ~NgramCounts() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the specified "ngram" is found, we add counts.
|
|
|
|
* If not, we insert the default count in the container. */
|
2013-05-29 21:16:15 +04:00
|
|
|
inline void Add(const Key& ngram) {
|
|
|
|
m_counts[ngram]++;
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2012-03-19 18:21:02 +04:00
|
|
|
/**
|
|
|
|
* Return true iff the specified "ngram" is found in the container.
|
|
|
|
*/
|
|
|
|
bool Lookup(const Key& ngram, Value* v) const {
|
|
|
|
const_iterator it = m_counts.find(ngram);
|
|
|
|
if (it == m_counts.end()) return false;
|
|
|
|
*v = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-14 17:14:11 +04:00
|
|
|
/**
|
|
|
|
* Clear all elments in the container.
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
void clear() {
|
|
|
|
m_counts.clear();
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true iff the container is empty.
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
bool empty() const {
|
|
|
|
return m_counts.empty();
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the the number of elements in the container.
|
|
|
|
*/
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t size() const {
|
|
|
|
return m_counts.size();
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t max_size() const {
|
|
|
|
return m_counts.max_size();
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
|
|
|
// Note: This is mainly used by unit tests.
|
2013-05-29 21:16:15 +04:00
|
|
|
int get_default_count() const {
|
|
|
|
return kDefaultCount;
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
iterator find(const Key& ngram) {
|
|
|
|
return m_counts.find(ngram);
|
|
|
|
}
|
|
|
|
const_iterator find(const Key& ngram) const {
|
|
|
|
return m_counts.find(ngram);
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
Value& operator[](const Key& ngram) {
|
|
|
|
return m_counts[ngram];
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
iterator begin() {
|
|
|
|
return m_counts.begin();
|
|
|
|
}
|
|
|
|
const_iterator begin() const {
|
|
|
|
return m_counts.begin();
|
|
|
|
}
|
|
|
|
iterator end() {
|
|
|
|
return m_counts.end();
|
|
|
|
}
|
|
|
|
const_iterator end() const {
|
|
|
|
return m_counts.end();
|
|
|
|
}
|
2012-03-14 17:14:11 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
private:
|
2012-03-14 17:14:11 +04:00
|
|
|
const int kDefaultCount;
|
2012-12-06 17:08:33 +04:00
|
|
|
boost::unordered_map<Key, Value> m_counts;
|
2012-03-14 17:14:11 +04:00
|
|
|
};
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
}
|
|
|
|
|
2012-03-14 17:14:11 +04:00
|
|
|
#endif // MERT_NGRAM_H_
|