Difference between revisions of "LingoCloud API in 5 minutes"

From Caiyun Wiki
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<div style="padding:5px;border: 1px dashed MediumAquamarine;text-align:right;float:right">
 
<div style="padding:5px;border: 1px dashed MediumAquamarine;text-align:right;float:right">
 
<div>
 
<div>
[[LingoCloud/en|English]]·[[LingoCloud/ja|日本语]]·[[LingoCloud/zhs|简体中文]]·[[LingoCloud/zht|繁体中文]]
+
[https://app.swaggerhub.com/apis/caiyun/lingo-cloud_api/1.0.1 API spec]
</div>
 
<div>
 
[https://app.swaggerhub.com/apis/caiyun/lingo-cloud_api/1.0.0 API spec]
 
 
</div>
 
</div>
 
</div>
 
</div>
Line 42: Line 39:
 
fi
 
fi
  
BODY='{"source": ["'\$SOURCE'"], "trans_type": "'\$DIRECTION'", "request_id": "demo" }'
+
BODY='{"source": ["'\$SOURCE'"], "trans_type": "'\$DIRECTION'", "replaced": true, "media": "text", "request_id": "demo" }'
  
 
export PYTHONIOENCODING=utf8
 
export PYTHONIOENCODING=utf8
Line 72: Line 69:
  
 
== In Python ==
 
== In Python ==
<syntaxhighlight lang="python">
+
<source lang="python">
 
import requests
 
import requests
 
import json
 
import json
Line 78: Line 75:
 
url = "http://api.interpreter.caiyunai.com/v1/translator"
 
url = "http://api.interpreter.caiyunai.com/v1/translator"
  
payload = "{\"source\":[\"Lingocloud is the best translation service.\"],\"trans_type\": \"en2zh\", \"request_id\": \"a11111\"}"
+
payload = "{\"source\":[\"Lingocloud is the best translation service.\"],\"trans_type\": \"en2zh\", \"replaced\": true, \"media\": \"text\", \"request_id\": \"demo\"}"
 
headers = {
 
headers = {
 
     'content-type': "application/json",
 
     'content-type': "application/json",
Line 89: Line 86:
 
print(json.loads(response.text)['target'])
 
print(json.loads(response.text)['target'])
  
</syntaxhighlight>
+
</source>
  
 
Output:
 
Output:
<syntaxhighlight lang="javascript">
+
<source lang="javascript">
 
  {"confidence":0.9938237638,"target":"\u5c0f\u8bd1\u662f\u6700\u597d\u7684\u7ffb\u8bd1\u670d\u52a1\u3002","isdict":0,"rc":0}
 
  {"confidence":0.9938237638,"target":"\u5c0f\u8bd1\u662f\u6700\u597d\u7684\u7ffb\u8bd1\u670d\u52a1\u3002","isdict":0,"rc":0}
  
 
  小译是最好的翻译服务。
 
  小译是最好的翻译服务。
</syntaxhighlight>
+
</source>
  
 
==A version support concurrent request==
 
==A version support concurrent request==
Line 102: Line 99:
 
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.
 
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.
  
<syntaxhighlight lang="python">
+
<source lang="python">
 
import requests
 
import requests
 
import json
 
import json
Line 108: Line 105:
 
url = "http://api.interpreter.caiyunai.com/v1/translator"
 
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\", \"request_id\": \"a11111\"}"
+
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 = {
 
headers = {
 
     'content-type': "application/json",
 
     'content-type': "application/json",
Line 119: Line 116:
 
print(json.loads(response.text)['target'][0])
 
print(json.loads(response.text)['target'][0])
 
print(json.loads(response.text)['target'][1])
 
print(json.loads(response.text)['target'][1])
</syntaxhighlight>
+
</source>
  
 
=The supported languages=
 
=The supported languages=
Line 147: Line 144:
  
 
Please send your feedback or advice to us at [mailto:api@caiyunapp.com api@caiyunapp.com]!
 
Please send your feedback or advice to us at [mailto:api@caiyunapp.com api@caiyunapp.com]!
 +
 +
[[zh-cn:五分钟学会彩云小译 API]]

Latest revision as of 14:54, 7 December 2018

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!