Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
WIP: refactor copy trans
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 10, 2014
1 parent 27a49fe commit 9459a43
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 142 deletions.
Expand Up @@ -47,6 +47,11 @@ Optional<HTextFlowTarget> searchBestMatchTransMemory(HTextFlow textFlow,
LocaleId targetLocaleId, LocaleId sourceLocaleId,
boolean checkContext, boolean checkDocument, boolean checkProject);

Optional<TransMemoryResultItem> searchBestMatchTransMemory(
HTextFlow textFlow, LocaleId targetLocaleId,
LocaleId sourceLocaleId, boolean checkContext,
boolean checkDocument, boolean checkProject, int thresholdPercent);

List<TransMemoryResultItem> searchTransMemory(LocaleId targetLocaleId,
LocaleId sourceLocaleId, TransMemoryQuery transMemoryQuery);
}
Expand Up @@ -20,10 +20,8 @@
*/
package org.zanata.service.impl;

import static com.google.common.collect.Collections2.filter;
import static org.zanata.service.SecurityService.TranslationAction.MODIFY;

import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -50,15 +48,13 @@
import org.zanata.service.TranslationService;
import org.zanata.webtrans.server.rpc.TransMemoryMergeStatusResolver;
import org.zanata.webtrans.shared.model.TransMemoryDetails;
import org.zanata.webtrans.shared.model.TransMemoryQuery;
import org.zanata.webtrans.shared.model.TransMemoryResultItem;
import org.zanata.webtrans.shared.model.TransUnitUpdateRequest;
import org.zanata.webtrans.shared.rpc.HasSearchType;
import org.zanata.webtrans.shared.rpc.MergeRule;
import org.zanata.webtrans.shared.rpc.TransMemoryMerge;
import org.zanata.webtrans.shared.rpc.TransUnitUpdated;

import com.google.common.base.Predicate;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -89,13 +85,10 @@ public class TransMemoryMergeServiceImpl implements TransMemoryMergeService {
@In
private TranslationService translationServiceImpl;

private static final TransMemoryResultItem NULL_OBJECT =
new TransMemoryResultItem(null, null, null, 0, 0);

@Override
public List<TranslationService.TranslationResult> executeMerge(
TransMemoryMerge action) throws ActionException {
HLocale hLocale =
HLocale targetLocale =
localeServiceImpl.getByLocaleId(action.getWorkspaceId()
.getLocaleId());

Expand All @@ -109,14 +102,10 @@ public List<TranslationService.TranslationResult> executeMerge(
textFlowDAO
.findByIdList(Lists.newArrayList(requestMap.keySet()));

TransMemoryAboveThresholdPredicate predicate =
new TransMemoryAboveThresholdPredicate(
action.getThresholdPercent());

List<TransUnitUpdateRequest> updateRequests = Lists.newArrayList();
for (HTextFlow hTextFlow : hTextFlows) {
HTextFlowTarget hTextFlowTarget =
hTextFlow.getTargets().get(hLocale.getId());
hTextFlow.getTargets().get(targetLocale.getId());

// TODO rhbz953734 - TM getUpdateRequests won't override Translated
// to Approved
Expand All @@ -128,35 +117,26 @@ public List<TranslationService.TranslationResult> executeMerge(
continue;
}

TransMemoryQuery.Condition project =
new TransMemoryQuery.Condition(
action.getDifferentProjectRule() == MergeRule.REJECT,
hTextFlow.getDocument().getProjectIteration()
.getProject().getSlug());
TransMemoryQuery.Condition document =
new TransMemoryQuery.Condition(
action.getDifferentDocumentRule() == MergeRule.REJECT,
hTextFlow.getDocument().getDocId());
TransMemoryQuery.Condition res =
new TransMemoryQuery.Condition(
action.getDifferentContextRule() == MergeRule.REJECT,
hTextFlow.getResId());

List<TransMemoryResultItem> tmResults =
translationMemoryServiceImpl.searchTransMemory(hLocale
.getLocaleId(), hTextFlow.getDocument().getLocale()
.getLocaleId(),
new TransMemoryQuery(hTextFlow.getContents(),
HasSearchType.SearchType.FUZZY_PLURAL,
project, document, res));

TransMemoryResultItem tmResult =
findTMAboveThreshold(tmResults, predicate, hTextFlow,
boolean checkContext =
action.getDifferentContextRule() == MergeRule.REJECT;

boolean checkDocument =
action.getDifferentDocumentRule() == MergeRule.REJECT;

boolean checkProject =
action.getDifferentProjectRule() == MergeRule.REJECT;

Optional<TransMemoryResultItem> tmResult =
translationMemoryServiceImpl.searchBestMatchTransMemory(
hTextFlow, targetLocale.getLocaleId(), hTextFlow
.getDocument().getLocale().getLocaleId(),
checkContext, checkDocument, checkProject,
action.getThresholdPercent());
TransUnitUpdateRequest request =
createRequest(action, hLocale, requestMap, hTextFlow,
tmResult, hTextFlowTarget);
if (request != null) {

if (tmResult.isPresent()) {
TransUnitUpdateRequest request =
createRequest(action, targetLocale, requestMap,
hTextFlow, tmResult.get(), hTextFlowTarget);
updateRequests.add(request);
}
}
Expand Down Expand Up @@ -195,9 +175,6 @@ private TransUnitUpdateRequest createRequest(TransMemoryMerge action,
HLocale hLocale, Map<Long, TransUnitUpdateRequest> requestMap,
HTextFlow hTextFlowToBeFilled, TransMemoryResultItem tmResult,
HTextFlowTarget oldTarget) {
if (tmResult == NULL_OBJECT) {
return null;
}

Long tmSourceId = tmResult.getSourceIdList().get(0);
ContentState statusToSet;
Expand Down Expand Up @@ -236,69 +213,16 @@ private TransUnitUpdateRequest createRequest(TransMemoryMerge action,
}

private static String buildTargetComment(TransMemoryDetails tmDetail) {
return new StringBuilder(
"auto translated by TM merge from ")
return new StringBuilder("auto translated by TM merge from ")
.append("project: ").append(tmDetail.getProjectName())
.append(", version: ").append(tmDetail.getIterationName())
.append(", DocId: ").append(tmDetail.getDocId()).toString();
}

private static String buildTargetComment(TransMemoryUnit tu) {
return new StringBuilder(
"auto translated by TM merge from ")
return new StringBuilder("auto translated by TM merge from ")
.append("translation memory: ")
.append(tu.getTranslationMemory().getSlug())
.append(", unique id: ").append(tu.getUniqueId()).toString();
}

private static class TransMemoryAboveThresholdPredicate implements
Predicate<TransMemoryResultItem> {
private final int approvedThreshold;

public TransMemoryAboveThresholdPredicate(int approvedThreshold) {
this.approvedThreshold = approvedThreshold;
}

@Override
public boolean apply(TransMemoryResultItem tmResult) {
return (int) tmResult.getSimilarityPercent() >= approvedThreshold;
}
}

private static TransMemoryResultItem findTMAboveThreshold(
List<TransMemoryResultItem> tmResults,
TransMemoryAboveThresholdPredicate predicate,
final HTextFlow hTextFlow, int thresholdPercent) {

Collection<TransMemoryResultItem> aboveThreshold;

if (thresholdPercent == 100) {
aboveThreshold =
filter(tmResults,
new ContentsIdenticalPredicate(hTextFlow
.getContents()));
} else {
aboveThreshold = filter(tmResults, predicate);
}
if (aboveThreshold.size() > 0) {
return aboveThreshold.iterator().next();
} else {
return NULL_OBJECT;
}
}

private static class ContentsIdenticalPredicate implements
Predicate<TransMemoryResultItem> {
private final List<String> sourceContents;

public ContentsIdenticalPredicate(List<String> sourceContents) {
this.sourceContents = sourceContents;
}

@Override
public boolean apply(TransMemoryResultItem input) {
return input.getSourceContents().equals(sourceContents);
}
}

}

0 comments on commit 9459a43

Please sign in to comment.