1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
61 String translatedHtml = response.getTranslatedText();
62 qr.target = makeFragment(translatedHtml, originalSource);
63
64 qr.setFuzzyScore(100);
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
73
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();
92 qr.setFuzzyScore(0);
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 }