完成拼音与汉字之间的互相转换,同时支持声母、韵母、音调、音标和输入法首字母首声母功能。





POST
| 参数名称 | 参数说明 | 请求类型 | 是否必须 | 数据类型 |
|---|---|---|---|---|
| text | 需要转换的文本 | Body:form-data | true | string |
| token | 使用API时系统需要验证的身份信息 | Headers | true | string |
| 参数名称 | 参数示例 |
|---|---|
| 参数为json格式,json 中包含除token外所有参数 | {"text":"重载不是重任"} |
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.13</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.11</version> </dependency> </dependencies>
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 汉字转拼音范例
*/
public class test {
public static void main(String[] args) {
//请求头中的token 需要进行替换成在官网申请的新token
String token="TOKEN请在官网自行获取";
//申请的接口地址
String url="http://comdo.hanlp.com/hanlp/v1/pinyin/toPinyin";
//处理分析的文本,作为params参数传入
Map<String,Object> params=new HashMap<String,Object>();
//参数传入要处理分析的文本
params.put("text", "重载不是重任");
//执行HanLP接口,result为返回的结果
String result=doHanlpApi(token,url,params);
//打印输出结果
System.out.println(result);
}
public static String doHanlpApi(String token,String url,Map<String,Object> params) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
//添加header请求头,token请放在header里
httpPost.setHeader("token", token);
// 创建参数列表
List<NameValuePair> paramList = new ArrayList<>();
if (params != null) {
for (String key : params.keySet()) {
//所有参数依次放在paramList中
paramList.add(new BasicNameValuePair(key, (String) params.get(key)));
}
//模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
return resultString;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(response!=null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
{
"code":0,
"data":[
{
"head":"ch",
"pinyinWithToneMark":"chóng",
"pinyinWithoutTone":"chong",
"shengmu":"ch",
"tone":2,
"yunmu":"ong"
},
{
"head":"z",
"pinyinWithToneMark":"zǎi",
"pinyinWithoutTone":"zai",
"shengmu":"z",
"tone":3,
"yunmu":"ai"
},
{
"head":"b",
"pinyinWithToneMark":"bú",
"pinyinWithoutTone":"bu",
"shengmu":"b",
"tone":2,
"yunmu":"u"
},
{
"head":"sh",
"pinyinWithToneMark":"shì",
"pinyinWithoutTone":"shi",
"shengmu":"sh",
"tone":4,
"yunmu":"i"
},
{
"head":"zh",
"pinyinWithToneMark":"zhòng",
"pinyinWithoutTone":"zhong",
"shengmu":"zh",
"tone":4,
"yunmu":"ong"
},
{
"head":"r",
"pinyinWithToneMark":"rèn",
"pinyinWithoutTone":"ren",
"shengmu":"r",
"tone":4,
"yunmu":"en"
}
]
}