Example #1
0
 def get_translation_settings(self):
     """
     Returns a `TranslationSettings` object based on the parsed console
     arguments.
     """
     args = self.parse_args()
     return TranslationSettings(args)
 def test_ende(self):
     with open('en-de/in', 'r', encoding='utf-8') as in_file, \
          open('en-de/out', 'w', encoding='utf-8') as out_file:
         os.chdir('models/en-de/')
         settings = TranslationSettings()
         settings.input = in_file
         settings.output = out_file
         settings.models = ["model.npz"]
         settings.beam_size = 12
         settings.normalization_alpha = 1.0
         settings.translation_strategy = 'sampling'
         settings.sampling_temperature = 0.4
         translate(settings=settings)
         os.chdir('../..')
     self.outputEqual('en-de/ref2','en-de/out')
Example #3
0
    def translate(self, segment):
        """
        Processes a translation request.
        """

        translations = self._translator.translate_string(
            segment, TranslationSettings())

        result = " ".join(translations[0].target_words)
        return result
Example #4
0
def get_settings(model, beam):
    """
    Initialize and customize settings.
    """
    translation_settings = TranslationSettings()
    translation_settings.models = [model]
    translation_settings.num_processes = 1
    translation_settings.beam_width = beam
    translation_settings.normalization_alpha = 1.0
    translation_settings.suppress_unk = True
    translation_settings.get_word_probs = False
    return translation_settings
Example #5
0
def get_settings():
    """
    Initialize and customize settings.
    """
    translation_settings = TranslationSettings()
    translation_settings.models = ["models/model.npz"]
    translation_settings.num_processes = 1
    translation_settings.beam_width = 12
    translation_settings.normalization_alpha = 1.0
    translation_settings.suppress_unk = True
    translation_settings.get_word_probs = True
    return translation_settings
Example #6
0
    def __init__(self, request):
        """
        Initialises a translation request.

        @type raw_body: str
        @param raw_body: the POST request submitted to Nematus server.
        """
        self._request = request
        self.segments = []
        self.settings = TranslationSettings()  # default values
        self._parse()
Example #7
0
 def get_settings(self):
     """
     Initialize and customize settings.
     """
     translation_settings = TranslationSettings()
     translation_settings.input = open('../../en-de/in')
     translation_settings.output = open('../../en-de/out', 'w')
     translation_settings.models = ["model.npz"]
     translation_settings.beam_size = 12
     translation_settings.normalization_alpha = 1.0
     return translation_settings
Example #8
0
 def test_ende(self):
     with open('en-de/in', 'r', encoding='utf-8') as in_file, \
          open('en-de/out', 'w', encoding='utf-8') as out_file:
         os.chdir('models/en-de/')
         settings = TranslationSettings()
         settings.input = in_file
         settings.output = out_file
         settings.models = ["model.npz"]
         settings.beam_size = 12
         settings.normalization_alpha = 1.0
         translate(settings=settings)
         os.chdir('../..')
     self.outputEqual('en-de/ref2','en-de/out')
        configs.append(argparse.Namespace(**config))

    # Create the model graphs and restore their variables.
    logging.debug("Loading models")
    models = []
    for i, config in enumerate(configs):
        with tf.variable_scope("model%d" % i) as scope:
            model = rnn_model.RNNModel(config)
            saver = model_loader.init_or_restore_variables(
                config, session, ensemble_scope=scope)
            models.append(model)

    logging.debug("Models load done.")
    # Translate the source file.
    inference.translate_file(input_file=settings.input,
                             output_file=settings.output,
                             session=session,
                             models=models,
                             configs=configs,
                             beam_size=settings.beam_size,
                             nbest=settings.n_best,
                             minibatch_size=settings.minibatch_size,
                             maxibatch_size=settings.maxibatch_size,
                             normalization_alpha=settings.normalization_alpha)


if __name__ == "__main__":
    # Parse console arguments.
    settings = TranslationSettings(from_console_arguments=True)
    main(settings)
Example #10
0
def outputEqual(output1, output2):
    """given two translation outputs, check that output string is identical
    """
    for i, (line, line2) in enumerate(
            zip(open(output1).readlines(),
                open(output2).readlines())):
        #assertEqual(line.strip(), line2.strip())
        print "translate {}".format(i)
        print line
        print line2


# English-German WMT16 system, no dropout
"""
Initialize and customize settings.
"""
translation_settings = TranslationSettings()
translation_settings.models = ["model_test/model.npz-80000"]
#translation_settings.num_processes = 8
translation_settings.beam_width = 10
translation_settings.normalization_alpha = 1.0
translation_settings.verbose = True
translation_settings.n_best = True
translation_settings.suppress_unk = True

translate(input_file=open('data/translated/fr.00'),
          output_file=open('data/fr.00.8.out', 'w'),
          translation_settings=translation_settings)
print "everyting ok"
#outputEqual('en-de/ref2','en-de/out')