diff --git a/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java b/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java index 4c94a56b..146cd916 100644 --- a/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java +++ b/translation-service-common/src/main/java/eu/europeana/api/translation/service/TranslationService.java @@ -46,7 +46,7 @@ public interface TranslationService { */ void translate(List translationObjs, boolean detectLanguages) throws TranslationException; - void detectLanguages(List translationObjs, List validIndexes) throws TranslationException; + void detectLanguages(List translationObjs) throws TranslationException; @Deprecated /** diff --git a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java index fbd78aef..11eafdd6 100644 --- a/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java +++ b/translation-service-google/src/main/java/eu/europeana/api/translation/service/google/GoogleTranslationService.java @@ -166,7 +166,7 @@ public void close() { } @Override - public void detectLanguages(List translationObjs, List validIndexes) + public void detectLanguages(List translationObjs) throws TranslationException { } } diff --git a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java index 9e2f36ce..057e1714 100644 --- a/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java +++ b/translation-service-pangeanic/src/main/java/eu/europeana/api/translation/service/pangeanic/PangeanicTranslationService.java @@ -104,31 +104,12 @@ public void translate(List translationObjs, boolean detectLangua try { if(translationObjs.isEmpty()) return; - /*here we analyze the general case where some texts may have the source lang, some may not, and - * the source languages may be different, but only for the texts that already do not have the translations - * (maybe fetched from the cache or computed with another service) - */ if(detectLanguages) { - List validIndexesWithoutSourceLang = IntStream.range(0, translationObjs.size()) - .filter(i -> translationObjs.get(i).getTranslation()==null && translationObjs.get(i).getSourceLang()==null) - .boxed() - .collect(Collectors.toList()); - if(!validIndexesWithoutSourceLang.isEmpty()) { - detectLanguages(translationObjs, validIndexesWithoutSourceLang); - } + detectLanguages(translationObjs); } - List validIndexes = IntStream.range(0, translationObjs.size()) - .filter(i -> translationObjs.get(i).getTranslation()==null) - .boxed() - .collect(Collectors.toList()); - List sourceLangs = new ArrayList(); - for(Integer validIndexWithSourceLangElem : validIndexes) { - sourceLangs.add(translationObjs.get(validIndexWithSourceLangElem).getSourceLang()); - } - if(!validIndexes.isEmpty()) { - computeTranslations(translationObjs, sourceLangs, validIndexes); - } + computeTranslations(translationObjs); + } catch (JSONException e) { throw new TranslationException("Exception occured during Pangeanic translation!", @@ -146,52 +127,58 @@ public List translate(List texts, String targetLanguage) return translate(texts, targetLanguage, null); } - private void computeTranslations(List translationObjs, List detectedLanguages, List validIndexes) throws JSONException, TranslationException { - if (LOG.isDebugEnabled()) { - LOG.debug( - "Pangeanic detect lang request with hint null is executed. Detected languages are {} ", - LoggingUtils.sanitizeUserInput(detectedLanguages.toString())); - } - + private void computeTranslations(List translationObjs) throws JSONException, TranslationException { List analyzedLangs = new ArrayList(); - for(int i=0;i translIndexes = new ArrayList(); - translIndexes.add(validIndexes.get(i)); + translIndexes.add(i); List translTexts = new ArrayList(); - translTexts.add(translationObjs.get(validIndexes.get(i)).getText()); - String targetLang = translationObjs.get(validIndexes.get(i)).getTargetLang(); - if(sourceLang!=null) { - for(int j=i+1;j translationObjs, List validIndexes) throws TranslationException { + public void detectLanguages(List translationObjs) throws TranslationException { + List indexesWithoutSourceAndTranslation = IntStream.range(0, translationObjs.size()) + .filter(i -> translationObjs.get(i).getSourceLang()==null && translationObjs.get(i).getTranslation()==null) + .boxed() + .collect(Collectors.toList()); + if(indexesWithoutSourceAndTranslation.isEmpty()) { + return; + } + if (langDetectService == null) { throw new TranslationException("No langDetectService configured!", HttpStatus.SC_INTERNAL_SERVER_ERROR); } - List detectedLanguages=null; - List texts = validIndexes.stream() + List texts = indexesWithoutSourceAndTranslation.stream() .map(index -> translationObjs.get(index).getText()) .collect(Collectors.toList()); + List detectedLanguages=null; try { detectedLanguages = langDetectService.detectLang(texts, null); } catch (LanguageDetectionException e) { @@ -199,8 +186,15 @@ public void detectLanguages(List translationObjs, List e.getRemoteStatusCode(), e); } - for(int i=0;i translate(List texts, String targetLanguage) throws @Override public void translate(List translationObjs, boolean detectLanguages) throws TranslationException { //first detect languages for the texts that do not have it using the pangeanic lang detect - List indexesWithoutSourceLang = IntStream.range(0, translationObjs.size()) - .filter(i -> translationObjs.get(i).getSourceLang()==null) - .boxed() - .collect(Collectors.toList()); - if(!indexesWithoutSourceLang.isEmpty()) { - translationServicePangeanic.detectLanguages(translationObjs, indexesWithoutSourceLang); - } - + translationServicePangeanic.detectLanguages(translationObjs); //then check if the translations exist in cache 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 + //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); } @@ -85,7 +77,7 @@ public String getExternalServiceEndPoint() { } @Override - public void detectLanguages(List translationObjs, List validIndexes) + public void detectLanguages(List translationObjs) throws TranslationException { }