由于阿里云服务SDK方式调用只提供了JAVA和Python,用go就只能使用普通HTTP请求调用
阿里云NLP基础服务
API概览
获取AccessKey
中文分词(基础版)API
公共参数每个接口都需要使用的请求参数(公共参数概览);公共参数大多参数都给了对应的取值.唯一一个Signature(签名结果串)比较复杂(签名机制),其他的参数全部都拼接到地址后面就可以了
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/satori/go.uuid"
"io/ioutil"
"net/http"
url2 "net/url"
"time"
)
func main() {
nlp("今天,股市沸腾!发生了啥?")
}
var AccessKeyID = ""
var AccessKeySecret = ""
func nlp(text string) {
if len(text) > 1024 {
panic("text Max Length 1024")
}
duration, err := time.ParseDuration("-8h")
if err != nil {
panic(err)
}
timeStamp := time.Now().Add(duration).Format("2006-01-02T15:04:05Z")
signatureNonce := uuid.NewV4().String()
params := url2.Values{}
params.Add("AccessKeyId", AccessKeyID)
params.Add("Action", "GetWsChGeneral")
params.Add("Format", "JSON")
params.Add("ServiceCode", "alinlp")
params.Add("SignatureMethod", "HMAC-SHA1")
params.Add("SignatureNonce", signatureNonce)
params.Add("SignatureVersion", "1.0")
params.Add("Text", text)
params.Add("Timestamp", timeStamp)
params.Add("TokenizerId", "GENERAL_CHN")
params.Add("Version", "2020-06-29")
// 0:大粒度,1:中粒度(默认),2:小粒度
params.Add("OutType", "1")
// 将所有请求参数进行URL编码
stringToSign := url2.QueryEscape(params.Encode())
// 构造成待签名的字符串
stringToSign = http.MethodGet + "&" + url2.QueryEscape("/") + "&" + stringToSign
secret := AccessKeySecret + "&"
// 计算签名值
hash := hmac.New(sha1.New, []byte(secret))
hash.Write([]byte(stringToSign))
signature := base64.StdEncoding.EncodeToString(hash.Sum(nil))
params.Add("Signature", signature)
// 请求地址
url := "https://alinlp.cn-hangzhou.aliyuncs.com/"
uri, err := url2.ParseRequestURI(url)
if err != nil {
panic(err)
}
uri.RawQuery = params.Encode()
//发送请求
url = uri.String()
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var result RequestResult
err = json.Unmarshal(bytes, &result)
if err != nil {
panic(err)
}
var data RequestResultData
err = json.Unmarshal([]byte(result.Data), &data)
if err != nil {
panic(err)
}
bytes, err = json.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
type RequestResult struct {
RequestId string `json:"RequestId"`
Data string `json:"Data"`
}
type RequestResultData struct {
Result []DataResult `json:"result"`
Success bool `json:"success"`
}
type DataResult struct {
ID string `json:"id"`
Word string `json:"word"`
Tags []string `json:"tags"`
}
{
"result": [
{
"id": "0",
"word": "今天",
"tags": [
"基本词-中文",
"机构-机构特指",
"文体娱乐类-报纸杂志类",
"文体娱乐类-戏剧歌曲类",
"文体娱乐类-影视类",
"文体娱乐类-书文课程类"
]
},
{
"id": "1",
"word": ",",
"tags": []
},
{
"id": "2",
"word": "股市",
"tags": [
"基本词-中文",
"产品类型修饰词"
]
},
{
"id": "3",
"word": "沸腾",
"tags": [
"基本词-中文",
"产品-品牌"
]
},
{
"id": "4",
"word": "!",
"tags": []
},
{
"id": "5",
"word": "发生",
"tags": [
"基本词-中文"
]
},
{
"id": "6",
"word": "了",
"tags": [
"基本词-中文",
"文体娱乐类-flash作品"
]
},
{
"id": "7",
"word": "啥",
"tags": [
"基本词-中文"
]
},
{
"id": "8",
"word": "?",
"tags": []
}
],
"success": true
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)