一、最近在做一个医院管理项目的时候,发现医院对错别字这块的质量要求很高,作为一个理工科出身的技术,我对汉字差错也是头大,后来在百度上找到一款神奇的产品:讯飞智检,体验下来节省了我不少的成本与时间,我还让他们的技术人员帮我开发了网页审核的功能,真的是帮了我大忙了。
二、我一共审查了将近500多个页面,对里面的医生职称、错误表达、一些互联网违禁的内容做了全面的审查与纠正,最终项目方也是比较满意我的交付。这么好的纠错方式分享给各位开发者知道下讯飞智检https://zj.xfyun.cn/exam/text?ch=xfzjlttg
三、这个工具产品支持文字审核、Word文档审核、图片审核、音视频审核,可以说审核的维度还是非常齐全的,我主要是用了这款工具的在线URL审核功能,觉得效果很好,而且支持个性化词库定义,所以分享给各位开发者同仁~
四、同时附上他们纠错能力的调用代码,请各位参考
package com.iflytek;
import com.google.gson.Gson;
import okhttp3.HttpUrl;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 文本纠错 WebAPI 接口调用示例
* 运行前:请先填写Appid、APIKey、APISecret以及图片路径
* 运行方法:直接运行 main() 即可
* 结果: 控制台输出结果信息
* 接口文档(必看):https://www.xfyun.cn/doc/nlp/textCorrection/API.html
* uid与res_id可以到ResIdGet上传和获取
*
* @author iflytek
*/
public class WebTextCorrection {
// 地址与鉴权信息
public static final String hostUrl = "https://api.xf-yun.com/v1/private/s9a87e3ec";
public static final String appid = "";
public static final String apiSecret = "";
public static final String apiKey = "";
// 待纠错文本
public static final String text = "鸡犬不能";
// json
public static final Gson gson = new Gson();
// 主函数
public static void main(String[] args) throws Exception {
String url = getAuthUrl(hostUrl, apiKey, apiSecret);
String json = getRequestJson();
String backResult = doPostJson(url, json);
System.out.println("文本纠错返回结果:" + backResult);
JsonParse jsonParse = gson.fromJson(backResult, JsonParse.class);
String base64Decode = new String(Base64.getDecoder().decode(jsonParse.payload.result.text), StandardCharsets.UTF_8);
System.out.println("text字段base64解码后纠错信息:" + base64Decode);
}
// 请求参数json拼接
public static String getRequestJson() {
return "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"uid\": \"uid_wdfgdzx_001\",\n" +
" \"status\": 3\n" +
" },\n" +
" \"parameter\": {\n" +
" \"s9a87e3ec\": {\n" +
" \"res_id\": \"wdfgdzx\",\n" +
" \"result\": {\n" +
" \"encoding\": \"utf8\",\n" +
" \"compress\": \"raw\",\n" +
" \"format\": \"json\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"payload\": {\n" +
" \"input\": {\n" +
" \"encoding\": \"utf8\",\n" +
" \"compress\": \"raw\",\n" +
" \"format\": \"plain\",\n" +
" \"status\": 3,\n" +
" \"text\": \"" + getBase64TextData(text) + "\"\n" +
" }\n" +
" }\n" +
"}";
}
// 读取文件
public static String getBase64TextData(String text) {
return Base64.getEncoder().encodeToString(text.getBytes());
}
// 根据json和url发起post请求
public static String doPostJson(String url, String json) {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
CloseableHttpResponse closeableHttpResponse = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
closeableHttpResponse = closeableHttpClient.execute(httpPost);
resultString = EntityUtils.toString(closeableHttpResponse.getEntity(), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (closeableHttpResponse != null) {
closeableHttpResponse.close();
}
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return resultString;
}
// 鉴权方法
public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
URL url = new URL(hostUrl);
// 时间
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = format.format(new Date());
// 拼接
String preStr = "host: " + url.getHost() + "\n" +
"date: " + date + "\n" +
"POST " + url.getPath() + " HTTP/1.1";
//System.out.println(preStr);
// SHA256加密
Mac mac = Mac.getInstance("hmacsha256");
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "hmacsha256");
mac.init(spec);
byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
// Base64加密
String sha = Base64.getEncoder().encodeToString(hexDigits);
// 拼接
String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);
// 拼接地址
HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().//
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).//
addQueryParameter("date", date).//
addQueryParameter("host", url.getHost()).//
build();
return httpUrl.toString();
}
//返回的json结果拆解
static class JsonParse {
Payload payload;
}
static class Payload {
Result result;
}
static class Result {
String text;
}
}