rxn.onmt_utils.translator.Translator

class rxn.onmt_utils.translator.Translator(opt)[source]

Bases: object

Wraps the OpenNMT translation functionality into a class.

Parameters

opt (Namespace) –

__init__(opt)[source]

Should not be called directly as implementation may change; call the classmethods from_model_path or from_opt instead.

Parameters

opt (Namespace) – model options.

Methods

__init__(opt)

Should not be called directly as implementation may change; call the classmethods from_model_path or from_opt instead.

from_model_path(model_path, **kwargs)

Create a Translator instance from the model path(s).

from_opt(opt)

Create a Translator instance from the opt arguments.

translate_multiple_with_scores(sentences[, ...])

Translate multiple sentences.

translate_sentences(sentences)

Translate multiple sentences.

translate_single(sentence)

Translate one single sentence.

classmethod from_model_path(model_path, **kwargs)[source]

Create a Translator instance from the model path(s).

Parameters
  • model_path (Union[str, Iterable[str]]) – path to the translation model file(s). If multiple are given, will be an ensemble model.

  • kwargs (Any) – Additional values to be parsed for instantiating the translator, such as n_best, beam_size, max_length, etc.

Return type

Translator

classmethod from_opt(opt)[source]

Create a Translator instance from the opt arguments.

Parameters

opt (Namespace) – model options.

Return type

Translator

translate_multiple_with_scores(sentences, n_best=None)[source]

Translate multiple sentences.

Parameters
  • sentences (Iterable[str]) – Sentences to translate.

  • n_best (Optional[int], default: None) – if provided, will overwrite the number of predictions to make.

Return type

Iterator[List[TranslationResult]]

translate_sentences(sentences)[source]

Translate multiple sentences.

Parameters

sentences (Iterable[str]) –

Return type

List[str]

translate_single(sentence)[source]

Translate one single sentence.

Parameters

sentence (str) –

Return type

str