From 73e69b2721bed7a74272222a7a15eb77516a828f Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Thu, 10 Sep 2020 18:28:30 +0800 Subject: [PATCH] close task#7786 --- go.mod | 1 + src/model/definition.go | 16 ++++ test/article/common.go | 2 +- test/article/convert_test.go | 14 ++-- test/article/generate_test.go | 137 +++++++++++++++++++++++++++++++++- tmp/cache/.words.db | Bin 1118208 -> 1118208 bytes 6 files changed, 161 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 6544044a..b2e57288 100644 --- a/go.mod +++ b/go.mod @@ -24,5 +24,6 @@ require ( golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect golang.org/x/sys v0.0.0-20200819171115-d785dc25833f // indirect gopkg.in/ini.v1 v1.60.0 + gopkg.in/yaml.v2 v2.2.8 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) diff --git a/src/model/definition.go b/src/model/definition.go index 76ddc87d..fa92e073 100644 --- a/src/model/definition.go +++ b/src/model/definition.go @@ -107,4 +107,20 @@ func (def *DefSimple) Init(tableName, author, desc, version string) { } func (fld *FieldSimple) Init(field string) { fld.Field = field +} + + +type DefExport struct { + ClsBase `yaml:",inline"` + XFields []DefFieldExport `yaml:"xfields,flow"` // control orders +} +type DefFieldExport struct { + Field string `yaml:"field"` + Prefix string `yaml:"prefix"` + Postfix string `yaml:"postfix"` + + Select string `yaml:"select"` + Where string `yaml:"where"` + Rand bool `yaml:"rand"` + Limit int `yaml:"limit"` } \ No newline at end of file diff --git a/test/article/common.go b/test/article/common.go index cab80edb..fd07a168 100644 --- a/test/article/common.go +++ b/test/article/common.go @@ -36,7 +36,7 @@ func getFilesInDir(folder, ext string, files *[]string) { filePath := fileUtils.AddSepIfNeeded(folder) + name if fi.IsDir() { getFilesInDir(filePath, ext, files) - } else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" { + } else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ext { *files = append(*files, filePath) } } diff --git a/test/article/convert_test.go b/test/article/convert_test.go index 6209901b..7f82f9dd 100644 --- a/test/article/convert_test.go +++ b/test/article/convert_test.go @@ -6,8 +6,8 @@ import ( "github.com/360EntSecGroup-Skylar/excelize/v2" constant "github.com/easysoft/zendata/src/utils/const" i118Utils "github.com/easysoft/zendata/src/utils/i118" - logUtils "github.com/easysoft/zendata/src/utils/log" _ "github.com/mattn/go-sqlite3" + "log" "strconv" "strings" "testing" @@ -15,7 +15,7 @@ import ( func TestImportSqlite(t *testing.T) { files := make([]string, 0) - getFilesInDir("xdoc/words-9.3", "xlsx", &files) + getFilesInDir("xdoc/words-9.3", ".xlsx", &files) tableName := "words" seq := 1 @@ -33,7 +33,7 @@ func TestImportSqlite(t *testing.T) { dropSql := `DROP TABLE IF EXISTS ` + tableName + `;` _, err = db.Exec(dropSql) if err != nil { - logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_drop_table", tableName, err.Error())) + log.Println(i118Utils.I118Prt.Sprintf("fail_to_drop_table", tableName, err.Error())) return } @@ -44,14 +44,14 @@ func TestImportSqlite(t *testing.T) { ddlSql := fmt.Sprintf(ddlTemplate, strings.Join(ddlFields, ", \n")) _, err = db.Exec(ddlSql) if err != nil { - logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_create_table", tableName, err.Error())) + log.Println(i118Utils.I118Prt.Sprintf("fail_to_create_table", tableName, err.Error())) return } sql := strings.Join(insertSqls, "\n") _, err = db.Exec(sql) if err != nil { - logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_exec_query", sql, err.Error())) + log.Println(i118Utils.I118Prt.Sprintf("fail_to_exec_query", sql, err.Error())) return } } @@ -59,7 +59,7 @@ func TestImportSqlite(t *testing.T) { func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]string, colMap *map[string]bool) { excel, err := excelize.OpenFile(filePath) if err != nil { - logUtils.PrintTo("fail to read file " + filePath + ", error: " + err.Error()) + log.Println("fail to read file " + filePath + ", error: " + err.Error()) return } @@ -90,7 +90,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] colName = "ci" } if colName != "ci" { - colName = colPrefix + ":" + colName + colName = colPrefix + "-" + colName } if (*colMap)[colName] == false { diff --git a/test/article/generate_test.go b/test/article/generate_test.go index dc125a9d..bebcca90 100644 --- a/test/article/generate_test.go +++ b/test/article/generate_test.go @@ -1,12 +1,25 @@ package main import ( + "github.com/easysoft/zendata/src/model" + constant "github.com/easysoft/zendata/src/utils/const" fileUtils "github.com/easysoft/zendata/src/utils/file" _ "github.com/mattn/go-sqlite3" + "gopkg.in/yaml.v3" + "strconv" + "strings" "testing" ) -func TestGenerate(t *testing.T) { +const ( + strLeft = "“" + strRight = "”" + + expLeft = "(" + expRight = ")" +) + +func TestGenerate(ts *testing.T) { files := make([]string, 0) getFilesInDir("xdoc/words-9.3", ".txt", &files) @@ -20,7 +33,129 @@ func TestGenerate(t *testing.T) { } func convertToYaml(article string) (content string) { + sections := parseSections(article) + + conf := createDef(constant.ConfigTypeArticle, "words.v1") + + prefix := "" + for index, section := range sections { + tye := section["type"] + val := section["val"] + if tye == "exp" { + field := createField(index, prefix, val) + conf.XFields = append(conf.XFields, field) + prefix = "" + } else { + prefix += val + } + } + + bytes, _ := yaml.Marshal(&conf) + content = string(bytes) + + // convert yaml format by using a map + m := make(map[string]interface{}) + yaml.Unmarshal([]byte(content), &m) + bytes, _ = yaml.Marshal(&m) + content = string(bytes) + content = strings.Replace(content, "xfields", "\nfields", -1) + + return +} + +func createDef(typ, table string) (conf model.DefExport) { + conf.Title = "automation" + conf.Author = "zendata" + conf.From = table + conf.Type = typ + conf.Desc = "generated from article text automatically" + + return +} + +func createField(index int, prefix, exp string) (field model.DefFieldExport) { + field.Field = strconv.Itoa(index) + field.Prefix = prefix + + field.Select = getPinyin(exp) + field.Where = "true" + field.Rand = true + field.Limit = 1 + + return +} + +func parseSections(content string) (sections []map[string]string) { + strStart := false + expStart := false + + content = strings.TrimSpace(content) + runeArr := []rune(content) + + section := "" + for i := 0; i < len(runeArr); i++ { + item := runeArr[i] + str := string(item) + + isCouple, duplicateStr := isCouple(i, runeArr) + if isCouple { + section += duplicateStr + i += 1 + } else if strStart && str == strRight { // str close + addSection(section, "str", §ions) + + strStart = false + section = "" + } else if expStart && str == expRight { // exp close + addSection(section, "exp", §ions) + + expStart = false + section = "" + } else if !strStart && !expStart && str == strLeft { // str start + if section != "" && strings.TrimSpace(section) != "+" { + addSection(section, "str", §ions) + } + + strStart = true + section = "" + } else if !strStart && !expStart && str == expLeft { // exp start + if section != "" && strings.TrimSpace(section) != "+" { + addSection(section, "str", §ions) + } + + expStart = true + section = "" + } else { + section += str + } + } + + return +} + +func addSection(str, typ string, arr *[]map[string]string) { + mp := map[string]string{} + mp["type"] = typ + mp["val"] = str + + *arr = append(*arr, mp) +} + +func isCouple(i int, arr []rune) (isCouple bool, duplicateStr string) { + if string(arr[i]) == strLeft && (i + 1 < len(arr) && string(arr[i + 1]) == strLeft) { + isCouple = true + duplicateStr = string(arr[i]) + } else if string(arr[i]) == strRight && (i + 1 < len(arr) && string(arr[i + 1]) == strRight) { + isCouple = true + duplicateStr = string(arr[i]) + } else if string(arr[i]) == expLeft && (i + 1 < len(arr) && string(arr[i + 1]) == expLeft) { + isCouple = true + duplicateStr = string(arr[i]) + } else if string(arr[i]) == expRight && (i + 1 < len(arr) && string(arr[i + 1]) == expRight) { + isCouple = true + duplicateStr = string(arr[i]) + } return } \ No newline at end of file diff --git a/tmp/cache/.words.db b/tmp/cache/.words.db index 8b36fb239e78b6ee345a5908e90e2c2f1cf15b23..c967077fdca15a94e9b1ca9f28dc253e1dfde0e0 100644 GIT binary patch delta 5490 zcmZ9Q33yc1702f#civ1U%M41{A%pCYkwH;#!--<03L1Cn0uv#?KncSVR>MpnWDl9- zC3y)UWI~|nrj62&+OOKNwYF|`?Cw6bU-znG_qx#j&KqL=%J=>LcR%;s^X@y$OEkVb z8eblpJmx){O&epg***s@Fj|W))!u6jKCk(VB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