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: 4ad6752a4c9779261b3d2ce3eaf5fbfb9f1d276e (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
#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"

namespace nplm
{

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

public:
    neuralTM() 
      : neuralNetwork(),
        map_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_input_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 input_vocab->lookup_word(mapped_word);
		}
        return input_vocab->lookup_word(word);
    }

    int lookup_output_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 output_vocab->lookup_word(mapped_word);
		}
	return output_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> 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