技術部落格

集結國內外精選文章,掌握最新雲端技術新知與應用

iKala Cloud / AI 與機器學習 / Machine Learning(三):Cloud Translation API 介紹與實作

Machine Learning(三):Cloud Translation API 介紹與實作

GCP 機器學習(3) – Cloud Translation API 應用實例

Google Translation 翻譯 icon

來到 Google Cloud Platform 系列文的第三篇,今天要講解 Translation API。我認為語言翻譯 API 是一般大眾最為熟悉的,今天就讓我們來看一下,它到底還有什麼新把戲?

快速測試 Translate API

首先我們先點進 Translate API 的介紹 網址。大家可以快速的使用 Google 提供的小 board 來 做測試,這部分我就不加贅述。

translation API 介紹

語言翻譯

接著我們進行 API 測試,但在開始之前,我們必須先 setup 一個 project,請參考[Machine Learning(一):Cloud Speech API 介紹與實作] 來設置一個新的 project。如果你和我一樣是延用上一次的 project,那請記得要打開 translation API。點選右方的 bar、API和服務、然後啟用 API 和服務。搜尋到 translation API 之後,將它開啟。

啟用 API 服務

設置完畢後,我們就來發一些  REST request,看可以得到什麼 response。新增一個名為 translate-request.json 的檔案,而檔案的內容如下:

 {
  "q": "The quick brown fox jumped over the lazy dog.",
  "source": "en",
  "target": "zh-tw",
  "format": "text"
}

然後在 command line 輸入以下指令:

 curl -s -X POST -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer ACCESS_TOKEN' \
    'https://translation.googleapis.com/language/translate/v2' \
    -d @translate-request.json

那麼應該會看到如下的 response:

command line response

順帶一提,快速的棕色狐狸跳過了懶狗 和 CoreNLP 預設的測試字串是一樣的 。

偵測語言

如果各位常用 Google Chrome 逛國外網站的話,例如像是 Yahoo Japan,那麼一定常會看到以下圖片。這代表 Google Chrome 已經自動幫你偵測出此網站最可能的語言了。想當然 Google Chrome 一定也是用了 Google Cloud Platform 的 API —— 偵測語言。你要翻譯網頁內容嗎

請將 request.json 改寫成如下:

 {
  "q": [
    "The quick brown fox jumped over the lazy dog.",
    "你好嗎?",
    "C'est la vie"
  ]
}

然後 API 的網址改成如下,再 post 一次。

https://translation.googleapis.com/language/translate/v2/detect

那麼應該會得到如下的 response:

改API網址後response

其中 confidence 代表的是偵測結果的信心程度,而 isReliable 代表這個偵測結果是否值得相信。

列出語言別

如果想要知道 Translate API 總共提供了哪些語言翻譯,請 GET 以下這個 URL:

https://translation.googleapis.com/language/translate/v2/languages

那麼會得到 response 如下:

總共語言翻譯

經過計算 ,到目前為止總共有 104 種語言。

API Explorer

如果不習慣打指令的同學, Google Cloud Platform 也提供了很直覺得 Web 測試工具 —— API Explorer。點到剛剛啟用 API 的畫面,這次要點選旁邊的試用這個 API。

API Explorer

接著會看到如下的選單,我們測試第一個 language detection。

語言偵測

接著照著我們之前輸的格式輸進去,按下方藍色的按鈕,就可以得到和前面一模一樣的 response 了。

API response

Google Translation API 計價方式

Google Translation API 的計價方式為每百萬個字 20 USD,也就是說,如果我們拿 Google Translation API 來翻譯一本羅密歐與茱麗葉,大概是 45 元新台幣,折合一杯珍珠奶茶。那麼如果你再省下更多杯珍奶的錢,去翻譯一套莎士比亞全集,那麼你不僅會變得更瘦,也會變得更有涵養喔!Translate API 費用

這一系列的文章將帶您跨過 GCP 機器學習領域的實作門檻,下一篇會為您介紹:Cloud Vision API
機器學習 – 相關文章:
Machine Learning(一):Cloud Speech API 介紹與實作
Machine Learning(二):Natural Language API 介紹與實作
Machine Learning(四):Cloud Vision API 介紹與實作
Machine Learning(五):Cloud Video Intelligence API 介紹與實作
Machine Learning 是什麼? 該怎麼應用? 一份來自資深 Googler 的 ML 介紹課程

(本文來自合作部落客 沒一村:沒一村生活點滴,iKala Cloud授權轉載。)

 

分享本文:
FacebookLineTwitter
回到頂端