00001 #pragma once
00002
00003 #include <vector>
00004 #include <string>
00005
00006 #include <boost/unordered_map.hpp>
00007
00008 namespace MosesTuning
00009 {
00010
00015 class NgramCounts
00016 {
00017 public:
00018
00019 struct NgramComparator {
00020 bool operator()(const std::vector<int>& a, const std::vector<int>& b) const {
00021 std::size_t i;
00022 const std::size_t as = a.size();
00023 const std::size_t bs = b.size();
00024 for (i = 0; i < as && i < bs; ++i) {
00025 if (a[i] < b[i]) {
00026 return true;
00027 }
00028 if (a[i] > b[i]) {
00029 return false;
00030 }
00031 }
00032
00033 return as < bs;
00034 }
00035 };
00036
00037 typedef std::vector<int> Key;
00038 typedef int Value;
00039 typedef boost::unordered_map<Key, Value>::iterator iterator;
00040 typedef boost::unordered_map<Key, Value>::const_iterator const_iterator;
00041
00042 NgramCounts() : kDefaultCount(1) { }
00043 virtual ~NgramCounts() { }
00044
00048 inline void Add(const Key& ngram) {
00049 m_counts[ngram]++;
00050 }
00051
00055 bool Lookup(const Key& ngram, Value* v) const {
00056 const_iterator it = m_counts.find(ngram);
00057 if (it == m_counts.end()) return false;
00058 *v = it->second;
00059 return true;
00060 }
00061
00065 void clear() {
00066 m_counts.clear();
00067 }
00068
00072 bool empty() const {
00073 return m_counts.empty();
00074 }
00075
00079 std::size_t size() const {
00080 return m_counts.size();
00081 }
00082
00083 std::size_t max_size() const {
00084 return m_counts.max_size();
00085 }
00086
00087
00088 int get_default_count() const {
00089 return kDefaultCount;
00090 }
00091
00092 iterator find(const Key& ngram) {
00093 return m_counts.find(ngram);
00094 }
00095 const_iterator find(const Key& ngram) const {
00096 return m_counts.find(ngram);
00097 }
00098
00099 Value& operator[](const Key& ngram) {
00100 return m_counts[ngram];
00101 }
00102
00103 iterator begin() {
00104 return m_counts.begin();
00105 }
00106 const_iterator begin() const {
00107 return m_counts.begin();
00108 }
00109 iterator end() {
00110 return m_counts.end();
00111 }
00112 const_iterator end() const {
00113 return m_counts.end();
00114 }
00115
00116 private:
00117 const int kDefaultCount;
00118 boost::unordered_map<Key, Value> m_counts;
00119 };
00120
00121 }
00122