Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Ngram.h « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de270360525ab181a86f01156ed84d540af4b2ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once

#include <vector>
#include <string>

#include <boost/unordered_map.hpp>

namespace MosesTuning
{

/** A simple STL-std::map based n-gram counts. Basically, we provide
 * typical accessors and mutaors, but we intentionally does not allow
 * erasing elements.
 */
class NgramCounts
{
public:
  // Used to construct the ngram map
  struct NgramComparator {
    bool operator()(const std::vector<int>& a, const std::vector<int>& b) const {
      std::size_t i;
      const std::size_t as = a.size();
      const std::size_t bs = b.size();
      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;
  typedef boost::unordered_map<Key, Value>::iterator iterator;
  typedef boost::unordered_map<Key, Value>::const_iterator const_iterator;

  NgramCounts() : kDefaultCount(1) { }
  virtual ~NgramCounts() { }

  /**
   * If the specified "ngram" is found, we add counts.
   * If not, we insert the default count in the container. */
  inline void Add(const Key& ngram) {
    m_counts[ngram]++;
  }

  /**
   * 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;
  }

  /**
   * Clear all elments in the container.
   */
  void clear() {
    m_counts.clear();
  }

  /**
   * Return true iff the container is empty.
   */
  bool empty() const {
    return m_counts.empty();
  }

  /**
   * Return the the number of elements in the container.
   */
  std::size_t size() const {
    return m_counts.size();
  }

  std::size_t max_size() const {
    return m_counts.max_size();
  }

  // Note: This is mainly used by unit tests.
  int get_default_count() const {
    return kDefaultCount;
  }

  iterator find(const Key& ngram) {
    return m_counts.find(ngram);
  }
  const_iterator find(const Key& ngram) const {
    return m_counts.find(ngram);
  }

  Value& operator[](const Key& ngram) {
    return m_counts[ngram];
  }

  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();
  }

private:
  const int kDefaultCount;
  boost::unordered_map<Key, Value> m_counts;
};

}