Skip to content

Commit

Permalink
support corpus files from chatterbot
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 5, 2021
1 parent 8c87c06 commit 2895e5b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
# corpus files
data
**/*.corpus.json
**/*.yml
**/*.gob
**/stopwords.txt
53 changes: 46 additions & 7 deletions bot/corpus/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package corpus

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

type Corpus struct {
Categories []string `json:"categories"`
Conversations [][]string `json:"conversations"`
}

func LoadCorpora(filePaths []string) (map[string][][]string, error) {
result := make(map[string][][]string)

Expand All @@ -23,15 +32,45 @@ func LoadCorpora(filePaths []string) (map[string][][]string, error) {
}

func readCorpus(file string) (map[string][][]string, error) {
var result map[string][][]string

if f, err := os.Open(file); err != nil {
f, err := os.Open(file)
if err != nil {
return nil, err
} else if content, err := ioutil.ReadAll(f); err != nil {
}

ext := filepath.Ext(file)
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
} else if err := json.Unmarshal(content, &result); err != nil {
}

ret, err := unmarshal(ext, content)
if err != nil {
return nil, err
} else {
return result, nil
}

return ret, nil
}

func unmarshal(ext string, content []byte) (map[string][][]string, error) {
var corpus Corpus
ret := make(map[string][][]string)

switch ext {
case ".json":
if err := json.Unmarshal(content, &corpus); err != nil {
return nil, err
}
case ".yml", ".yaml":
if err := yaml.Unmarshal(content, &corpus); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown file type: %s", ext)
}

for _, v := range corpus.Categories {
ret[v] = corpus.Conversations
}

return ret, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.15
require (
github.com/tal-tech/go-zero v1.2.1
github.com/wangbin/jiebago v0.3.2
gopkg.in/yaml.v2 v2.4.0
)

0 comments on commit 2895e5b

Please sign in to comment.