Skip to content

Commit

Permalink
feat: support loading whole directory on trainning
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 6, 2021
1 parent 2895e5b commit 8a0c758
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions cli/train/train.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"flag"
"fmt"
"log"
"path/filepath"
"strings"

"github.com/kevwan/chatbot/bot"
Expand All @@ -11,14 +13,27 @@ import (

var (
corpora = flag.String("i", "", "the corpora files, comma to separate multiple files")
dir = flag.String("d", "", "the directory to look for corpora files")
storeFile = flag.String("o", "corpus.gob", "the file to store corpora")
printMemStats = flag.Bool("m", false, "enable printing memory stats")
)

func main() {
flag.Parse()

if len(*corpora) == 0 {
var files []string
if len(*dir) > 0 {
files = findCorporaFiles(*dir)
}

var corporaFiles string
if len(files) > 0 {
corporaFiles = strings.Join(files, ",")
}
if len(*corpora) > 0 {
corporaFiles = strings.Join([]string{corporaFiles, *corpora}, ",")
}
if len(corporaFiles) == 0 {
flag.Usage()
return
}
Expand All @@ -33,7 +48,35 @@ func main() {
Trainer: bot.NewCorpusTrainer(store),
StorageAdapter: store,
}
if err := chatbot.Train(strings.Split(*corpora, ",")); err != nil {
if err := chatbot.Train(strings.Split(corporaFiles, ",")); err != nil {
log.Fatal(err)
}
}

func findCorporaFiles(dir string) []string {
var files []string

jsonFiles, err := filepath.Glob(filepath.Join(dir, "*.json"))
if err != nil {
fmt.Println(err)
return nil
}

files = append(files, jsonFiles...)

ymlFiles, err := filepath.Glob(filepath.Join(dir, "*.yml"))
if err != nil {
fmt.Println(err)
return nil
}

files = append(files, ymlFiles...)

yamlFiles, err := filepath.Glob(filepath.Join(dir, "*.yaml"))
if err != nil {
fmt.Println(err)
return nil
}

return append(files, yamlFiles...)
}

0 comments on commit 8a0c758

Please sign in to comment.