diff --git a/test/article/common.go b/test/article/common.go new file mode 100644 index 0000000000000000000000000000000000000000..cab80edb9249322ba68b3b490e9ab601a131204c --- /dev/null +++ b/test/article/common.go @@ -0,0 +1,63 @@ +package main + +import ( + "github.com/Chain-Zhang/pinyin" + commonUtils "github.com/easysoft/zendata/src/utils/common" + fileUtils "github.com/easysoft/zendata/src/utils/file" + _ "github.com/mattn/go-sqlite3" + "io/ioutil" + "path" + "path/filepath" + "strings" +) + +func getFilesInDir(folder, ext string, files *[]string) { + folder, _ = filepath.Abs(folder) + + if !fileUtils.IsDir(folder) { + if path.Ext(folder) == ext { + *files = append(*files, folder) + } + + return + } + + dir, err := ioutil.ReadDir(folder) + if err != nil { + return + } + + for _, fi := range dir { + name := fi.Name() + if commonUtils.IngoreFile(name) { + continue + } + + filePath := fileUtils.AddSepIfNeeded(folder) + name + if fi.IsDir() { + getFilesInDir(filePath, ext, files) + } else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" { + *files = append(*files, filePath) + } + } +} + +func getFileName(filePath string) string { + fileName := path.Base(filePath) + fileName = strings.TrimSuffix(fileName, path.Ext(filePath)) + + return fileName +} + +func changeFileExt(filePath, ext string) string { + ret := strings.TrimSuffix(filePath, path.Ext(filePath)) + ret += ext + + return ret +} + +func getPinyin(word string) string { + p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert() + + return p +} \ No newline at end of file diff --git a/test/article/convert_test.go b/test/article/convert_test.go index 2ac1b699898a5658f8527d51d1f58e8bc2cfb839..6209901bac6e2130e3ddf76152f2fa9cfa44cca7 100644 --- a/test/article/convert_test.go +++ b/test/article/convert_test.go @@ -4,16 +4,10 @@ import ( "database/sql" "fmt" "github.com/360EntSecGroup-Skylar/excelize/v2" - "github.com/Chain-Zhang/pinyin" - commonUtils "github.com/easysoft/zendata/src/utils/common" constant "github.com/easysoft/zendata/src/utils/const" - fileUtils "github.com/easysoft/zendata/src/utils/file" i118Utils "github.com/easysoft/zendata/src/utils/i118" logUtils "github.com/easysoft/zendata/src/utils/log" _ "github.com/mattn/go-sqlite3" - "io/ioutil" - "path" - "path/filepath" "strconv" "strings" "testing" @@ -21,7 +15,7 @@ import ( func TestImportSqlite(t *testing.T) { files := make([]string, 0) - getExcelFilesInDir("xdoc/words-9.3", &files) + getFilesInDir("xdoc/words-9.3", "xlsx", &files) tableName := "words" seq := 1 @@ -69,8 +63,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] return } - fileName := path.Base(filePath) - fileName = strings.TrimSuffix(fileName, path.Ext(filePath)) + fileName := getFileName(filePath) fileName = strings.TrimSuffix(fileName, "词库") colPrefix := getPinyin(fileName) @@ -95,7 +88,8 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] colName := getPinyin(val) if colIndex == 0 && colName != "ci" { colName = "ci" - } else { + } + if colName != "ci" { colName = colPrefix + ":" + colName } @@ -145,41 +139,4 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] ) *insertSqls = append(*insertSqls, insertSql) } -} - -func getExcelFilesInDir(folder string, files *[]string) { - folder, _ = filepath.Abs(folder) - - if !fileUtils.IsDir(folder) { - if path.Ext(folder) == ".xlsx" { - *files = append(*files, folder) - } - - return - } - - dir, err := ioutil.ReadDir(folder) - if err != nil { - return - } - - for _, fi := range dir { - name := fi.Name() - if commonUtils.IngoreFile(name) { - continue - } - - filePath := fileUtils.AddSepIfNeeded(folder) + name - if fi.IsDir() { - getExcelFilesInDir(filePath, files) - } else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" { - *files = append(*files, filePath) - } - } -} - -func getPinyin(word string) string { - p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert() - - return p } \ No newline at end of file diff --git a/test/article/generate_test.go b/test/article/generate_test.go new file mode 100644 index 0000000000000000000000000000000000000000..dc125a9d63f4b5483890bc02a4dbf8cbf7cdd776 --- /dev/null +++ b/test/article/generate_test.go @@ -0,0 +1,26 @@ +package main + +import ( + fileUtils "github.com/easysoft/zendata/src/utils/file" + _ "github.com/mattn/go-sqlite3" + "testing" +) + +func TestGenerate(t *testing.T) { + files := make([]string, 0) + getFilesInDir("xdoc/words-9.3", ".txt", &files) + + for _, filePath := range files { + article := fileUtils.ReadFile(filePath) + content := convertToYaml(article) + + newPath := changeFileExt(filePath, ".yaml") + fileUtils.WriteFile(newPath, content) + } +} + +func convertToYaml(article string) (content string) { + + + return +} \ No newline at end of file diff --git a/tmp/cache/.words.db b/tmp/cache/.words.db index 05c385c8f6dc6f3449d17edc9196bd13d6b81174..8b36fb239e78b6ee345a5908e90e2c2f1cf15b23 100644 Binary files a/tmp/cache/.words.db and b/tmp/cache/.words.db differ