返回为字节json,排序

This commit is contained in:
renzhiyuan 2025-07-24 18:18:20 +08:00
parent 7d58c0dc9d
commit 639a397814
1 changed files with 33 additions and 5 deletions

38
ai.go
View File

@ -6,14 +6,28 @@ import (
"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{}
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[v] = char
}
withOutSpecialMatchToExcelHeader[k] = char
}
modelObj := doubao.NewDouBao(model, key)
text := []string{
"第一张表表头为key第二张表表头为value如果未匹配到则返回空",
"-第一个表头:[" + strings.Join(matchToExcelHeader, ",") + "]",
"-第一个表头:[" + strings.Join(withOutSpecialMatchToExcelHeader, ",") + "]",
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串不需要其他额外的表述以及格式",
"-输出的json长度必须和第一张表的长度一致",
@ -27,18 +41,32 @@ func ExcelMatch(ctx context.Context, selfExcelHeader, matchToExcelHeader []strin
return nil, err
}
return OrderMapWithSlice(result, matchToExcelHeader)
return OrderMapWithSlice(result, withOutSpecialMatchToExcelHeader, specialChars)
}
func OrderMapWithSlice(data map[string]interface{}, desiredOrder []string) ([]byte, error) {
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) {
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)