LingoCloud API in 5 minutes

From CaiyunWiki
Jump to: navigation, search

What can LingoCloud API be used for?

The LingoCloud API can be used to translate information into a specified language. We can support text, Web pages, PDF / Word documents, audio, video and other forms.

You can use it to build your application, website, tool, or any solution that requires multi-lingual support.

One Minute Introduction

In the next minute, we'll create a command line tool that lets you translate a short sentence.

Apply for an access token

  • Please send email to api@caiyunapp.com to apply for an access token. Please tell us what you want to do with our translation API? We'll write back and send a token to you.
  • Please change the title of the message to "apply for access token of LingoCloud API" and provide the following information: name, cell phone number, usage, etc.
  • 100,000 words per month are free of charge, and if you exceed one million words per month, we will charge you at a rate of 20 RMB per million words. (words are calculated in accordance with the original characters and contain spaces and punctuation)

A command line translation tool

Performs the following Bash script to create a command line translation tool.

#!/bin/bash
tee xiaoyi.sh << END
DIRECTION=\$1
SOURCE=\$2

if test -f \$HOME/.xiaoyi ; then
  . \$HOME/.xiaoyi
else
  echo "Please input your token: "
  read TOKEN
  echo "TOKEN=\$TOKEN" > \$HOME/.xiaoyi
fi

BODY='{"source": ["'\$SOURCE'"], "trans_type": "'\$DIRECTION'", "replaced": true, "media": "text", "request_id": "demo" }'

export PYTHONIOENCODING=utf8
curl -s -X POST http://api.interpreter.caiyunai.com/v1/translator\
      -H 'Content-Type: application/json'\
      -H "X-Authorization: token \$TOKEN"\
      -d "\$BODY" | python -c "import sys, json; print json.load(sys.stdin)['target']"
END

Excecute xiaoyi.sh as below and you will see the result

sh xiaoyi.sh en2zh "You know some birds are not meant to be caged, their feathers are just too bright."
你知道有些鸟不应该被关在笼子里,它们的羽毛太亮了。
sh xiaoyi.sh ja2zh "薄紅の秋の実に"
淡红色的秋天的果实

Note: The first time you execute xiaoyi.sh, it will be promoted a message to let you enter the access token

More advanced Examples

Please set your token as YOURTOKEN

In Python

import requests
import json

url = "http://api.interpreter.caiyunai.com/v1/translator"

payload = "{\"source\":[\"Lingocloud is the best translation service.\"],\"trans_type\": \"en2zh\", \"replaced\": true, \"media\": \"text\", \"request_id\": \"demo\"}"
headers = {
    'content-type': "application/json",
    'x-authorization': "token YOUR_TOKEN",
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
print(json.loads(response.text)['target'])

Output:

 {"confidence":0.9938237638,"target":"\u5c0f\u8bd1\u662f\u6700\u597d\u7684\u7ffb\u8bd1\u670d\u52a1\u3002","isdict":0,"rc":0}

 小译是最好的翻译服务

A version support concurrent request

Concurrent requests can make the requests as fast as possible. Please package 20-40 sentences of similar length when it is feasible. And below is the exemplary code for concurrent requests.

import requests
import json

url = "http://api.interpreter.caiyunai.com/v1/translator"

payload = "{\"source\":[\"Lingocloud is the best translation service.\", \"ColorfulClouds Weather is the best weather service.\"],\"trans_type\": \"en2zh\", \"replaced\": true, \"media\": \"text\", \"request_id\": \"demo\"}"
headers = {
    'content-type': "application/json",
    'x-authorization': "token YOUR_TOKEN",
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
print(json.loads(response.text)['target'][0])
print(json.loads(response.text)['target'][1])

The supported languages

The Language and translation directions currently supported are shown in the following table

To
From
Chinese zh English en Japanese ja
Chinese zh - zh2en zh2ja
English en en2zh - -
Japanese ja ja2zh - -

The API is about to support automatic language recognition. And by the end of 2018, it will support more than 15 languages and more than 200 translation directions.

Anything more?

Please send your feedback or advice to us at api@caiyunapp.com!