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 net.sf.okapi.common.query.MatchType;
25  import net.sf.okapi.common.query.QueryResult;
26  import net.sf.okapi.common.resource.TextFragment;
27  
28  /**
29   * Builder for creating QueryResult objects from plain text translations.
30   */
31  public class TextQueryResultBuilder implements QueryResultBuilder<String> {
32  
33    private final String origin;
34    private final int weight;
35  
36    public TextQueryResultBuilder(String origin, int weight) {
37      this.origin = origin;
38      this.weight = weight;
39    }
40  
41    @Override
42    public List<QueryResult> convertResponses(List<TranslationResponse> responses,
43        String originalSource) {
44      List<QueryResult> results = new ArrayList<>();
45  
46      for (TranslationResponse response : responses) {
47        QueryResult qr = new QueryResult();
48        
49        qr.source = new TextFragment(originalSource);
50        qr.target = new TextFragment(response.getTranslatedText());
51        qr.setFuzzyScore(100); // MT is always 100% match
52        qr.setCombinedScore(100);
53        qr.matchType = MatchType.MT;
54        qr.origin = origin;
55        qr.weight = weight;
56        qr.setQuality(QueryResult.QUALITY_UNDEFINED);
57  
58        results.add(qr);
59      }
60  
61      return results;
62    }
63  
64    @Override
65    public QueryResult createDummyResponse(String originalSource) {
66      QueryResult qr = new QueryResult();
67      
68      qr.source = new TextFragment(originalSource);
69      qr.target = new TextFragment(originalSource); // Return source as target
70      qr.setFuzzyScore(0); // No match
71      qr.setCombinedScore(0);
72      qr.matchType = MatchType.MT;
73      qr.origin = origin;
74      qr.weight = weight;
75      qr.setQuality(QueryResult.QUALITY_UNDEFINED);
76      
77      return qr;
78    }
79  }