Fix alignment check is now performed at sentence-piece level
@@ -312,48 +312,18 @@ public class TranslationFacade {
if (decoder.supportsSentenceSplit()) {
Sentence[] sentencePieces = SentenceSplitter.forLanguage(direction.source).split(sentence);
- Translation[] translationPieces = translate(sentencePieces, decoder, engine);
+ Translation[] translationPieces = translate(sentencePieces, decoder);
translation = this.merge(sentence, sentencePieces, translationPieces);
} else {
translation = translate(sentence, decoder);
}
- // Alignment
- if (!translation.hasAlignment()) {
- Aligner aligner = engine.getAligner();
-
- Alignment alignment = aligner.getAlignment(direction, sentence, translation);
- translation.setWordAlignment(alignment);
-
- if (translation.hasNbest()) {
- for (Translation nbest : translation.getNbest()) {
- Alignment nbestAlignment = aligner.getAlignment(direction, sentence, nbest);
- nbest.setWordAlignment(nbestAlignment);
- }
- }
- }
-
postprocessor.process(direction, translation);
// NBest list
if (translation.hasNbest()) {
List<Translation> hypotheses = translation.getNbest();
-
- if (!hypotheses.get(0).hasAlignment()) {
- ArrayList<Sentence> sources = new ArrayList<>(hypotheses.size());
- for (int i = 0; i < hypotheses.size(); i++)
- sources.add(sentence);
-
- Aligner aligner = engine.getAligner();
- Alignment[] alignments = aligner.getAlignments(direction, sources, hypotheses);
-
- int i = 0;
- for (Translation hypothesis : hypotheses) {
- hypothesis.setWordAlignment(alignments[i]);
- i++;
- }
- }
postprocessor.process(direction, hypotheses);
}
@@ -369,32 +339,17 @@ public class TranslationFacade {
}
}
- private Translation[] translate(Sentence[] sentences, Decoder decoder, Engine engine)
+ private Translation[] translate(Sentence[] sentences, Decoder decoder)
throws DecoderException, AlignerException {
Translation[] translations = new Translation[sentences.length];
- for (int i = 0; i < sentences.length; i++) {
- Translation translation = this.translate(sentences[i], decoder);
- // Alignment
- if (!translation.hasAlignment()) {
- Aligner aligner = engine.getAligner();
-
- Alignment alignment = aligner.getAlignment(direction, sentences[i], translation);
- translation.setWordAlignment(alignment);
+ for (int i = 0; i < sentences.length; i++)
+ translations[i] = this.translate(sentences[i], decoder);
- if (translation.hasNbest()) {
- for (Translation nbest : translation.getNbest()) {
- Alignment nbestAlignment = aligner.getAlignment(direction, sentences[i], nbest);
- nbest.setWordAlignment(nbestAlignment);
- }
- }
- }
- translations[i] = translation;
- }
return translations;
}
- private Translation translate(Sentence sentence, Decoder decoder) throws DecoderException {
+ private Translation translate(Sentence sentence, Decoder decoder) throws DecoderException, AlignerException {
Translation translation;
if (nbest > 0) {
@@ -404,6 +359,25 @@ public class TranslationFacade {
translation = decoder.translate(direction, variant, sentence, context);
}
+ // Compute alignments if missing
+
+ if (!translation.hasAlignment()) {
+ Engine engine = ModernMT.getNode().getEngine();
+ Aligner aligner = engine.getAligner();
+
+ Alignment alignment = aligner.getAlignment(direction, sentence, translation);
+ translation.setWordAlignment(alignment);
+
+ if (translation.hasNbest()) {
+ for (Translation nbest : translation.getNbest()) {
+ if (!nbest.hasAlignment()) {
+ Alignment nbestAlignment = aligner.getAlignment(direction, sentence, nbest);
+ nbest.setWordAlignment(nbestAlignment);
+ }
+ }
+ }
+ }
+
return translation;
}
