This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
TransMemoryMatcher.java
563 lines (498 loc) · 23.6 KB
/
TransMemoryMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package org.zanata.search;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.parser.Tag;
import org.zanata.model.HLocale;
import org.zanata.model.HTextFlow;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Slf4j
public class TransMemoryMatcher {
private static final Document.OutputSettings OUTPUT_SETTINGS =
new Document.OutputSettings()
.charset(Charsets.UTF_8).indentAmount(0)
.prettyPrint(false);
private boolean canFullyReuseTM = false;
private final Element transMemorySourceRootElement;
private final Document upcomingSourceDoc;
private final String upcomingSourceTextOnly;
public TransMemoryMatcher(HTextFlow upcomingSource,
HTextFlow transMemory, HLocale targetLocale) {
// TODO pahuang work on plural
String upcomingSourceContent = upcomingSource.getContents().get(0);
String tmSource = transMemory.getContents().get(0);
String tmTarget = transMemory.getTargets().get(targetLocale.getId()).getContents().get(
0);
logContext(upcomingSource, transMemory, targetLocale);
// the HTML parse will use can not handle <p> <br> very well.
// see http://webdesign.about.com/od/htmltags/a/aabg092299a.htm
// jsoup will append a </p> to the end if it sees a standalone <p>
// jsoup will ignore <br>
upcomingSourceDoc = Jsoup.parseBodyFragment(upcomingSourceContent);
upcomingSourceTextOnly = upcomingSourceDoc.body().text();
transMemorySourceRootElement = Jsoup.parseBodyFragment(tmSource).body();
Element transMemoryTargetRootElement =
Jsoup.parseBodyFragment(tmTarget).body();
TransMemoryHTMLParser transMemoryParser =
new TransMemoryHTMLParser(transMemorySourceRootElement,
transMemoryTargetRootElement);
if (!transMemoryParser.perfectMatch && !transMemoryParser.heuristicMatch) {
log.info(
"can not analyze TM - don't know how to match some translation tokens: {}",
transMemoryParser.leftOverTransTokens);
canFullyReuseTM = false;
return;
}
List<TextTokenKey> transMemoryTextTokens = transMemoryParser.textTokens;
log.debug("=== about to parse upcoming source ===");
List<Node> upcomingNodes = upcomingSourceDoc.body()
.childNodes();
LinkedList<TextTokenKey> upcomingSourceTokens = Lists.newLinkedList();
depthFirstTraverseSourceNodes(upcomingNodes, new ParentNodes(),
upcomingSourceTokens);
Iterator<TextTokenKey> upcomingSourceTokensIt = upcomingSourceTokens
.iterator();
Iterator<TextTokenKey>
transMemoryTokensIt = transMemoryTextTokens.iterator();
TextTokenKey nextSourceToken = upcomingSourceTokensIt.next();
TextTokenKey nextTransMemoryToken = transMemoryTokensIt.next();
while (nextSourceToken != null) {
if (upcomingSourceTokenMatchesTMSourceToken(
nextSourceToken,
nextTransMemoryToken)) {
log.debug("found source token for [{}] in TM [{}]", nextSourceToken, nextTransMemoryToken);
// we can't update text on the node here because it will affect matchText(nextSourceToken.optElementBefore, nextTransMemoryToken.optElementBefore) above
nextSourceToken.targetText = nextTransMemoryToken.targetText;
nextTransMemoryToken = nextOrNull(transMemoryTokensIt);
} else {
log.debug("can not find matching translation for [{}] in TM", nextSourceToken);
nextSourceToken.targetText = "!UnKnoWn!";
}
nextSourceToken = nextOrNull(upcomingSourceTokensIt);
}
canFullyReuseTM = !upcomingSourceTokensIt.hasNext() &&
!transMemoryTokensIt.hasNext();
// apply matched tokens
if (canFullyReuseTM) {
StringBuilder generatedTargetTextOnly = new StringBuilder();
for (TextTokenKey upcomingSourceToken : upcomingSourceTokens) {
upcomingSourceToken.sourceNode.text(upcomingSourceToken.targetText);
generatedTargetTextOnly.append(upcomingSourceToken.targetText);
}
String tmTargetTextOnly =
transMemoryParser.tmTargetTextOnly.toString();
String translationBuildFromTM = upcomingSourceDoc.outputSettings(OUTPUT_SETTINGS).body().html();
log.debug("Translation build from given TM is:{}", translationBuildFromTM);
// do a final comparison to see if the generated translation matches
// TM translation (in case some tags has changed locations,
// I don't have a way to fix it.
log.debug(
"comparing TM target text only to generated target text:\nTM :{}\nGEN:{}",
tmTargetTextOnly, generatedTargetTextOnly);
canFullyReuseTM = generatedTargetTextOnly.toString().equals(
tmTargetTextOnly);
}
}
/**
* We compare the token's before and after element text.
*
* @param upcomingSourceToken text token in upcoming source
* @param transMemoryToken text token in trans memory
* @return true if two tokens' before element and after element matches in text
*/
private static boolean upcomingSourceTokenMatchesTMSourceToken(
TextTokenKey upcomingSourceToken,
TextTokenKey transMemoryToken) {
return transMemoryToken != null
&& upcomingSourceToken != null
&& matchText(upcomingSourceToken.optElementBefore,
transMemoryToken.optElementBefore)
&& matchText(upcomingSourceToken.optElementAfter,
transMemoryToken.optElementAfter);
}
private static boolean matchText(Optional<Element> optElement,
Optional<Element> optOtherElement) {
return getOptionalElementText(optElement).equals(getOptionalElementText(optOtherElement));
}
private static Optional<String> getOptionalElementText(Optional<Element> optElement) {
return optElement.isPresent() ? Optional.of(optElement.get().text()) : Optional.<String>absent();
}
private static void logContext(HTextFlow upcomingSource, HTextFlow transMemory,
HLocale targetLocale) {
if (log.isDebugEnabled()) {
log.debug("about to match \nupcoming source: \n * {} to \nTM: \n * {} \n-> {}",
upcomingSource.getContents(), transMemory.getContents(),
transMemory.getTargets().get(targetLocale.getId())
.getContents());
}
}
private static void depthFirstTraverseSourceNodes(List<Node> sourceNodes,
ParentNodes parentNodes,
List<TextTokenKey> sourceTextTokens) {
for (Node node : sourceNodes) {
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
TextTokenKey textTokenKey =
new TextTokenKey(textNode, parentNodes,
getOptionalBeforeSiblingElement(textNode),
getOptionalAfterSiblingElement(textNode),
textNode.siblingIndex(),
textNode.getWholeText());
sourceTextTokens.add(textTokenKey);
} else {
Element element = (Element) node;
depthFirstTraverseSourceNodes(element.childNodes(), parentNodes.append(element),
sourceTextTokens);
}
}
}
private static Optional<Element> getOptionalAfterSiblingElement(
TextNode textNode) {
return Optional.fromNullable(
(Element) textNode.nextSibling());
}
private static Optional<Element> getOptionalBeforeSiblingElement(
TextNode textNode) {
return Optional.fromNullable(
(Element) textNode.previousSibling());
}
private static <T> T nextOrNull(Iterator<T> iterator) {
return (iterator.hasNext()) ? iterator.next() : null;
}
public double calculateSimilarityPercent() {
double similarity =
LevenshteinTokenUtil.getSimilarity(
upcomingSourceTextOnly,
transMemorySourceRootElement.text());
double similarityPercent = similarity * 100;
if (similarityPercent < 99.99) {
// TODO pahuang here we could still try to put in reasonable effort (i.e. try to match as much text token as possible)
return similarityPercent;
} else if (canFullyReuseTM) {
// only return 100 if we can fully reuse TM.
return 100;
} else {
// text only matches 100% but we can not fully reuse TM translation
return 99;
}
}
public String translationFromTransMemory() {
Preconditions.checkState(canFullyReuseTM, "do not know how to apply translation memory to this source! Similarity must be 100%.");
return upcomingSourceDoc.outputSettings(OUTPUT_SETTINGS).body().html();
}
private static class ParentNodes {
private final List<Element> parentElements;
private transient List<Tag> parentTags;
ParentNodes() {
this.parentElements = ImmutableList.of();
}
private ParentNodes(ParentNodes parentNodes, Element elementNode) {
this.parentElements =
ImmutableList.<Element> builder().addAll(parentNodes.parentElements)
.add(elementNode).build();
}
ParentNodes append(Element currentElementNode) {
return new ParentNodes(this, currentElementNode);
}
public int size() {
return parentElements.size();
}
/**
* Checking whether the parent elements are identical. When we try to
* compare between source tokens, we need to make sure they share
* exactly the same elements not just tags. When we do look up for
* matching translation tokens, we use tags.
*
* @param o
* other ParentNodes object
* @return true if parent nodes list are identical in identity
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParentNodes that = (ParentNodes) o;
return Objects.equals(parentElements, that.parentElements);
}
@Override
public int hashCode() {
ensureParentTags();
return Objects.hash(parentElements);
}
private void ensureParentTags() {
if (parentTags == null) {
parentTags = Lists
.transform(parentElements, new Function<Element, Tag>() {
@Nullable
@Override
public Tag apply(Element input) {
return input.tag();
}
});
}
}
List<Tag> parentTags() {
ensureParentTags();
return parentTags;
}
@Override
public String toString() {
return parentTags().toString();
}
}
@RequiredArgsConstructor
@EqualsAndHashCode
private static class TextTokenKey implements Comparable<TextTokenKey> {
private final TextNode sourceNode;
private final ParentNodes parentNodes;
private final Optional<Element> optElementBefore;
private final Optional<Element> optElementAfter;
private final int siblingIndex;
private final String sourceText;
private TranslationToken matchedTranslationToken;
private String targetText = "";
public int appearanceOrder;
@Override
public String toString() {
return "(" + parentNodes + ")#" + siblingIndex +":" + sourceText;
}
@Override
public int compareTo(@Nonnull TextTokenKey o) {
return appearanceOrder - o.appearanceOrder;
}
}
@RequiredArgsConstructor
private static class TranslationToken {
private final List<Tag> parentTags;
private final Optional<Element> optElementBefore;
private final Optional<Element> optElementAfter;
private final int siblingIndex;
private final String targetText;
@Override
public String toString() {
return "(" + parentTags + ")" + targetText;
}
}
private static class TransMemoryHTMLParser {
private boolean perfectMatch = true;
private boolean heuristicMatch = true;
private List<TextTokenKey> textTokens = Lists.newLinkedList();
private StringBuilder tmTargetTextOnly = new StringBuilder();
private transient Map<List<Tag>, List<TranslationToken>> parentTagsToTransTokens =
Maps.newHashMap();
private transient List<TextTokenKey> leftOverSourceTokens = Lists.newLinkedList();
private transient List<TranslationToken> leftOverTransTokens = Collections.emptyList();
private TransMemoryHTMLParser(
Element sourceRootElement, Element targetRootElement) {
parse(sourceRootElement, targetRootElement);
}
private void parse(Element sourceRootElement, Element targetElement) {
List<Node> targetChildNodes = targetElement.childNodes();
depthFirstTraverseTargetNodes(targetChildNodes, new ParentNodes());
depthFirstTraverseSourceNodes(sourceRootElement.childNodes(),
new ParentNodes(), textTokens);
matchSourceTokensToTargetTokens();
}
// TODO uses recursion. Potentially for deep HTML doc it may bloat up stack
private void depthFirstTraverseTargetNodes(List<Node> targetChildNodes,
ParentNodes parentNodes) {
for (Node node : targetChildNodes) {
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
tmTargetTextOnly.append(textNode.getWholeText());
TranslationToken translationToken = new TranslationToken(
parentNodes.parentTags(),
getOptionalBeforeSiblingElement(textNode),
getOptionalAfterSiblingElement(textNode),
textNode.siblingIndex(),
textNode.getWholeText());
addToParentTagsMap(parentNodes.parentTags(),
translationToken);
} else {
Element element = (Element) node;
depthFirstTraverseTargetNodes(element.childNodes(),
parentNodes.append(element));
}
}
}
private void addToParentTagsMap(
List<Tag> tags, TranslationToken translationToken) {
if (parentTagsToTransTokens.containsKey(tags)) {
parentTagsToTransTokens.get(tags).add(translationToken);
} else {
parentTagsToTransTokens.put(tags, Lists.newArrayList(translationToken));
}
}
private void matchSourceTokensToTargetTokens() {
for (TextTokenKey sourceToken : textTokens) {
List<TranslationToken> transTokensUnderThisParentTags =
MoreObjects.firstNonNull(parentTagsToTransTokens
.get(sourceToken.parentNodes.parentTags()),
Collections.<TranslationToken> emptyList());
log.debug("translation tokens under this parent tags [{}]: {}",
sourceToken.parentNodes,
transTokensUnderThisParentTags);
// we match translation token to source token by checking their
// before and after element tag matches and they have same
// parent tags
Optional<TranslationToken> optMatchTransToken =
Iterables.tryFind(transTokensUnderThisParentTags,
new ExactTMSourceToTargetMatchingPredicate(
sourceToken));
if (optMatchTransToken.isPresent()) {
TranslationToken translationToken =
optMatchTransToken.get();
log.debug("found matching translation for [{}] -> [{}]", sourceToken, translationToken);
sourceToken.matchedTranslationToken = translationToken;
sourceToken.targetText = translationToken.targetText;
// remove matched translation
transTokensUnderThisParentTags.remove(translationToken);
} else {
log.debug("can not find matching translation for [{}], assuming empty string for now.", sourceToken);
sourceToken.targetText = "";
leftOverSourceTokens.add(sourceToken);
}
}
leftOverTransTokens =
Lists.newLinkedList(Iterables.concat(parentTagsToTransTokens.values()));
if (leftOverTransTokens.size() > 0) {
perfectMatch = false;
log.info("target tokens left: {}", leftOverTransTokens);
int leftOverTransSize = leftOverTransTokens.size();
for (TranslationToken leftOverTransToken : leftOverTransTokens) {
// here we have to use some heuristic
Iterable<TextTokenKey> potentialMatchedTrans =
Iterables.filter(leftOverSourceTokens,
new HeuristicTMSourceToTargetMatchingPredicate(
leftOverTransToken));
if (Iterables.size(potentialMatchedTrans) > 0) {
TextTokenKey sourceToken =
potentialMatchedTrans.iterator().next();
log.info("<Heuristic match>: assuming source token [{}] -> target token [{}]", sourceToken,
leftOverTransToken);
sourceToken.matchedTranslationToken = leftOverTransToken;
sourceToken.targetText = leftOverTransToken.targetText;
leftOverSourceTokens.remove(sourceToken);
leftOverTransSize--;
}
}
// after apply heuristic, if there is no left over translation token
heuristicMatch = leftOverTransSize == 0;
}
}
/**
* This will match text tokens by its parent tags, before element and
* after element tag.
*/
private static class ExactTMSourceToTargetMatchingPredicate
implements Predicate<TranslationToken> {
private final TextTokenKey sourceToken;
public ExactTMSourceToTargetMatchingPredicate(
TextTokenKey sourceToken) {
this.sourceToken = sourceToken;
}
@Override
public boolean apply(@Nullable TranslationToken transToken) {
return transToken != null
&& sourceToken.parentNodes.parentTags().equals(
transToken.parentTags)
&& matchTag(
sourceToken.optElementBefore,
transToken.optElementBefore)
&& matchTag(
sourceToken.optElementAfter,
transToken.optElementAfter);
}
private static Optional<Tag> getTag(Optional<Element> elementOptional) {
return elementOptional.isPresent() ? Optional.of(
elementOptional.get().tag()) : Optional.<Tag>absent();
}
private static boolean matchTag(Optional<Element> optSourceEle, Optional<Element> optTargetEle) {
return (getTag(optSourceEle).equals(getTag(optTargetEle)));
}
}
/**
* This will try to match parent tags, but only match one of below in order:
* <pre>
* 1. Before element tag matches
* 2. After element tag matches
* 3. sibling index matches
* </pre>
*/
private static class HeuristicTMSourceToTargetMatchingPredicate
implements Predicate<TextTokenKey> {
private final TranslationToken targetToken;
public HeuristicTMSourceToTargetMatchingPredicate(
TranslationToken targetToken) {
this.targetToken = targetToken;
}
@Override
public boolean apply(
@Nullable TextTokenKey input) {
if (input == null) {
return false;
}
// if parent tag is different, it's definitely wrong
if (!targetToken.parentTags
.equals(input.parentNodes
.parentTags())) {
return false;
}
// before element tag matches
if (targetToken.optElementBefore
.isPresent()) {
Tag beforeTag =
targetToken.optElementBefore
.get().tag();
return input.optElementBefore
.isPresent() &&
input.optElementBefore
.get().tag()
.equals(
beforeTag);
}
// after element tag matches
if (targetToken.optElementAfter
.isPresent()) {
Tag afterTag =
targetToken.optElementAfter
.get().tag();
return input.optElementAfter
.isPresent() &&
input.optElementAfter
.get().tag()
.equals(afterTag);
}
// if nothing matches (tags may have swapped location in translation)
return targetToken.siblingIndex ==
input.siblingIndex;
}
}
}
}