View Javadoc
1   /*
2    * ===========================================================================
3    * Copyright (C) 2011-2025 by the Okapi Framework contributors
4    * -----------------------------------------------------------------------------
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   * ===========================================================================
17   */
18  
19  package net.sf.okapi.connectors.google.v3;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import net.sf.okapi.common.query.MatchType;
28  import net.sf.okapi.common.query.QueryResult;
29  import net.sf.okapi.common.resource.InvalidContentException;
30  import net.sf.okapi.common.resource.TextFragment;
31  import net.sf.okapi.lib.translation.QueryUtil;
32  
33  /**
34   * Builder for creating QueryResult objects from TextFragment translations.
35   */
36  public class FragmentQueryResultBuilder implements QueryResultBuilder<TextFragment> {
37  
38    private final Logger logger = LoggerFactory.getLogger(getClass());
39    private final String origin;
40    private final int weight;
41    private final QueryUtil util;
42  
43    public FragmentQueryResultBuilder(String origin, int weight) {
44      this.origin = origin;
45      this.weight = weight;
46      this.util = new QueryUtil();
47    }
48  
49    @Override
50    public List<QueryResult> convertResponses(List<TranslationResponse> responses,
51        TextFragment originalSource) {
52  
53      List<QueryResult> results = new ArrayList<>();
54  
55      for (TranslationResponse response : responses) {
56        QueryResult qr = new QueryResult();
57        qr.source = originalSource;
58  
59        try {
60          // Convert the translated HTML back to a TextFragment
61          String translatedHtml = response.getTranslatedText();
62          qr.target = makeFragment(translatedHtml, originalSource);
63  
64          qr.setFuzzyScore(100); // MT is always 100% match
65          qr.setCombinedScore(100);
66          qr.matchType = MatchType.MT;
67          qr.origin = origin;
68          qr.weight = weight;
69          qr.setQuality(QueryResult.QUALITY_UNDEFINED);
70  
71        } catch (InvalidContentException e) {
72          // Something went wrong in the resulting MT candidate
73          // We fall back on no candidate with a zero score
74          logger.error("This MT candidate will be ignored.\n{}\n{}", originalSource.toString(),
75              e.getMessage());
76  
77          qr = createDummyResponse(originalSource);
78        }
79  
80        results.add(qr);
81      }
82  
83      return results;
84    }
85  
86    @Override
87    public QueryResult createDummyResponse(TextFragment originalSource) {
88      QueryResult qr = new QueryResult();
89  
90      qr.source = originalSource;
91      qr.target = originalSource.clone(); // Return source as target
92      qr.setFuzzyScore(0); // No match
93      qr.setCombinedScore(0);
94      qr.matchType = MatchType.MT;
95      qr.origin = origin;
96      qr.weight = weight;
97      qr.setQuality(QueryResult.QUALITY_UNDEFINED);
98  
99      return qr;
100   }
101 
102   private TextFragment makeFragment(String codedHtml, TextFragment sourceFragment) {
103     return sourceFragment.hasCode()
104         ? new TextFragment(util.fromCodedHTML(codedHtml, sourceFragment, false),
105             sourceFragment.getClonedCodes())
106 
107         : new TextFragment(util.fromCodedHTML(codedHtml, sourceFragment, false));
108   }
109 }