78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
package l_ai_category
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/self-tools/l_request"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func GetCategoryWithSelf(ctx context.Context, goodsName string, address string) (cateId int, confidence float64, err error) {
|
|
cateId, name, confidence, err := getCategoryWithAi(ctx, goodsName, address)
|
|
fmt.Printf("商品:%s,所属分类:%s,置信度:%f\n", goodsName, name, confidence)
|
|
return
|
|
}
|
|
|
|
type CatRes struct {
|
|
Predictions []struct {
|
|
Category string `json:"category"`
|
|
Confidence float64 `json:"confidence"`
|
|
} `json:"predictions"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func getCategoryWithAi(ctx context.Context, goodsName string, address string) (cateId int, cateName string, confidence float64, err error) {
|
|
request := map[string]interface{}{
|
|
"product_names": goodsName,
|
|
}
|
|
req := l_request.Request{
|
|
Method: "POST",
|
|
Url: address,
|
|
Json: request,
|
|
}
|
|
res, err := req.Send()
|
|
if err != nil {
|
|
return
|
|
}
|
|
var resMap CatRes
|
|
err = json.Unmarshal(res.Content, &resMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
cateInfo := strings.Split(resMap.Predictions[0].Category, "_")
|
|
if len(cateInfo) != 2 {
|
|
cateName = ""
|
|
cateId = 0.000000
|
|
} else {
|
|
cateName = cateInfo[0]
|
|
cateId, err = strconv.Atoi(cateInfo[1])
|
|
if err != nil {
|
|
}
|
|
}
|
|
|
|
return cateId, cateName, resMap.Predictions[0].Confidence, nil
|
|
}
|
|
|
|
type Category struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
Level int `json:"level"`
|
|
Pid int `json:"pid"`
|
|
Pids map[int]int `json:"pid"`
|
|
}
|
|
|
|
type CategoryDic struct {
|
|
Category
|
|
}
|
|
type QuesStruct struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type Req struct {
|
|
Text string `json:"text"`
|
|
Cate []string `json:"cate"`
|
|
}
|