java调用阿里云NLP自然语言处理模型
生活随笔
收集整理的這篇文章主要介紹了
java调用阿里云NLP自然语言处理模型
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
由于SDK調(diào)用需要收費(fèi),所以采用API調(diào)用,每天有50萬次免費(fèi)額度,直接復(fù)制就能用
一.開通賬號服務(wù)
1.百度搜索阿里巴巴自然語言處理開放平臺
2.登錄后頁面
?
3.獲取AccessKey和Secret,()找到右上角頭像后點(diǎn)擊AccessKey管理)
4.自行開通需要的版本
二.代碼部分
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import sun.misc.BASE64Encoder; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.net.URI; import java.net.URLEncoder; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*;public class TestCloud {static String key = "xxx"; // 你自己賬號對應(yīng)的AccessKeystatic String secret = "xxx"; // 你自己賬號對應(yīng)的secretstatic String url = "http://alinlp.cn-hangzhou.aliyuncs.com/?";public static void main(String[] args) throws Exception {//根據(jù)自己的region更換String sortStr = buildSortedUrl(null);String sign = getSign(sortStr);String targetUrl = url + sortStr + "&" + "Signature=" + sign ;// 創(chuàng)建Httpclient對象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;URIBuilder builder = new URIBuilder(targetUrl);URI uri = builder.build();// 創(chuàng)建http GET請求HttpGet httpGet = new HttpGet(uri);// 執(zhí)行請求response = httpclient.execute(httpGet);// 判斷返回狀態(tài)是否為200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");} // JSONObject jsonObject = restTemplate.getForObject(URLDecoder.decode(targetUrl), JSONObject.class);System.out.println(resultString);}public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {String MAC_NAME = "HmacSHA1";String ENCODING = "UTF-8";byte[] data=encryptKey.getBytes(ENCODING);//根據(jù)給定的字節(jié)數(shù)組構(gòu)造一個密鑰,第二參數(shù)指定一個密鑰算法的名稱SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);//生成一個指定 Mac 算法 的 Mac 對象Mac mac = Mac.getInstance(MAC_NAME);//用給定密鑰初始化 Mac 對象mac.init(secretKey);byte[] text = encryptText.getBytes(ENCODING);//完成 Mac 操作return mac.doFinal(text);}public static String encryptBASE64(byte[] key) {return (new BASE64Encoder()).encodeBuffer(key);}public static String getISO8601Timestamp(Date date){TimeZone tz = TimeZone.getTimeZone("UTC");DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");df.setTimeZone(tz);String nowAsISO = df.format(date);return nowAsISO;}public static String getSign(String sortUrl) throws Exception {String StringToSign = "GET&%2F&" + java.net.URLEncoder.encode(sortUrl);byte[] rlt = HmacSHA1Encrypt(StringToSign, secret + "&");String sig = encryptBASE64(rlt);sig = sig.replace("\r", "").replace("\n", "");sig = URLEncoder.encode(sig);return sig;}public static String buildSortedUrl(String nextPageToken){Map<String, String> map = new HashMap<>();map.put("Format","json");map.put("Version","2020-06-29");map.put("AccessKeyId",key);map.put("SignatureMethod","HMAC-SHA1");map.put("Timestamp", getISO8601Timestamp(new Date()));map.put("SignatureVersion", "1.0");map.put("SignatureNonce", UUID.randomUUID().toString().replace("-",""));map.put("Text", "深圳洗剪吹");map.put("TokenizerId", "GENERAL_CHN");/*** xxxx替換為你想要的處理方式* 中文分詞: GetWsChGeneral* 詞性標(biāo)注: GetPosChGeneral* 命名實(shí)體類別: GetNerChEcom* 情感分析: GetSaChGeneral* 關(guān)鍵詞提取: GetKeywordChEcom* 文本分類: GetTcChGeneral*/map.put("Action", "xxxx");map.put("ServiceCode", "alinlp");List<String> sortList = new ArrayList<>();for (String key : map.keySet()) {sortList.add(key);}sortList.sort(Comparator.naturalOrder());String sortStr = null;for(String key : sortList){String temp = URLEncoder.encode(key) + "=" + URLEncoder.encode(map.get(key));if(sortStr == null){sortStr = temp;}else{sortStr += "&" + temp;}}sortStr.replace("+","%20").replace("*","%2A").replace("%7E","~");return sortStr;}}總結(jié)
以上是生活随笔為你收集整理的java调用阿里云NLP自然语言处理模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java单元测试框架与实践(Junit5
- 下一篇: cemu有linux版本吗,cemu更新