Skip to content

Commit

Permalink
lang detect removed from the CachedTranslationService
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Nov 22, 2023
1 parent dbbbd38 commit a66c348
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public interface TranslationService {
* @param translationObjs
* @throws TranslationException
*/
void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException;

void detectLanguages(List<TranslationObj> translationObjs) throws TranslationException;

void translate(List<TranslationObj> translationObjs) throws TranslationException;

/**
* to close the engine
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DummyGTranslateService(GoogleTranslationServiceClientWrapper clientWrappe
}

@Override
public void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException {
public void translate(List<TranslationObj> translationObjs) throws TranslationException {
for(TranslationObj obj : translationObjs) {
obj.setTranslation(obj.getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void init(GoogleTranslationServiceClientWrapper clientWrapper) {
}

@Override
public void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException {
public void translate(List<TranslationObj> translationObjs) throws TranslationException {
try {
if(translationObjs.isEmpty()) {
return;
Expand All @@ -62,25 +62,16 @@ public void translate(List<TranslationObj> translationObjs, boolean detectLangua
.map(el -> translationObjs.get(el).getText())
.collect(Collectors.toList());

List<String> sourceLangs = validIndexes.stream()
.filter(el -> translationObjs.get(el).getSourceLang()!=null)
.map(el -> translationObjs.get(el).getSourceLang())
.collect(Collectors.toList());
boolean sameSourceLang = sourceLangs.size()==validIndexes.size() && sourceLangs.stream().distinct().count()==1;
String targetLang = translationObjs.get(validIndexes.get(0)).getTargetLang();
Builder requestBuilder = TranslateTextRequest.newBuilder().setParent(locationName.toString())
.setMimeType(MIME_TYPE_TEXT).setTargetLanguageCode(targetLang).addAllContents(texts);
//only set the source language if it is the same for all texts
if (sameSourceLang) {
requestBuilder.setSourceLanguageCode(sourceLangs.get(0));
}
TranslateTextRequest request = requestBuilder.build();

TranslateTextResponse response = this.clientWrapper.getClient().translateText(request);

int counter=0;
for (Translation t : response.getTranslationsList()) {
if(! sameSourceLang) {
if(translationObjs.get(validIndexes.get(counter)).getSourceLang()==null) {
translationObjs.get(validIndexes.get(counter)).setSourceLang(t.getDetectedLanguageCode());
}
translationObjs.get(validIndexes.get(counter)).setTranslation(t.getTranslatedText());
Expand Down Expand Up @@ -123,8 +114,4 @@ public void close() {
this.clientWrapper.close();
}

@Override
public void detectLanguages(List<TranslationObj> translationObjs)
throws TranslationException {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DummyPangTranslationService() {
}

@Override
public void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException {
public void translate(List<TranslationObj> translationObjs) throws TranslationException {
for(TranslationObj obj : translationObjs) {
obj.setTranslation(obj.getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ private boolean isTargetSupported(String targetLanguage) {
}

@Override
public void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException {
public void translate(List<TranslationObj> translationObjs) throws TranslationException {
try {
if(translationObjs.isEmpty()) return;

if(detectLanguages) {
detectLanguages(translationObjs);
}
detectLanguages(translationObjs);

computeTranslations(translationObjs);

Expand Down Expand Up @@ -149,9 +147,8 @@ private void computeTranslations(List<TranslationObj> translationObjs) throws JS
}
}
}

@Override
public void detectLanguages(List<TranslationObj> translationObjs) throws TranslationException {

private void detectLanguages(List<TranslationObj> translationObjs) throws TranslationException {
List<Integer> indexesWithoutSourceAndTranslation = IntStream.range(0, translationObjs.size())
.filter(i -> translationObjs.get(i).getSourceLang()==null && translationObjs.get(i).getTranslation()==null)
.boxed()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
contents: "eine Textzeile auf Deutsch"
contents: "eine zweite Textzeile auf Deutsch"
mime_type: "text/plain"
source_language_code: "de"
target_language_code: "en"
parent: "projects/project-id-test/locations/global"
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
public class CachedTranslationService extends AbstractTranslationService {
private RedisCacheService redisCacheService;
private TranslationService translationService;
private TranslationService translationServicePangeanic;
private String serviceId;

/*
* The pangeanic translation service is used to detect the source languages of the input texts,
* before the lookup to the cache is made.
*/
public CachedTranslationService(RedisCacheService redisCacheService, TranslationService translationService, TranslationService translationServicePangeanic) {
public CachedTranslationService(RedisCacheService redisCacheService, TranslationService translationService) {
super();
this.redisCacheService = redisCacheService;
this.translationService = translationService;
this.translationServicePangeanic = translationServicePangeanic;
this.serviceId = translationService.getServiceId();
}

Expand All @@ -41,19 +39,14 @@ public boolean isSupported(String srcLang, String trgLang) {
}

@Override
public void translate(List<TranslationObj> translationObjs, boolean detectLanguages) throws TranslationException {
//first detect languages for the texts that do not have it using the pangeanic lang detect
translationServicePangeanic.detectLanguages(translationObjs);
//then check if the translations exist in cache
public void translate(List<TranslationObj> translationObjs) throws TranslationException {
redisCacheService.getCachedTranslations(translationObjs);
boolean anyCachedTransl = translationObjs.stream().filter(el -> el.getIsCached()).collect(Collectors.toList()).size()>0;
//if there is any translation in the cache set the serviceId to null, because we do not know which service translated that
if(anyCachedTransl) {
setServiceId(null);
}
//then translate those that do not exist
translationService.translate(translationObjs, false);
//and save those that do not exist in cache to the cache
translationService.translate(translationObjs);
redisCacheService.saveRedisCache(translationObjs);
}

Expand All @@ -66,9 +59,4 @@ public String getExternalServiceEndPoint() {
return null;
}

@Override
public void detectLanguages(List<TranslationObj> translationObjs)
throws TranslationException {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ public TranslationResponse translate(TranslationRequest translationRequest) thro
if (translationRequest.getFallback() != null) {
fallback = getTranslationService(translationRequest.getFallback(), languagePair, true);
}
TranslationService translationServicePangeanic = getTranslationService("PANGEANIC", languagePair);

//create the architecture with/without caching services
List<TranslationService> translServicesToCall = new ArrayList<TranslationService>();
if(translationRequest.useCaching() && isCachingEnabled()) {
CachedTranslationService cachedTranslServ1 = new CachedTranslationService(redisCacheService, translationService, translationServicePangeanic);
CachedTranslationService cachedTranslServ1 = new CachedTranslationService(redisCacheService, translationService);
translServicesToCall.add(cachedTranslServ1);
if(fallback!=null) {
CachedTranslationService cachedTranslServ2 = new CachedTranslationService(redisCacheService, fallback, translationServicePangeanic);
CachedTranslationService cachedTranslServ2 = new CachedTranslationService(redisCacheService, fallback);
translServicesToCall.add(cachedTranslServ2);
}
} else {
Expand All @@ -77,7 +76,7 @@ public TranslationResponse translate(TranslationRequest translationRequest) thro
String serviceId=null;
for(TranslationService translServ : translServicesToCall) {
try {
translServ.translate(translObjs, true);
translServ.translate(translObjs);
//call this method after the translate() method, because the serviceId changes depending if there is sth in the cache
serviceId = translServ.getServiceId();
break;
Expand Down

0 comments on commit a66c348

Please sign in to comment.