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

neuralTM.h « src - github.com/moses-smt/nplm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c3db51cac1578c34a2514c2dfc7c0aa5fbee5df (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef NEURALTM_H
#define NEURALTM_H

#include <vector>
#include <cctype>
#include <cstdlib>
#include <boost/shared_ptr.hpp>

#include <Eigen/Dense>

#include "util.h"
#include "vocabulary.h"
#include "neuralNetwork.h"
#include "replace_digits.hpp"

namespace nplm
{

class neuralTM : public neuralNetwork, graehl::replace_digits
{
  boost::shared_ptr<vocabulary> input_vocab, output_vocab;
  int start, null;

 public:
  neuralTM()
      : neuralNetwork(),
        graehl::replace_digits(0),
        input_vocab(new vocabulary()),
        output_vocab(new vocabulary())
  {
  }

  void set_map_digits(char value) { map_digits = value; }

  void set_input_vocabulary(const vocabulary &vocab)
  {
    *(this->input_vocab) = vocab;
    start = vocab.lookup_word("<s>");
    null = vocab.lookup_word("<null>");
  }

  void set_output_vocabulary(const vocabulary &vocab)
  {
    *(this->output_vocab) = vocab;
  }

  const vocabulary &get_input_vocabulary() const { return *(this->input_vocab); }
  const vocabulary &get_output_vocabulary() const { return *(this->output_vocab); }

  int lookup_word(const std::string &word, vocabulary const& vocab) const
  {
    if (map_digits)
      for (int i=0, n=word.size(); i<n; ++i)
        if (graehl::ascii_digit(word[i])) {
          std::string mapped_word(word);
          replace(mapped_word, i);
          return vocab->lookup_word(mapped_word);
        }
    return vocab->lookup_word(word);
  }

  int lookup_word(std::pair<char const*, char const*> slice, vocabulary const& vocab) const
  {
    if (map_digits)
      for (char const* i = slice.first; i != slice.second; ++i)
        if (graehl::ascii_digit(*i)) {
          std::string mapped_word(slice.first, slice.second);
          replace(mapped_word, i - slice.first);
          return vocab->lookup_word(mapped_word);
        }
    return vocab->lookup_word(slice);
  }

  int lookup_input_word(const std::string &word) const
  {
    return lookup_word(word, *input_vocab);
  }

  int lookup_output_word(const std::string &word) const
  {
    return lookup_word(word, *output_vocab);
  }

  int lookup_input_word(std::pair<char const*, char const*> word) const
  {
    return lookup_word(word, *input_vocab);
  }

  int lookup_output_word(std::pair<char const*, char const*> word) const
  {
    return lookup_word(word, *output_vocab);
  }

  double lookup_ngram(const int *ngram_a, int n)
  {
    Eigen::Matrix<int,Eigen::Dynamic,1> ngram(m->ngram_size);
    for (int i=0; i<m->ngram_size; i++)
    {
      if (i-m->ngram_size+n < 0)
      {
        if (ngram_a[0] == start)
          ngram(i) = start;
        else
          ngram(i) = null;
      }
      else
      {
        ngram(i) = ngram_a[i-m->ngram_size+n];
      }
    }
    return neuralNetwork::lookup_ngram(ngram);
  }

  double lookup_ngram(const std::vector<int> &ngram_v)
  {
    return lookup_ngram(ngram_v.data(), ngram_v.size());
  }

  template <typename Derived>
  double lookup_ngram(const Eigen::MatrixBase<Derived> &ngram)
  {
    return neuralNetwork::lookup_ngram(ngram);
  }

  template <typename DerivedA, typename DerivedB>
  void lookup_ngram(const Eigen::MatrixBase<DerivedA> &ngram, const Eigen::MatrixBase<DerivedB> &log_probs_const)
  {
    return neuralNetwork::lookup_ngram(ngram, log_probs_const);
  }

  void read(const std::string &filename)
  {
    std::vector<std::string> input_words;
    std::vector<std::string> output_words;
    m->read(filename, input_words, output_words);
    set_input_vocabulary(vocabulary(input_words));
    set_output_vocabulary(vocabulary(output_words));
    resize();
    // this is faster but takes more memory
    //m->premultiply();
  }

};

} // namespace nplm

#endif