2017年只剩下10天了。 小帅丶 7叔再来一篇NLP的示例代码
示例代码地址:https://gitee.com/xshuai/ai/tree/master/AIDemo/src/main/java/com/xs/nlp
- 官网接口文档:http://ai.baidu.com/docs#/NLP-API/top
- 准备工作 已经创建了语言处理基础技术应用并且拿到apikey sercetkey得到AccessToken
- 接口地址:https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer
***************************提供分词、词性标注、专名识别***************************
- 词法分析接口-Java-API示例代码
参数格式是JSON格式的字符串哦。
package com.xs.nlp;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.xs.common.APIContants;
import com.xs.pojo.nlp.LexerAnalysisBean;
import com.xs.pojo.nlp.LexerAnalysisBean.Items;
import com.xs.util.baidu.HttpUtil;
/**
* 自然语言处理-词法分析
* @author 小帅丶
*
*/
public class LexerAnalysis {
/**
* 词法分析接口地址
*/
public static String LEXERANALYSIS_URL = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer";
public static void main(String[] args) throws Exception {
//输出字符串内容
// String result = getLexerAnalysisResult("百度是一家互联网公司", "自己的token");
// System.out.println(result);
LexerAnalysisBean lexerAnalysisBean = getLexerAnalysisBean("百度是一家互联网公司","自己的token");
List items = lexerAnalysisBean.getItems();
for (int i = 0; i < items.size(); i++) {
//输出Items对象中basic_words内容
System.out.println(items.get(i).getBasic_words());
}
}
/**
* 词法分析接口
* @param text
* @param accessToken
* @return data
* @throws Exception
*/
public static String getLexerAnalysisResult(String text,String accessToken) throws Exception {
String url_param = "?access_token="+accessToken;
String url = LEXERANALYSIS_URL+url_param;
String param = "{\"text\":\""+text+"\"}";
String data = HttpUtil.postNLP(url, param);
return data;
}
/**
* 词法分析接口
* @param text
* @param accessToken
* @return data
* @throws Exception
*/
public static LexerAnalysisBean getLexerAnalysisBean(String text,String accessToken) throws Exception {
String url_param = "?access_token="+accessToken;
String url = LEXERANALYSIS_URL+url_param;
String param = "{\"text\":\""+text+"\"}";
String data = HttpUtil.postNLP(url, param);
LexerAnalysisBean lexerAnalysisBean = JSON.parseObject(data,LexerAnalysisBean.class);
return lexerAnalysisBean;
}
}
- 返回结果内容:
{
"log_id": 2621974962532031000,
"text": "百度是一家互联网公司",
"items": [
{
"loc_details": [ ],
"byte_offset": 0,
"uri": "",
"pos": "",
"ne": "ORG",
"item": "百度",
"basic_words": [
"百度"
],
"byte_length": 4,
"formal": ""
},
{
"loc_details": [ ],
"byte_offset": 4,
"uri": "",
"pos": "v",
"ne": "",
"item": "是",
"basic_words": [
"是"
],
"byte_length": 2,
"formal": ""
},
{
"loc_details": [ ],
"byte_offset": 6,
"uri": "",
"pos": "m",
"ne": "",
"item": "一家",
"basic_words": [
"一",
"家"
],
"byte_length": 4,
"formal": ""
},
{
"loc_details": [ ],
"byte_offset": 10,
"uri": "",
"pos": "n",
"ne": "",
"item": "互联网",
"basic_words": [
"互联",
"网"
],
"byte_length": 6,
"formal": ""
},
{
"loc_details": [ ],
"byte_offset": 16,
"uri": "",
"pos": "n",
"ne": "",
"item": "公司",
"basic_words": [
"公司"
],
"byte_length": 4,
"formal": ""
}
]
}
- 所用到的postNLP方法
需要注意GBK编码。文档要求是GBK哦。不过也支持UTF-8 只需要在接口地址增加charset=UTF-8 注意大小写敏感
/**
* NLP接口HTTP请求方法
* @param requestUrl
* @param params
* @return
* @throws Exception
*/
public static String postNLP(String requestUrl,String params) throws Exception {
String encoding = "";
if(requestUrl.contains("nlp")){
encoding = "GBK";
}else{
encoding = "UTF-8";
}
String generalUrl = requestUrl;
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.out.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
if (requestUrl.contains("nlp"))
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
else
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.out.println("result:" + result);
return result;
}
- 所用到的JavaBean对象
package com.xs.pojo.nlp;
import java.util.List;
/**
* NLP对象
* @author 小帅丶
* 2017年12月20日
*/
public class LexerAnalysisBean {
private long log_id;
private String text;
private List items;
public long getLog_id() {
return log_id;
}
public void setLog_id(long log_id) {
this.log_id = log_id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public static class Items{
private List loc_details;
private int byte_offset;
private String uri;
private String pos;
private String ne;
private String item;
private List basic_words;
private int byte_length;
private String formal;
public List getLoc_details() {
return loc_details;
}
public void setLoc_details(List loc_details) {
this.loc_details = loc_details;
}
public int getByte_offset() {
return byte_offset;
}
public void setByte_offset(int byte_offset) {
this.byte_offset = byte_offset;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getPos() {
return pos;
}
public void setPos(String pos) {
this.pos = pos;
}
public String getNe() {
return ne;
}
public void setNe(String ne) {
this.ne = ne;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public List getBasic_words() {
return basic_words;
}
public void setBasic_words(List basic_words) {
this.basic_words = basic_words;
}
public int getByte_length() {
return byte_length;
}
public void setByte_length(int byte_length) {
this.byte_length = byte_length;
}
public String getFormal() {
return formal;
}
public void setFormal(String formal) {
this.formal = formal;
}
}
}
以上就是示例代码所有内容。
小程序增加了食材识别的功能。您体验了吗?
#2用户已被禁言回复于2017-12
- POSTMAN请求示例截图
1
请登录后评论
TOP
切换版块