Exemplo n.º 1
0
 def do_config(self, cmd_args):
     """Change SGNMT configuration. Syntax: 'config <key> <value>.
     For most configuration changes the decoder needs to be
     rebuilt.
     """
     global outputs, decoder, args
     split_args = cmd_args.split()
     if len(split_args) < 2:
         print("Syntax: 'config <key> <new-value>'")
     else:
         key, val = (split_args[0], ' '.join(split_args[1:]))
         try:
             val = int(val)
         except:
             try:
                 val = float(val)
             except:
                 if val == "true":
                     val = True
                 elif val == "false":
                     val = False
         setattr(args, key, val)
         print("Setting %s=%s..." % (key, val))
         outputs = decode_utils.create_output_handlers()
         if key in [
                 "wmap", "src_wmap", "trg_wmap", "preprocessing",
                 "postprocessing", "bpe_codes"
         ]:
             io.initialize(args)
         elif not key in ['outputs', 'output_path']:
             decoder = decode_utils.create_decoder()
Exemplo n.º 2
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.º 3
0
    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())
    else:
        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())
    print("Test sentences are read directly from stdin.")
Exemplo n.º 4
0
    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())
    print("Test sentences are read directly from stdin.")
    print("!sgnmt help lists all available directives")
    print("Quit with ctrl-c or !sgnmt quit")
    quit_sgnmt = False