谷歌翻译网站和谷歌翻译API客户端

时间:2018-02-07 08:59:36

标签: google-cloud-platform google-translate

我正在使用谷歌翻译API将文本从阿拉伯语翻译成英语,我在翻译goolge翻译网页和使用API​​进行翻译时得到了不同的结果。谷歌支持阿拉伯语的功能是Arabizi检测,可以在他们的网站上找到但不使用API​​。它将源语言检测为阿拉伯语,但不会将其翻译为英语。

这是google翻译网页上结果的快照。

google translate site result

另一方面,使用google java库或作为rest客户端调用translate api会将语言检测为阿拉伯语,但不会将其翻译。

使用google java客户端库的结果:

文字:3awez aro7 3ala el bait 检测语言:ar,置信度:1.0

从ar到英文的翻译:3awez aro7 3ala el bait

作为休息客户的json结果:

请求正文:TranslateReq [query = 3awez aro7 3ala el bait,target = ar,model = null] JSON响应:

{
  "data": {
    "translations": [
      {
        "translatedText": "3awez aro7 3ala el bait",
        "detectedSourceLanguage": "ar"
      }
    ]
  }
}

以下是代码。

package test;

import java.io.IOException;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.cloud.translate.Detection;
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
import com.tagtac.gs.util.rest.RestUtil;

public class TestGooglerTransAPI
{

    public static void main(String[] args) throws IOException
    {
        boolean useGoogleClientLib = true;
        String text = "3awez aro7 3ala el bait"; // The text to translate

        if(useGoogleClientLib)
        {
            Translate translate = TranslateOptions.getDefaultInstance().getService();

            //Language Detection
            Detection detectedLang = translate.detect(text);
            System.out.printf("Text: %s%n", text);
            System.out.printf("Detected Language: %s, Confidence: %s%n", detectedLang.getLanguage(), detectedLang.getConfidence());

            //Translation
            TranslateOption srcLang = TranslateOption.sourceLanguage(detectedLang.getLanguage());
            TranslateOption targetLang = TranslateOption.targetLanguage("en");
            Translation translation = translate.translate(text, srcLang, targetLang);

            System.out.printf("Translation from %s to English: %s%n", translation.getSourceLanguage(), translation.getTranslatedText());
        }
        else
        {
            //ReST client
            TranslateReq request = new TranslateReq();
            request.setQuery(text);
            request.setTarget("ar");
            System.out.println("Request Body: " + request);

            ResponseEntity<String> resp = 
                    RestUtil.getConfiguredRestTemplate().exchange(
                    "https://translation.googleapis.com/language/translate"
                    + "/v2?key=MY_API_KEY", 
                    HttpMethod.POST, 
                    new HttpEntity<TranslateReq>(request), 
                    String.class);

            System.out.printf("JSON Response:%n%s%n", resp.getBody());
        }
    }


    @JsonInclude(JsonInclude.Include.NON_NULL)
    static class TranslateReq
    {
        @JsonProperty("q")
        private String query;

        private String target;
        private String model;

        public String getQuery()
        {
            return query;
        }

        public void setQuery(String query)
        {
            this.query = query;
        }

        public String getTarget()
        {
            return target;
        }

        public void setTarget(String target)
        {
            this.target = target;
        }

        public String getModel()
        {
            return model;
        }

        public void setModel(String model)
        {
            this.model = model;
        }

        @Override
        public String toString()
        {
            return "TranslateReq [query=" + query + ", target=" + target + ", model=" + model + "]";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用谷歌翻译网站获得翻译答案,因为它实际上被翻译的是文本框下的“您是不是的意思”文本,这是阿拉伯文字符。

“你的意思是”文字来自谷歌的拼写检查API(与使用谷歌搜索时建议替代搜索的API相同)。当您直接使用翻译API时,您正在跳过拼写检查,这就是您没有翻译的原因,Arabzizi不是受支持的语言/格式,需要在翻译之前进行纠正。

当您使用Java开发时,您可能会对adding this step to your program with the Google spelling Java API here

感兴趣

如果您将请求发送到以阿拉伯语编码的API,它会被翻译,请参阅:

 POST https://translation.googleapis.com/language/translate/v2?key={YOUR_API_KEY}

{
 "format": "text",
 "q": [
  "عاوز أروح على إل بيت"
 ],
 "target": "en"
}




Response

200

- Show headers -

{
 "data": {
  "translations": [
   {
    "translatedText": "I want to go to the house",
    "detectedSourceLanguage": "ar"
   }
  ]
 }
}