Skip to content

Commit

Permalink
chore: upgrade go-zero
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Aug 19, 2023
1 parent 406218e commit 3ffb49f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 668 deletions.
19 changes: 8 additions & 11 deletions bot/adapters/logic/closestmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ func (match *closestMatch) processExactMatch(responses map[string]int) []Answer
}

func (match *closestMatch) processSimilarMatch(text string) []Answer {
result, err := mr.MapReduce(generator(match, text), mapper(match), reducer(match))
slice, err := mr.MapReduce(generator(match, text), mapper(match), reducer(match))
if err != nil {
return nil
}

var answers []Answer
slice := result.([]questionAndScore)
for _, each := range slice {
if each.score > 0 {
if responses, ok := match.storage.Find(each.question); ok {
Expand Down Expand Up @@ -145,8 +144,8 @@ func (top *topOccurAnswers) put(answer string, occurrence int) {
}
}

func generator(match *closestMatch, text string) mr.GenerateFunc {
return func(source chan<- interface{}) {
func generator(match *closestMatch, text string) mr.GenerateFunc[sourceAndTargets] {
return func(source chan<- sourceAndTargets) {
keys := match.storage.Search(text)
if match.verbose {
printMatches(keys)
Expand All @@ -162,10 +161,9 @@ func generator(match *closestMatch, text string) mr.GenerateFunc {
}
}

func mapper(match *closestMatch) mr.MapperFunc {
return func(data interface{}, writer mr.Writer, cancel func(error)) {
func mapper(match *closestMatch) mr.MapperFunc[sourceAndTargets, *topScoreQuestions] {
return func(pair sourceAndTargets, writer mr.Writer[*topScoreQuestions], cancel func(error)) {
tops := newTopScoreQuestions(match.tops)
pair := data.(sourceAndTargets)
for i := range pair.targets {
score := nlp.SimilarityForStrings(pair.source, pair.targets[i])
tops.add(questionAndScore{
Expand All @@ -178,11 +176,10 @@ func mapper(match *closestMatch) mr.MapperFunc {
}
}

func reducer(match *closestMatch) mr.ReducerFunc {
return func(input <-chan interface{}, writer mr.Writer, cancel func(error)) {
func reducer(match *closestMatch) mr.ReducerFunc[*topScoreQuestions, []questionAndScore] {
return func(input <-chan *topScoreQuestions, writer mr.Writer[[]questionAndScore], cancel func(error)) {
tops := newTopScoreQuestions(match.tops)
for each := range input {
qs := each.(*topScoreQuestions)
for qs := range input {
for _, question := range qs.questions {
tops.add(question)
}
Expand Down
14 changes: 6 additions & 8 deletions bot/adapters/storage/memorystorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (storage *memoryStorage) buildKeys() []string {
}

func (storage *memoryStorage) buildIndex(keys []string) map[string][]int {
channel := make(chan interface{})
channel := make(chan *keyChunk)

go func() {
chunks := splitStrings(keys, chunkSize)
Expand All @@ -188,7 +188,7 @@ func (storage *memoryStorage) buildIndex(keys []string) map[string][]int {
close(channel)
}()

result, err := mr.MapReduce(func(source chan<- interface{}) {
result, err := mr.MapReduce(func(source chan<- *keyChunk) {
chunks := splitStrings(keys, chunkSize)
for i := range chunks {
source <- chunks[i]
Expand All @@ -199,7 +199,7 @@ func (storage *memoryStorage) buildIndex(keys []string) map[string][]int {
return nil
}

return result.(map[string][]int)
return result
}

func (storage *memoryStorage) saveStopWords() {
Expand Down Expand Up @@ -301,9 +301,8 @@ func (storage *memoryStorage) generateSearchResults(ids []int) []string {
return result
}

func (storage *memoryStorage) mapper(data interface{}, writer mr.Writer, cancel func(error)) {
func (storage *memoryStorage) mapper(chunk *keyChunk, writer mr.Writer[map[string][]int], cancel func(error)) {
indexes := make(map[string][]int)
chunk := data.(*keyChunk)

for i := range chunk.keys {
collector := func(word string) {
Expand Down Expand Up @@ -333,10 +332,9 @@ func (storage *memoryStorage) mapper(data interface{}, writer mr.Writer, cancel
writer.Write(indexes)
}

func (storage *memoryStorage) reducer(input <-chan interface{}, writer mr.Writer, cancel func(error)) {
func (storage *memoryStorage) reducer(input <-chan map[string][]int, writer mr.Writer[map[string][]int], cancel func(error)) {
indexes := make(map[string]map[int]struct{})
for each := range input {
chunkIndexes := each.(map[string][]int)
for chunkIndexes := range input {
for key, ids := range chunkIndexes {
mapping, ok := indexes[key]
if !ok {
Expand Down
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module github.com/kevwan/chatbot

go 1.17
go 1.18

require (
github.com/wangbin/jiebago v0.3.2
github.com/zeromicro/go-zero v1.3.1
github.com/zeromicro/go-zero v1.5.4
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/google/go-cmp v0.5.7 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.opentelemetry.io/otel v1.3.0 // indirect
go.opentelemetry.io/otel/trace v1.3.0 // indirect
go.uber.org/automaxprocs v1.4.0 // indirect
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/automaxprocs v1.5.2 // indirect
golang.org/x/sys v0.10.0 // indirect
)
Loading

0 comments on commit 3ffb49f

Please sign in to comment.