提交 5dbb81ea 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

generate yaml and article

上级 a7e6f725
难忘的秋游 难忘的秋游
段落一:“秋天的天气”+(形容词-天气-秋季-好)、(形容词-天气-秋季-好),(形容词-外貌-女性)+(名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),“因为”+(名词-称呼=F)+(动词-及物_名词-意愿)+“带她去”+(名词-地点-名山)+(动词-及物_名词-动作-眼部-中性)+(形容词-颜色-植物-树木)+(名词-植物-树木)+(连词-并列关系)+(形容词-颜色-植物-花卉)+(名词-植物-花卉)。(S:形容词-外貌-女性)+(S:名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),(副词-形容词做副词-情绪-快乐)+(动词-及物_名词-动作-手臂-亲切)+(形容词-性格-积极)+(S:名词-称呼=F)。 段落一:“秋天的天气”+(形容词-天气-秋季-好)、(形容词-天气-秋季-好),(形容词-外貌-女性)+(名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),“因为”+(名词-称呼=F)+(动词-及物_名词-意愿)+“带她去”+(名词-地点-名山)+(动词-及物_名词-动作-眼部-中性)+(形容词-颜色-植物-树木)+(名词-植物-树木)+(连词-并列关系)+(形容词-颜色-植物-花卉)+(名词-植物-花卉)。(S:形容词-外貌-女性)+(S:名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),(副词-形容词做副词-情绪-快乐)+(动词-及物_名词-动作-手臂-亲切)+(形容词-性格-积极)+(S:名词-称呼=F)+ “结尾。”
\ No newline at end of file \ No newline at end of file
...@@ -59,15 +59,11 @@ func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) mod ...@@ -59,15 +59,11 @@ func LoadConfigDef(defaultFile, configFile string, fieldsToExport *[]string) mod
mergerDefine(&defaultDef, &configDef, fieldsToExport) mergerDefine(&defaultDef, &configDef, fieldsToExport)
orderFields(&defaultDef, *fieldsToExport) orderFields(&defaultDef, *fieldsToExport)
for index, field := range defaultDef.Fields { for _, field := range defaultDef.Fields {
if vari.Trim { if vari.Trim {
field.Prefix = "" field.Prefix = ""
field.Postfix = "" field.Postfix = ""
} }
if defaultDef.Type == constant.ConfigTypeArticle {
defaultDef.Fields[index].Select = strings.Replace(field.Select, "-", "_", -1)
}
} }
return defaultDef return defaultDef
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
fileUtils "github.com/easysoft/zendata/src/utils/file" fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118" i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log" logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari" "github.com/easysoft/zendata/src/utils/vari"
"log" "log"
"os" "os"
...@@ -168,7 +167,7 @@ func ConvertSingleExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet ...@@ -168,7 +167,7 @@ func ConvertSingleExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet
func ConvertWordExcelsToSQLiteIfNeeded(tableName string, dir string) { func ConvertWordExcelsToSQLiteIfNeeded(tableName string, dir string) {
if !isExcelChanged(dir) { if !isExcelChanged(dir) {
return //return
} }
files := make([]string, 0) files := make([]string, 0)
...@@ -231,26 +230,35 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ( ...@@ -231,26 +230,35 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) (
} }
selectCol := field.Select selectCol := field.Select
if vari.Def.Type == constant.ConfigTypeArticle {
selectCol = stringUtils.GetPinyin(selectCol)
}
from := dbName from := dbName
if tableName != "" { if tableName != "" {
from += "_" + tableName from += "_" + tableName
} }
where := field.Where where := strings.TrimSpace(field.Where)
if vari.Def.Type == constant.ConfigTypeArticle { if vari.Def.Type == constant.ConfigTypeArticle {
where = stringUtils.GetPinyin(where) if where == "" {
} where = "y"
if where == "" { }
where = "1=1"
} cols := strings.Split(selectCol, "-")
if vari.Def.Type == constant.ConfigTypeArticle && strings.ToLower(where) == "true" { wheres := ""
where = selectCol + " = 'y'" for index, col := range cols {
} if index == 0 {
wheres += fmt.Sprintf("`%s` = '%s'", col, "y")
} else {
wheres += " AND "
wheres += fmt.Sprintf("`%s` = '%s'", col, where)
}
}
where = wheres
where = strings.Replace(where, "-", "_", -1) } else {
if where == "" {
where = "1=1"
}
}
if field.Rand { if field.Rand {
where += " ORDER BY RANDOM() " where += " ORDER BY RANDOM() "
...@@ -266,7 +274,7 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ( ...@@ -266,7 +274,7 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) (
colStr := selectCol colStr := selectCol
if vari.Def.Type == constant.ConfigTypeArticle { if vari.Def.Type == constant.ConfigTypeArticle {
colStr = "ci AS " + selectCol colStr = "`词语` AS `" + selectCol + "`"
} }
sqlStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s", colStr, from, where) sqlStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s", colStr, from, where)
...@@ -317,10 +325,6 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ( ...@@ -317,10 +325,6 @@ func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) (
} }
} }
if field.Select == "xingrongci_waimao_nvxing" {
log.Println(field.Select)
}
return list, selectCol return list, selectCol
} }
...@@ -442,7 +446,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] ...@@ -442,7 +446,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
colPrefix := fileName // stringUtils.GetPinyin(fileName) colPrefix := fileName // stringUtils.GetPinyin(fileName)
*ddlFields = append(*ddlFields, " `" + colPrefix + "` VARCHAR DEFAULT ''") *ddlFields = append(*ddlFields, " `" + colPrefix + "` VARCHAR DEFAULT ''")
for rowIndex, sheet := range excel.GetSheetList() { for sheetIndex, sheet := range excel.GetSheetList() {
rows, _ := excel.GetRows(sheet) rows, _ := excel.GetRows(sheet)
if len(rows) == 0 { if len(rows) == 0 {
continue continue
...@@ -450,69 +454,95 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[] ...@@ -450,69 +454,95 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
colDefine := "" colDefine := ""
colList := make([]string, 0) colList := make([]string, 0)
colList = append(colList, "`" + colPrefix + "`")
colCount := 0 colCount := 0
index := 0 index := 0
// gen col list for ddl and insert cols
for colIndex, col := range rows[0] { for colIndex, col := range rows[0] {
val := strings.TrimSpace(col) val := strings.TrimSpace(col)
if rowIndex == 0 && val == "" {
if sheetIndex == 0 && val == "" {
break break
} }
colCount++ colCount++
colName := val // stringUtils.GetPinyin(val) colList = append(colList, val)
if (*colMap)[colName] == false { colNames := val
colType := "VARCHAR" colNameArr := strings.Split(colNames, "-")
colDefine = " " + "`" + colName + "` " + colType + " DEFAULT ''" for _, colName := range colNameArr {
if (*colMap)[colName] == false {
colType := "VARCHAR"
colDefine = " " + "`" + colName + "` " + colType + " DEFAULT ''"
if colIndex == 0 { if colIndex == 0 {
colName = "词语" colName = "词语"
} else { // first already added } else { // first already added
*ddlFields = append(*ddlFields, colDefine) *ddlFields = append(*ddlFields, colDefine)
} }
(*colMap)[colName] = true (*colMap)[colName] = true
}
} }
colList = append(colList, "`" + colName + "`")
index++ index++
} }
valList := make([]string, 0) insertTemplate := "INSERT INTO `" + tableName + "` (%s) VALUES (%s);"
// gen values for insert
for rowIndex, row := range rows { for rowIndex, row := range rows {
if rowIndex == 0 { if rowIndex == 0 { // ignore title line
continue continue
} }
valListItem := make([]string, 0) record := map[string]interface{}{}
valListItem = append(valListItem, strconv.Itoa(*seq)) record[colPrefix] = "y"
valListItem = append(valListItem, "'y'") record["seq"] = *seq
*seq += 1 *seq += 1
for i := 0; i < colCount; i++ { for colIndex, col := range row {
val := "" if colIndex >= len(colList) {
if i == 0 { // word break
val = strings.TrimSpace(row[i]) }
} else if i <= len(row) - 1 { // excel value
val = strings.ToLower(strings.TrimSpace(row[i])) colNames := colList[colIndex]
if val != "y" && val != "b" && val != "f" && val != "m" {
val = "" val := strings.ToLower(strings.TrimSpace(col))
} if val == "" {
continue
}
if colIndex == 0 { // word
record["词语"] = val
} else { } else {
val = "" if val != "y" && val != "b" && val != "f" && val != "m" { val = "" }
colNameArr := strings.Split(colNames, "-")
for _, colName := range colNameArr {
record[colName] = val
}
} }
valListItem = append(valListItem,"'" + val + "'")
} }
valList = append(valList, "(" + strings.Join(valListItem, ", ") + ")")
}
insertTemplate := "INSERT INTO `" + tableName + "` (`seq`, %s) VALUES %s;" cols := make([]string, 0)
insertSql := fmt.Sprintf(insertTemplate, vals := make([]string, 0)
strings.Join(colList, ", "),
strings.Join(valList, ", "), for key, val := range record {
) cols = append(cols, "`" + key + "`")
*insertSqls = append(*insertSqls, insertSql)
valStr := ""
switch val.(type) {
case int:
valStr = strconv.Itoa(val.(int))
default:
valStr = "'" + val.(string) + "'"
}
vals = append(vals, valStr)
}
insertSql := fmt.Sprintf(insertTemplate, strings.Join(cols, ","), strings.Join(vals, ","))
*insertSqls = append(*insertSqls, insertSql)
}
} }
} }
\ No newline at end of file
...@@ -48,14 +48,8 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) { ...@@ -48,14 +48,8 @@ func loadResField(field *model.DefField, res *map[string]map[string][]string) {
} }
} else if field.From != "" { // refer to res } else if field.From != "" { // refer to res
var valueMap map[string][]string var valueMap map[string][]string
if vari.Def.Type == constant.ConfigTypeArticle && field.UseLastSameValue { // use last resFile, resType, sheet := fileUtils.GetResProp(field.From)
valueMap = getLastDuplicateVal((*res)[field.From], field.Select) valueMap, _ = getResValue(resFile, resType, sheet, field)
}
if valueMap == nil {
resFile, resType, sheet := fileUtils.GetResProp(field.From)
valueMap, _ = getResValue(resFile, resType, sheet, field)
}
if (*res)[field.From] == nil { if (*res)[field.From] == nil {
(*res)[field.From] = map[string][]string{} (*res)[field.From] = map[string][]string{}
......
...@@ -85,7 +85,6 @@ type FieldSimple struct { ...@@ -85,7 +85,6 @@ type FieldSimple struct {
LoopIndex int `yaml:"-"` LoopIndex int `yaml:"-"`
IsRand bool `yaml:"-"` IsRand bool `yaml:"-"`
IsReferYaml bool `yaml:"-"` IsReferYaml bool `yaml:"-"`
UseLastSameValue bool `yaml:"useLastSameValue"`
} }
type FieldWithValues struct { type FieldWithValues struct {
...@@ -124,5 +123,5 @@ type DefFieldExport struct { ...@@ -124,5 +123,5 @@ type DefFieldExport struct {
Where string `yaml:"where"` Where string `yaml:"where"`
Rand bool `yaml:"rand"` Rand bool `yaml:"rand"`
Limit int `yaml:"limit"` Limit int `yaml:"limit"`
UseLastSameValue bool `yaml:"useLastSameValue"` Range string `yaml:"range"`
} }
\ No newline at end of file
package service package service
import ( import (
"fmt"
"github.com/easysoft/zendata/src/model" "github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const" constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file" fileUtils "github.com/easysoft/zendata/src/utils/file"
stringUtils "github.com/easysoft/zendata/src/utils/string"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"path" "path"
...@@ -22,8 +20,6 @@ const ( ...@@ -22,8 +20,6 @@ const (
expRight = ")" expRight = ")"
table = "words.v1" table = "words.v1"
//src = "data/words"
//dist = "demo"
) )
var ( var (
compares = []string{"=", "!=", ">", "<"} compares = []string{"=", "!=", ">", "<"}
...@@ -71,6 +67,13 @@ func convertToYaml(article, filePath string) (content string) { ...@@ -71,6 +67,13 @@ func convertToYaml(article, filePath string) (content string) {
prefix = "" prefix = ""
} else { } else {
prefix += val prefix += val
if prefix != "" && index == len(sections) - 1 {
field := model.DefFieldExport{}
field.Range = prefix
conf.XFields = append(conf.XFields, field)
}
} }
} }
...@@ -111,37 +114,26 @@ func createFields(index int, prefix, exp string) (fields []model.DefFieldExport) ...@@ -111,37 +114,26 @@ func createFields(index int, prefix, exp string) (fields []model.DefFieldExport)
if string(expArr[0]) == "s" && (string(expArr[1]) == ":" || string(expArr[1]) == ":") { if string(expArr[0]) == "s" && (string(expArr[1]) == ":" || string(expArr[1]) == ":") {
exp = string(expArr[2:]) exp = string(expArr[2:])
expArr = expArr[2:] expArr = expArr[2:]
field.UseLastSameValue = true
} }
if strings.Index(exp, "=") == len(exp) - 2 { if strings.Index(exp, "=") == len(exp) - 2 {
exp = string(expArr[:len(expArr) - 2]) exp = string(expArr[:len(expArr) - 2])
field.Select = stringUtils.GetPinyin(exp) field.Select = exp
field.Where = fmt.Sprintf("%s = '%s'", field.Select, string(expArr[len(expArr) - 1])) field.Where = string(expArr[len(expArr) - 1])
} else { } else {
field.Select = stringUtils.GetPinyin(exp) field.Select = exp
field.Where = "true" field.Where = ""
//field.Where = getPinyin(exp) + " = 'y'"
} }
if strings.Index(field.Select, "+") < 0 { if strings.Index(field.Select, "+") < 0 {
fields = append(fields, field) fields = append(fields, field)
} else if strings.Index(field.Select, "+") > 0 { // include more than one field, split to two } else if strings.Index(field.Select, "+") > 0 { // include more than one field, split to two
arr := strings.Split(field.Where, "=")
right := ""
if len(arr) > 1 {
right = arr[1]
}
items := strings.Split(field.Select, "+") items := strings.Split(field.Select, "+")
for _, item := range items { for _, item := range items {
var objClone interface{} = field var objClone interface{} = field
fieldClone := objClone.(model.DefFieldExport) fieldClone := objClone.(model.DefFieldExport)
fieldClone.Select = item fieldClone.Select = item
fieldClone.Where = field.Where
if len(arr) > 1 { // has conditions
fieldClone.Where = item + " = " + right
}
fields = append(fields, fieldClone) fields = append(fields, fieldClone)
} }
...@@ -193,6 +185,10 @@ func parseSections(content string) (sections []map[string]string) { ...@@ -193,6 +185,10 @@ func parseSections(content string) (sections []map[string]string) {
} else { } else {
section += str section += str
} }
//if i == len(runeArr) - 1 && len(section) > 0 {
// addSection(section, "str", &sections)
//}
} }
return return
......
无法预览此类型文件
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册