返回为字节json,排序
This commit is contained in:
parent
7d58c0dc9d
commit
639a397814
38
ai.go
38
ai.go
|
@ -6,14 +6,28 @@ import (
|
||||||
"gitea.cdlsxd.cn/self-tools/l_ai_excel_header_match/doubao"
|
"gitea.cdlsxd.cn/self-tools/l_ai_excel_header_match/doubao"
|
||||||
"github.com/iancoleman/orderedmap"
|
"github.com/iancoleman/orderedmap"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExcelMatch(ctx context.Context, selfExcelHeader, matchToExcelHeader []string, key string, model string) (resultByte []byte, err error) {
|
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)
|
modelObj := doubao.NewDouBao(model, key)
|
||||||
text := []string{
|
text := []string{
|
||||||
"第一张表表头为key,第二张表表头为value,如果未匹配到则返回空",
|
"第一张表表头为key,第二张表表头为value,如果未匹配到则返回空",
|
||||||
"-第一个表头:[" + strings.Join(matchToExcelHeader, ",") + "]",
|
"-第一个表头:[" + strings.Join(withOutSpecialMatchToExcelHeader, ",") + "]",
|
||||||
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
|
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
|
||||||
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串,不需要其他额外的表述以及格式",
|
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串,不需要其他额外的表述以及格式",
|
||||||
"-输出的json长度必须和第一张表的长度一致",
|
"-输出的json长度必须和第一张表的长度一致",
|
||||||
|
@ -27,18 +41,32 @@ func ExcelMatch(ctx context.Context, selfExcelHeader, matchToExcelHeader []strin
|
||||||
return nil, err
|
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()
|
m := orderedmap.New()
|
||||||
|
|
||||||
for _, key := range desiredOrder {
|
for _, key := range desiredOrder {
|
||||||
if value, exists := data[key]; exists {
|
if value, exists := data[key]; exists {
|
||||||
|
if specialReg, ex := specialChars[key]; ex {
|
||||||
|
key = specialReg
|
||||||
|
}
|
||||||
m.Set(key, value)
|
m.Set(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 编码为 JSON
|
// 编码为 JSON
|
||||||
return json.Marshal(m)
|
return json.Marshal(m)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue