summaryrefslogtreecommitdiff
path: root/extractor/translation_table.h
blob: a7be26f5babfa5f76db6f318dc5dd3e54e2b3f24 (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
#ifndef _TRANSLATION_TABLE_
#define _TRANSLATION_TABLE_

#include <memory>
#include <string>
#include <unordered_map>

#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>

using namespace std;
namespace fs = boost::filesystem;

namespace extractor {

typedef boost::hash<pair<int, int> > PairHash;

class Alignment;
class DataArray;

class TranslationTable {
 public:
  TranslationTable(
      shared_ptr<DataArray> source_data_array,
      shared_ptr<DataArray> target_data_array,
      shared_ptr<Alignment> alignment);

  virtual ~TranslationTable();

  virtual double GetTargetGivenSourceScore(const string& source_word,
                                           const string& target_word);

  virtual double GetSourceGivenTargetScore(const string& source_word,
                                           const string& target_word);

  void WriteBinary(const fs::path& filepath) const;

 protected:
  TranslationTable();

 private:
  void IncreaseLinksCount(
      unordered_map<int, int>& source_links_count,
      unordered_map<int, int>& target_links_count,
      unordered_map<pair<int, int>, int, PairHash>& links_count,
      int source_word_id,
      int target_word_id) const;

  shared_ptr<DataArray> source_data_array;
  shared_ptr<DataArray> target_data_array;
  unordered_map<pair<int, int>, pair<double, double>, PairHash>
      translation_probabilities;
};

} // namespace extractor

#endif