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

generate yaml and article

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