File size: 1,120 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * StoreVocab.h
 *
 *  Created on: 15 Jun 2016
 *      Author: hieu
 */
#pragma once
#include <string>
#include <boost/unordered_map.hpp>
#include "OutputFileStream.h"
#include "moses2/legacy/Util2.h"

namespace probingpt
{

template<typename VOCABID>
class StoreVocab
{
protected:
  std::string m_path;

  typedef boost::unordered_map<std::string, VOCABID> Coll;
  Coll m_vocab;

public:
  StoreVocab(const std::string &path)
    :m_path(path)
  {}

  virtual ~StoreVocab() {}

  VOCABID GetVocabId(const std::string &word) {
    typename Coll::iterator iter = m_vocab.find(word);
    if (iter == m_vocab.end()) {
      VOCABID ind = m_vocab.size() + 1;
      m_vocab[word] = ind;
      return ind;
    } else {
      return iter->second;
    }
  }

  void Insert(VOCABID id, const std::string &word) {
    m_vocab[word] = id;
  }

  void Save() {
    OutputFileStream strme(m_path);

    typename Coll::const_iterator iter;
    for (iter = m_vocab.begin(); iter != m_vocab.end(); ++iter) {
      strme << iter->first << "\t" << iter->second << std::endl;
    }

    strme.Close();
  }
};

} /* namespace Moses2 */