百度AI--自然语言处理之Java开发
参数:
public class APIConstants { //设置APPID/AK/SK public static final String APP_ID = "108***"; public static final String API_KEY = "fWC*****"; public static final String SECRET_KEY = "CP3***"; public static final String TOKEN = "24.7f7d825858b9f66819e48f6ab212a8a7.2592000.1522835019.*********"; }
HttpUtil:
import java.util.List; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; public class HttpUtil { public static String post(String requestUrl, String accessToken, String params) throws Exception { String contentType = "application/x-www-form-urlencoded"; System.err.println("post(String requestUrl, String accessToken, String params)"); return HttpUtil.post(requestUrl, accessToken, contentType, params); } public static String post(String requestUrl, String accessToken, String contentType, String params) throws Exception { String encoding = "UTF-8"; if (requestUrl.contains("nlp")) { encoding = "GBK"; } System.err.println("post(String requestUrl, String accessToken, String contentType, String params)"); return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding); } public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding) throws Exception { String url = requestUrl + "?access_token=" + accessToken; System.err.println("post(String requestUrl, String accessToken, String contentType, String params, String encoding)"); return HttpUtil.postGeneralUrl(url, contentType, params, encoding); } public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding) throws Exception { System.err.println("postGeneralUrl(String generalUrl, String contentType, String params, String encoding)"); URL url = new URL(generalUrl); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 设置通用的请求属性 connection.setRequestProperty("Content-Type", contentType); 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<String, List<String>> headers = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : headers.keySet()) { System.err.println(key + "--->" + headers.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = null; in = new BufferedReader( new InputStreamReader(connection.getInputStream(), encoding)); String result = ""; String getLine; while ((getLine = in.readLine()) != null) { result += getLine; } in.close(); System.err.println("result:" + result); return result; } }
测试:
public class Test { public static void main(String[] args) throws Exception { String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify"; String params = "{\n" + " \"text\": \"苹果是一家伟大的公司\" \n" + "}";
try { HttpUtil httpUtil = new HttpUtil(); String result = httpUtil.post(url, APIConstants.TOKEN, params); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
获取token:
import com.huawei.hst.nps.bdAI.APIConstants;; public class AccessToken { public static void main(String[] args) throws Exception {; String access_token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id="+APIConstants.API_KEY +"&client_secret="+ APIConstants.SECRET_KEY; //HttpResponse response = HttpUtil.post(access_token_url,new HashMap<String, String>(),new HashMap<String, String>()); //System.out.println(EntityUtils.toString(response.getEntity())); } }
资料:
连接1:http://blog.csdn.net/u010651369/article/details/64439090
连接2:http://blog.csdn.net/zmx729618/article/details/78132942
链接3:http://aixiaoshuai.mydoc.io/?t=234826
链接4:http://ai.baidu.com/tech/nlp/sentiment_classify
百度AI--自然语言处理之Java开发的更多相关文章
- 百度AI开放平台 UNIT平台开发在线客服 借助百度的人工智能如何开发一个在线客服系统
这段时间在研究一些人工智能的产品,对比了国内几家做人工智能在线客服的,有些接口是要收费的,有些是免费的,但是做了很多限制,比如每天调用的接口次数限制是100次.后来就找到了百度的AI,大家也知道,目前 ...
- 微信开发+百度AI学习:微信网页开发环境搭建
参考微信官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 两步即可获取微信网页开发能力 STEP1: ...
- c# 利用AForge和百度AI开发实时人脸识别
baiduAIFaceIdentify项目是C#语言,集成百度AI的SDK利用AForge开发的实时人脸识别的小demo,里边包含了人脸检测识别,人脸注册,人脸登录等功能 人脸实时检测识别功能 思路是 ...
- 用百度AI平台接口实现OCR文字识别
目录 一.接入指南 1.注册 2.登录 3.创建应用 二.安装接口模型 三.编写python代码 四.识别结果 一.接入指南 若想利用百度AI开放平台进行软件开发,首先应成为百度AI开放平台的开发者. ...
- java通过百度AI开发平台提取身份证图片中的文字信息
废话不多说,直接上代码... IdCardDemo.java package com.wulss.baidubce; import java.io.BufferedReader; import jav ...
- Java开发、网络爬虫、自然语言处理、数据挖掘简介
一.java开发 (1) 应用开发,即Java SE开发,不属于java的优势所在,所以市场占有率很低,前途也不被看好. (2) web开发,即Java Web开发,主要是基于自有或第三方成熟框架的系 ...
- java开发_模仿百度文库_OpenOffice2PDF_注意事项
在模仿百度文库的操作过程中,有很多朋友反映出来的一些问题,是我想起了写这篇blog. 主要是让大家在做的过程中注意一些东西,否则达不到想要的效果. 第一步:我们先从 java开发_模仿百度文库_Ope ...
- python 全栈开发,Day122(人工智能初识,百度AI)
一.人工智能初识 什么是智能? 我们通常把人成为智慧生物,那么”智慧生物的能力”就是所谓的”智能”我们有什么能力?听,说,看,理解,思考,情感等等 什么是人工智能? 顾名思义就是由人创造的”智慧能力” ...
- 用百度AI的OCR文字识别结合JAVA实现了图片的文字识别功能
第一步可定要获取百度的三个东西 要到百度AI网站(http://ai.baidu.com/)去注册 然后获得 -const APP_ID = '请填写你的appid'; -const API_KEY ...
- 利用百度AI OCR图片识别,Java实现PDF中的图片转换成文字
序言:我们在读一些PDF版书籍的时候,如果PDF中不是图片,做起读书笔记的还好:如果PDF中的是图片的话,根本无法编辑,做起笔记来,还是很痛苦的.我是遇到过了.我们搞技术的,当然得自己学着解决现在的痛 ...
随机推荐
- 如何用命令行刷新,启用,禁用Magento2的缓存
当你使用Magento商店时如何刷新Magento 2中的Cache命令行是基本的常用操作.Magento 2默认有12种缓存类型.在命令行中有5个简单的命令来管理缓存.在这篇文章中,我将逐步向您展示 ...
- linux操作小技巧锦集
0.常用Linux命令 查看端口被占用情况: 1.netstat -tunlp|grep 端口号 2.lsof -i:端口号 tar 压缩文件命令: -c:建立一个压缩文件的参数指令(create 的 ...
- Semaphore实现的生产者消费者程序
Semaphore:Semaphores are often used to restrict the number of threads than can access some (physical ...
- php-kafka
1.环境依赖 The extension support both PHP 5 and PHP 7. The extension requires » librdkafka >= 0.8 for ...
- Centos 6.5 pptpd服务端搭建过程
首先检测有没有启用ppp和tun cat /dev/ppp cat /dev/net/tun 如果显示是这样的 cat: /dev/ppp: No such device or address cat ...
- gas问题out of gas的解决
1.昨天遇见了还是以前遇见的问题,就是发现有些函数就是不能用web3调用,然后怎么弄都写不到数组上,但是今天终于将它解决了web3的学习:https://github.com/ethereum/wik ...
- solidity 汇编语言问题——内存数据mload时为什么从第32位开始
问题:内存数据mload时为什么从第32位开始 代码出处:https://gist.github.com/axic/5b33912c6f61ae6fd96d6c4a47afde6d pragma so ...
- day16 Python map函数
num_l=[1,2,10,5,3,7] #lambda x:x+1 # def add_one(x): # return x+1 #lambda x:x+1 # def reduce_one(x): ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- Redis 参数说明
4. Redis的配置 4.1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 4.2. 当Redis以守护进程方式运行时,Redis ...