Exemplo n.º 1
0
    def prepareSGNMT(self, args):
        """Initialise the SGNMT for agent training.
        The SGNMT returns hiddens states only when the function
        ``_get_hidden_states()'' is being called.
        It should stop after reading the first words, and returns the predictor
        """
        # UTF-8 support
        if sys.version_info < (3, 0):
            sys.stderr = codecs.getwriter('UTF-8')(sys.stderr)
            sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
            sys.stdin = codecs.getreader('UTF-8')(sys.stdin)

        utils.load_src_wmap(args.src_wmap)
        utils.load_trg_wmap(args.trg_wmap)
        utils.load_trg_cmap(args.trg_cmap)
        decode_utils.base_init(args)
        self.decoder = decode_utils.create_decoder()
        self.predictor = self.decoder.predictors[0][
            0]  # only sim_t2t predictor
        self.outputs = decode_utils.create_output_handlers()
        self._load_all_initial_hypos(args)
Exemplo n.º 2
0
http://ucam-smt.github.io/sgnmt/html/tutorial.html
"""

import logging
import os
import sys
from cmd import Cmd
import time

from cam.sgnmt import utils, io
from cam.sgnmt import decode_utils
from cam.sgnmt.ui import get_args, get_parser, run_diagnostics

# Load configuration from command line arguments or configuration file
args = get_args()
decode_utils.base_init(args)


class SGNMTPrompt(Cmd):
    def default(self, cmd_args):
        """Translate a single sentence."""
        decode_utils.do_decode(decoder, outputs, [cmd_args.strip()])

    def emptyline(self):
        pass

    def do_translate(self, cmd_args):
        """Translate a single sentence."""
        decode_utils.do_decode(decoder, outputs, [cmd_args.strip()])

    def do_diagnostics(self, cmd_args):
import logging
import sys
import traceback
import time
import numpy as np
import pickle

from cam.sgnmt.decoding import core
from cam.sgnmt import decode_utils
from cam.sgnmt import utils
from cam.sgnmt.ui import get_args
from cam.sgnmt.predictors.core import UnboundedVocabularyPredictor

# Load configuration from command line arguments or configuration file
args = get_args()
decode_utils.base_init(args)

class ForcedDecoder(core.Decoder):
    """Forced decoder implementation. The decode() function returns
    the same hypos as the GreedyDecoder with forced predictor. However,
    this implementation keeps track of all posteriors along the way,
    which are dumped to the file system afterwards.
    """
    
    def __init__(self, decoder_args):
        """Initialize the decoder and load target sentences."""
        super(ForcedDecoder, self).__init__(decoder_args)
        self.trg_sentences = load_sentences(decoder_args.trg_test, "target")

    def decode(self, src_sentence):
        self.initialize_predictors(src_sentence)