中文注音


简介

HI,您好,欢迎使用学而思网校AI开放平台中文注音接口服务。

本文档主要针对需要集成HTTP API的技术研发工程师,详细描述中文注音能力相关的技术内容。您可以通过快速接入安全接入使用此AI能力。

接口能力

接口名称 接口能力简要描述 api 地址
中文注音 对用户上传的汉字文本进行注音 http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic

适用范围

任意操作系统,任意编程语言,均可以通过http或https调用本接口。

文字识别范围

汉字与中文标点符号,不可以出现数字与英文字母,最多一次性识别250个双字节字符。

请求方式

HTTP 方法POST

请求 URLhttp://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic

注意: Content-Type为application/x-www-form-urlencoded,然后通过urlencode格式化请求体。

Header 如下

参数名 赋值
Content-Type application/x-www-form-urlencoded

 

快速接入方式

请求参数详情

参数名 类型 是否必选 赋值说明 样例 备注
app_key string 应用标识 8102b22a5e81e840176d9f381ec6f837  
text string 待注音的文本 待注音的文本  

请求代码示例

HTTP 代码示例:

POST /v1/api/nlp/nlp/phonetic HTTP/1.1
Host: openapiai.xueersi.com
Content-Type: application/x-www-form-urlencoded
User-Agent: PostmanRuntime/7.15.0
Accept: */*
Cache-Control: no-cache
Postman-Token: be1269b7-f235-4096-a14d-a663986c47cc,d1253d40-138a-465a-aaec-4865bfa1c56a
Host: 43.228.39.217:18092
accept-encoding: gzip, deflate
content-length: 100
Connection: keep-alive
cache-control: no-cache

app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC

cURL 代码示例:

curl -X POST 
  http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic 
  -H 'Accept: */*' 
  -H 'Cache-Control: no-cache' 
  -H 'Connection: keep-alive' 
  -H 'Content-Type: application/x-www-form-urlencoded' 
  -H 'Host: openapiai.xueersi.com' 
  -H 'Postman-Token: be1269b7-f235-4096-a14d-a663986c47cc,74f9e0b6-30bc-4be2-9b8d-4e9963dc9287' 
  -H 'User-Agent: PostmanRuntime/7.15.0' 
  -H 'accept-encoding: gzip, deflate' 
  -H 'cache-control: no-cache' 
  -H 'content-length: 100' 
  -d 'app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC'

PHP 代码示例:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Python(Python3) 代码示例:

URL传参方式:

import http.client

conn = http.client.HTTPConnection("openapiai.xueersi.com")

payload = "app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC"

headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
}

conn.request("POST", "/v1/api/nlp/nlp/phonetic", payload, headers)

res = conn.getresponse()
data = res.read()
#python2.7
#print(data)
#python3.6
print(data.decode("utf-8"))
conn.close()

C++(LibCurl) 代码示例:

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC");

CURLcode ret = curl_easy_perform(hnd);

Java 代码示例:

public static void QuickConnect(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("app_key", "8102b22a5e81e840176d9f381ec6f837");

                   
                    params.put("text","待注音的文本");

                    Log.i(TAG,"start post");
                    String result = HttpUtil.sendPostMessage(params,"utf-8");
                    System.out.println("result->"+result);

                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }).start();


    }

JavaScript 代码示例:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic",
  "method": "POST",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "cache-control": "no-cache",
  },
  "data": {
    "app_key": "8102b22a5e81e840176d9f381ec6f837",
    "text": "待注音的文本"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

Go 代码示例:

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic"

	payload := strings.NewReader("app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("cache-control", "no-cache")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}

C# 代码示例:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string celerityPost = CelerityPost();
            Console.WriteLine("快速接入方式:\n" + celerityPost);
            Console.ReadLine();
        }
        private static string CelerityPost()
        {
            string url = "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic";//URL地址
            string payload = "app_key=8102b22a5e81e840176d9f381ec6f837&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC";
            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.ContentLength = Encoding.UTF8.GetByteCount(payload);
            webRequest.ContentType = "application/x-www-form-urlencoded";//Content-Type
            webRequest.CookieContainer = cookieContainer;
            webRequest.Method = "post";
            Stream request = webRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(request, Encoding.GetEncoding("gb2312"));
            streamWriter.Write(payload);
            streamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
            Stream returnStream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(returnStream, Encoding.GetEncoding("utf-8"));
            string endResult = streamReader.ReadToEnd();//返回结果
            streamReader.Close();
            returnStream.Close();
            return endResult;
        }
    }
}

微信小程序 代码示例:

wx.request({
      url: 'http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic',
      method: "post",
      data:
      {
        app_key: "8102b22a5e81e840176d9f381ec6f837",
        text: "待注音的文本"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
      },
      success(res) {
        console.log(res.data)
      }
    })

iOS 代码示例:

NSURLSession *session = [NSURLSession sharedSession];
NSString *urlStr = @"http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *app_key = @"app_key=8102b22a5e81e840176d9f381ec6f837";
NSString *text = @"text=待注音的文本";
NSString *paramsStr = [NSString stringWithFormat:@"%@&%@&%@", app_key, text];
request.HTTPBody = [paramsStr dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"result=%@",dict);
}];
[dataTask resume];

安全接入方式

请求参数详情

参数名 类型 是否必选 赋值说明 样例 备注
app_key string 应用标识 8102b22a5e81e840176d9f381ec6f837  
time_stamp string 秒级时间戳 1493468759 安全接入必备,用于唯一地标识某一刻的时间
nonce_str string 随机字符串 dd599ef889859f9fe 安全接入必备
sign string 签名信息 99880aabb33f4def5c875875b6bdc3b1 安全接入必备
text string 待注音的文本 待注音的文本  

请求代码示例

HTTP 代码示例:

POST /v1/api/nlp/nlp/phonetic HTTP/1.1
Host: openapiai.xueersi.com
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC

cURL 代码示例:

curl -X POST \
  http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'cache-control: no-cache' \
  -d 'app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC'

PHP 代码示例:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Python(Python3) 代码示例:

URL传参方式:

import http.client

conn = http.client.HTTPConnection("openapiai.xueersi.com")

payload = "app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC"

headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
}

conn.request("POST", "/v1/api/nlp/nlp/phonetic", payload, headers)

res = conn.getresponse()
data = res.read()
#python2.7
#print(data)
#python3.6
print(data.decode("utf-8"))
conn.close()

C++(LibCurl) 代码示例:

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC");

CURLcode ret = curl_easy_perform(hnd);

Java 代码示例:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC");
Request request = new Request.Builder()
  .url("http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic")
  .post(body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

JavaScript 代码示例:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic",
  "method": "POST",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "cache-control": "no-cache",
  },
  "data": {
    "app_key": "8102b22a5e81e840176d9f381ec6f837",
    "time_stamp": "1493468759",
    "nonce_str": "dd599ef889859f9fe",
    "sign": "99880aabb33f4def5c875875b6bdc3b1",
    "text": "待注音的文本"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

Go 代码示例:

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic"

	payload := strings.NewReader("app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("cache-control", "no-cache")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}

C# 代码示例:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string safetyPost = SafetyPost();
            Console.WriteLine("安全接入方式:\n" + safetyPost);
            Console.ReadLine();
        }
        private static string SafetyPost()
        {
            string url = "http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic";//URL地址
            string payload = "app_key=8102b22a5e81e840176d9f381ec6f837&time_stamp=1493468759&nonce_str=dd599ef889859f9fe&sign=99880aabb33f4def5c875875b6bdc3b1&text=%E5%BE%85%E6%B3%A8%E9%9F%B3%E7%9A%84%E6%96%87%E6%9C%AC";
            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.ContentLength = Encoding.UTF8.GetByteCount(payload);
            webRequest.ContentType = "application/x-www-form-urlencoded";//Content-Type
            webRequest.CookieContainer = cookieContainer;
            webRequest.Method = "post";
            Stream request = webRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(request, Encoding.GetEncoding("gb2312"));
            streamWriter.Write(payload);
            streamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            response.Cookies = cookieContainer.GetCookies(response.ResponseUri);
            Stream returnStream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(returnStream, Encoding.GetEncoding("utf-8"));
            string endResult = streamReader.ReadToEnd();//返回结果
            streamReader.Close();
            returnStream.Close();
            return endResult;
        }
    }
}

微信小程序 代码示例:

wx.request({
      url: 'http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic',
      method: "post",
      data:
      {
        app_key: "8102b22a5e81e840176d9f381ec6f837",
        time_stamp: "1493468759",
        nonce_str: "dd599ef889859f9fe",
        sign: "99880aabb33f4def5c875875b6bdc3b1",
        text: "待注音的文本"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
      },
      success(res) {
        console.log(res.data)
      }
    })

iOS 代码示例:

NSURLSession *session = [NSURLSession sharedSession];
NSString *urlStr = @"http://openapiai.xueersi.com/v1/api/nlp/nlp/phonetic";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *app_key = @"app_key=8102b22a5e81e840176d9f381ec6f837";
NSString *time_stamp = @"time_stamp=1493468759";
NSString *nonce_str = @"nonce_str=dd599ef889859f9fe";
NSString *sign = @"sign=99880aabb33f4def5c875875b6bdc3b1";
NSString *text = @"text=待注音的文本";
NSString *paramsStr = [NSString stringWithFormat:@"%@&%@&%@&%@&%@&%@", app_key, time_stamp, nonce_str, sign, text];
request.HTTPBody = [paramsStr dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"result=%@",dict);
}];
[dataTask resume];

返回响应

返回格式

JSON格式

响应参数说明

参数名 类型 赋值说明 样例 备注
code int 返回码 0  
msg string 返回码解释 "请求成功"  
data obj 识别结果结构 数据结构  

py_info

[]item

所有字符的注音信息

识别结果的数组

 

p

string

每个字符的拼音

dài  
p_n string 每个中文汉字的拼音(不带声调) dai  
s string 每一个式子的批改状态  

run_msg

string

处理状态情况

Success  

响应代码示例

响应Body:

{
    "code": 0,
    "msg": "请求成功",
    "data": {
        "py_info": [
            {
                "p": "dài",
                "p_n": "dai",
                "s": "待"
            },
            {
                "p": "zhù",
                "p_n": "zhu",
                "s": "注"
            },
            {
                "p": "yīn",
                "p_n": "yin",
                "s": "音"
            },
            {
                "p": "de",
                "p_n": "de",
                "s": "的"
            },
            {
                "p": "wén",
                "p_n": "wen",
                "s": "文"
            },
            {
                "p": "běn",
                "p_n": "ben",
                "s": "本"
            }
        ],
        "run_msg": "Success"
    }
}

常见问题及反馈

Back to top