l_ai_excel_header_match/ai.go

74 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package l_ai_excel_header_match
import (
"context"
"encoding/json"
"gitea.cdlsxd.cn/self-tools/l_ai_excel_header_match/doubao"
"github.com/iancoleman/orderedmap"
"strings"
"unicode"
)
func ExcelMatch(ctx context.Context, selfExcelHeader, matchToExcelHeader []string, key string, model string) (resultByte []byte, err error) {
var (
result map[string]interface{}
specialChars = make(map[string]string)
withOutSpecialMatchToExcelHeader = make([]string, len(matchToExcelHeader))
)
for k, v := range matchToExcelHeader {
var char = v
char, isChange := cleanString(v)
if isChange {
specialChars[char] = v
}
withOutSpecialMatchToExcelHeader[k] = char
}
modelObj := doubao.NewDouBao(model, key)
text := []string{
"第一张表表头为key第二张表表头为value如果未匹配到则返回空",
"-第一个表头:[" + strings.Join(withOutSpecialMatchToExcelHeader, ",") + "]",
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串不需要其他额外的表述以及格式",
"-输出的json长度必须和第一张表的长度一致",
}
res, err := modelObj.GetData(ctx, doubao.Text, func(input string) (string, error) {
return input, nil
}, text...)
err = json.Unmarshal([]byte(res), &result)
if err != nil {
return nil, err
}
return OrderMapWithSlice(result, withOutSpecialMatchToExcelHeader, specialChars)
}
func cleanString(s string) (out string, isChange bool) {
var result []rune
for _, ch := range s {
if (unicode.IsLetter(ch) || unicode.IsDigit(ch) || unicode.IsSpace(ch) || unicode.IsPunct(ch)) && (ch != '*') {
result = append(result, ch)
} else {
isChange = true
}
}
return string(result), isChange
}
func OrderMapWithSlice(data map[string]interface{}, desiredOrder []string, specialChars map[string]string) ([]byte, error) {
m := orderedmap.New()
for _, key := range desiredOrder {
if value, exists := data[key]; exists {
if specialReg, ex := specialChars[key]; ex {
key = specialReg
}
m.Set(key, value)
}
}
// 编码为 JSON
return json.Marshal(m)
}