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

neuralLM.h « src - github.com/moses-smt/nplm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9de37cbb2f6d4ea95c5a5987995a7863736f8f17 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef NEURALLM_H
#define NEURALLM_H

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

#include <../3rdparty/Eigen/Dense>

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

/*
  To do:
  - move digit mapping into vocabulary.h
 */

namespace nplm
{

class neuralLM : public neuralNetwork
{
    char map_digits;
    boost::shared_ptr<vocabulary> vocab;
    int start, null;

public:
    neuralLM() 
      : neuralNetwork(),
        vocab(new vocabulary()),
	map_digits(0)
    { 
    }

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

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

    const vocabulary &get_vocabulary() const { return *(this->vocab); }

    int lookup_word(const std::string &word) const
    {
        if (map_digits)
	    for (int i=0; i<word.length(); i++)
	        if (isdigit(word[i]))
		{
		    std::string mapped_word(word);
		    for (; i<word.length(); i++)
		        if (isdigit(word[i]))
			    mapped_word[i] = map_digits;
		    return vocab->lookup_word(mapped_word);
		}
        return vocab->lookup_word(word);
    }

    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> words;
        m->read(filename, words);
        set_vocabulary(vocabulary(words));
        resize();
	// this is faster but takes more memory
        //m->premultiply();
    }

};

template <typename T>
void addStartStop(std::vector<T> &input, std::vector<T> &output, int ngram_size, const T &start, const T &stop)
{
    output.clear();
    output.resize(input.size()+ngram_size);
    for (int i=0; i<ngram_size-1; i++)
        output[i] = start;
    std::copy(input.begin(), input.end(), output.begin()+ngram_size-1);
    output[output.size()-1] = stop;
}

template <typename T>
void makeNgrams(const std::vector<T> &input, std::vector<std::vector<T> > &output, int ngram_size)
{
  output.clear();
  for (int j=ngram_size-1; j<input.size(); j++)
  {
      std::vector<T> ngram(input.begin() + (j-ngram_size+1), input.begin() + j+1);
      output.push_back(ngram);
  }
}

inline void preprocessWords(const std::vector<std::string> &words, 
    std::vector< std::vector<int> > &ngrams,
	  int ngram_size, 
    const vocabulary &vocab, 
	  bool numberize,
    bool add_start_stop,
    bool ngramize) {
  int start = vocab.lookup_word("<s>");
  int stop = vocab.lookup_word("</s>");
  
  // convert words to ints
  std::vector<int> nums;
  if (numberize) {
    for (int j=0; j<words.size(); j++) {
      nums.push_back(vocab.lookup_word(words[j]));
    }
  }
  else {
    for (int j=0; j<words.size(); j++) {
      nums.push_back(boost::lexical_cast<int>(words[j]));
    }            
  }
  
  // convert sequence to n-grams
  ngrams.clear();
  if (ngramize) {
    std::vector<int> snums;
    if (add_start_stop) {
      addStartStop<int>(nums, snums, ngram_size, start, stop);
    } else {
      snums = nums;
    }
    makeNgrams(snums, ngrams, ngram_size);
  }
  else {
    if (nums.size() != ngram_size)
      {
	std::cerr << "error: wrong number of fields in line" << std::endl;
	std::exit(1);
      }
    ngrams.push_back(nums);
  }
}

} // namespace nplm

#endif