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
        "!sgnmt config <name> <value>  Update the configuration. Some changes")
    print("                             may require loading the decoder from ")
    print(
        "                             scratch, some (like changing predictor")
    print("                             weights) can be done on the fly. For ")
    print("                             printing help text for all available")
    print("                             parameters use")
    print("                               !sgnmt config (without arguments)")
    print("!sgnmt decode <file_name>     Decode sentences in the given file")
    print("!sgnmt reset                  Reset predictors, e.g. set sentence")
    print("                             counter to 1 for fst predictor.")
    print("!sgnmt quit                   Quit SGNMT")
    print("!sgnmt help                   Print this help")


utils.load_src_wmap(args.src_wmap)
utils.load_trg_wmap(args.trg_wmap)
utils.load_trg_cmap(args.trg_cmap)
decoder = decode_utils.create_decoder(args)
outputs = decode_utils.create_output_handlers()

if args.input_method == 'file':
    # Check for additional input files
    if getattr(args, "src_test2"):
        decode_utils.do_decode(decoder, outputs,
                               _process_inputs(args.sync_symbol))
    else:
        decode_utils.do_decode(decoder, outputs,
                               _process_input(args.sync_symbol))
#        with codecs.open(args.src_test, encoding='utf-8') as f:
#            decode_utils.do_decode(decoder,
Exemplo n.º 3
0
def _print_shell_help():
    """Print help text for shell usage in interactive mode."""
    print("Available SGNMT directives:")
    print("!sgnmt config <name> <value>  Update the configuration. Some changes")
    print("                             may require loading the decoder from ")
    print("                             scratch, some (like changing predictor")
    print("                             weights) can be done on the fly. For ")
    print("                             printing help text for all available")
    print("                             parameters use")
    print("                               !sgnmt config (without arguments)")
    print("!sgnmt decode <file_name>     Decode sentences in the given file")
    print("!sgnmt quit                   Quit SGNMT")
    print("!sgnmt help                   Print this help")


utils.load_src_wmap(args.src_wmap)
utils.load_trg_wmap(args.trg_wmap)
utils.load_trg_cmap(args.trg_cmap)
decoder = decode_utils.create_decoder()
outputs = decode_utils.create_output_handlers()

if args.input_method == 'file':
    with codecs.open(args.src_test, encoding='utf-8') as f:
        decode_utils.do_decode(decoder,
                               outputs,
                               [line.strip().split() for line in f])
elif args.input_method == 'dummy':
    decode_utils.do_decode(decoder, outputs, False)
else: # Interactive mode: shell or stdin
    print("Start interactive mode.")
    print("PID: %d" % os.getpid())