Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature for get realistic words according a context #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions realistic_word.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package faker

import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
)

type WordInfo struct {
Word string `json:"word"`
Score int `json:"score"`
Tags []string `json:"tags"`
}

const apiEndpoint = "https://api.datamuse.com/words?ml=%s&max=%d"

var consultService = func(context string, max int, w *[]WordInfo) {
replaceContext := strings.Replace(context, " ", "+", -1)
apiURL := fmt.Sprintf(apiEndpoint, replaceContext, max)

resData, err := http.Get(apiURL)
if err != nil {
fmt.Println("Error in HTTP request:", err)
return
}
defer resData.Body.Close()

bodyRes, err := ioutil.ReadAll(resData.Body)
if err != nil {
fmt.Println("Error reading response data:", err)
return
}

err = json.Unmarshal([]byte(bodyRes), &w)
if err != nil {
fmt.Println("Error deserializing JSON:", err)
return
}
}

func getWords(context string) []string {
var words []WordInfo
consultService(context, 20, &words)

result := []string{}
for _, word := range words {
result = append(result, word.Word)
}
return result
}

func getRandomIndex(max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max + 1)
}

func getWord(context string) string {
var words []WordInfo
consultService(context, 10, &words)
randIndex := getRandomIndex(len(words) - 1)
return words[randIndex].Word
}
48 changes: 48 additions & 0 deletions realistic_word_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package faker

import (
"testing"
)

func TestGetWord(t *testing.T) {
tables := []struct {
context string
mockFunc func()
expectWords []string
}{
{
context: "test",
mockFunc: func() {
consultService = func(context string, max int, w *[]WordInfo) {
*w = []WordInfo{
{Word: "test1", Score: 1, Tags: []string{"tag1"}},
{Word: "test2", Score: 2, Tags: []string{"tag2"}},
{Word: "test3", Score: 3, Tags: []string{"tag3"}},
}
}
},
expectWords: []string{"test1", "test2", "test3"},
},
}

originalResponse := consultService
for _, table := range tables {
table.mockFunc()
resultWords := getWords(table.context)
if len(resultWords) != len(table.expectWords) {
t.Errorf("getWords(%s) was incorrect, got: %d, want: %d.", table.context, len(resultWords), len(table.expectWords))
}
resultWord := getWord(table.context)
valid := false
for _, w := range table.expectWords {
if w == resultWord {
valid = true
break
}
}
if !valid {
t.Errorf("getWord(%s) was incorrect, got: %s.", table.context, resultWord)
}
consultService = originalResponse
}
}