summaryrefslogtreecommitdiff
path: root/python/src/_cdec.pyx
blob: 698a66f67e4feb49b013aadb2ece5c892107070a (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
from libcpp.string cimport string
from libcpp.vector cimport vector
from utils cimport *
cimport decoder

include "vectors.pxi"
include "hypergraph.pxi"
include "lattice.pxi"

SetSilent(True)

class ParseFailed(Exception):
    pass

cdef class Decoder:
    cdef decoder.Decoder* dec
    cdef public DenseVector weights

    def __cinit__(self, char* config):
        decoder.register_feature_functions()
        cdef istringstream* config_stream = new istringstream(config)
        self.dec = new decoder.Decoder(config_stream)
        del config_stream
        self.weights = DenseVector()
        self.weights.vector = &self.dec.CurrentWeightVector()

    def __dealloc__(self):
        del self.dec

    def read_weights(self, cfg):
        with open(cfg) as fp:
            for line in fp:
                fname, value = line.split()
                self.weights[fname.strip()] = float(value)

    def translate(self, sentence, grammar=None):
        if isinstance(sentence, unicode):
            inp = sentence.strip().encode('utf8')
        elif isinstance(sentence, str):
            inp = sentence.strip()
        elif isinstance(sentence, Lattice):
            inp = str(sentence) # PLF format
        else:
            raise TypeError('Cannot translate input type %s' % type(sentence))
        if grammar:
            self.dec.SetSentenceGrammarFromString(string(<char *> grammar))
        cdef decoder.BasicObserver observer = decoder.BasicObserver()
        self.dec.Decode(string(<char *>inp), &observer)
        if observer.hypergraph == NULL:
            raise ParseFailed()
        cdef Hypergraph hg = Hypergraph()
        hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0])
        return hg