View Javadoc
1   package com.acumenvelocity.ath.steps;
2   
3   import com.acumenvelocity.ath.common.OkapiUtil;
4   import com.acumenvelocity.ath.common.OriginalTuAnnotation;
5   
6   import net.sf.okapi.common.Event;
7   import net.sf.okapi.common.resource.ITextUnit;
8   import net.sf.okapi.common.resource.Segment;
9   import net.sf.okapi.common.resource.TextContainer;
10  import net.sf.okapi.steps.leveraging.LeveragingStep;
11  
12  /**
13   * Skips machine translation for text units where all target segments already have non-empty
14   * content.
15   */
16  public class MtLeveragingStep extends LeveragingStep {
17  
18    private boolean mtSendPlainText;
19  
20    public MtLeveragingStep(boolean mtSendPlainText) {
21      super();
22      this.mtSendPlainText = mtSendPlainText;
23    }
24  
25    @Override
26    protected Event handleTextUnit(Event event) {
27      // Get the text unit from the event
28      ITextUnit textUnit = event.getTextUnit();
29  
30      if (textUnit == null) {
31        return event; // Return unchanged if no text unit
32      }
33  
34      if (mtSendPlainText) {
35        // Store the original TU with codes, remove codes to improve MT quality
36        // CodesReinsertionStep will use OriginalTuAnnotation to get the original source codes
37        textUnit.setAnnotation(new OriginalTuAnnotation(textUnit.clone(), getSourceLocale()));
38        OkapiUtil.removeCodes(textUnit, true);
39      }
40  
41      // Get the target container for the specified locale
42      TextContainer target = textUnit.getTarget(getTargetLocale());
43  
44      if (target == null || target.getSegments() == null) {
45        return super.handleTextUnit(event); // Proceed with MT if no target or segments
46      }
47  
48      // Get source container to align segments
49      TextContainer source = textUnit.getSource();
50  
51      if (source == null || source.getSegments() == null) {
52        return super.handleTextUnit(event); // Proceed with MT if no source or segments
53      }
54  
55      // Check if all target segments have non-empty content
56      boolean allSegmentsTranslated = true;
57  
58      for (Segment sourceSegment : source.getSegments()) {
59        if (sourceSegment == null) {
60          continue; // Skip null source segments
61        }
62  
63        Segment targetSegment = target.getSegments().get(sourceSegment.getId());
64  
65        if (targetSegment == null || targetSegment.getContent() == null ||
66            targetSegment.getContent().isEmpty()) {
67  
68          allSegmentsTranslated = false;
69          break; // Exit loop if any segment is untranslated
70        }
71      }
72  
73      // If all segments are translated, return the event unchanged to skip MT
74      if (allSegmentsTranslated) {
75        return event;
76      }
77  
78      // Otherwise, proceed with machine translation in the superclass
79      return super.handleTextUnit(event);
80    }
81  }