Skip to content

Commit

Permalink
Merge pull request #77 from Harkishen-Singh/general-conversations
Browse files Browse the repository at this point in the history
support for General conversations
  • Loading branch information
Harkishen-Singh authored Apr 5, 2019
2 parents d34d3ac + c4a28ba commit 6defef4
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ view/package-lock.json
tests/node_modules
tests/package-lock.json
service/audio/
conversations/audio/
tests/audio/
tests/mochawesome-report
package-lock.json
service/subprocesses/node_modules/
25 changes: 21 additions & 4 deletions service/controllers/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strings"
"encoding/json"
"github.com/Harkishen-Singh/Jarvis-personal-assistant/service/messages"
"fmt"
)

Expand Down Expand Up @@ -222,22 +223,38 @@ func routes(routeObject response, w http.ResponseWriter) {
}
} else {
// general conversation
w.Write([]byte(`{"status": "success", "message": "Hi from reply bot", "result": ""}`))
TextToSpeech("Hi from reply bot", 0)
speech := messages.GeneralConvHandler(routeObject.message, routeObject.username, w)
TextToSpeech(filterForSpeech(speech), 0)
}
} else {

if strings.ToLower(firstPars) == "google" || strings.ToLower(firstPars) == "yahoo" || strings.ToLower(firstPars) == "bing" || strings.ToLower(firstPars) == "youtube" || strings.ToLower(firstPars) == "image" || strings.ToLower(firstPars) == "weather" {
w.Write([]byte(`{"status": "success", "message": "Services unavailable at the moment ! Check your Internet Connection and try again.", "result": ""}`))
TextToSpeech("Services unavailable at the moment!", 0)
} else {
// general conversation
w.Write([]byte(`{"status": "success", "message": "Hi from reply bot", "result": ""}`))
TextToSpeech("Hi from reply bot", 0)
speech := messages.GeneralConvHandler(routeObject.message, routeObject.username, w)
TextToSpeech(filterForSpeech(speech), 0)
}
}

}

func filterForSpeech(s string) string {

s = strings.Replace(s, "?", " ", -1)
s = strings.Replace(s, "%", " ", -1)
s = strings.Replace(s, "#", " ", -1)
s = strings.Replace(s, "$", " ", -1)
s = strings.Replace(s, "@", " ", -1)
s = strings.Replace(s, "&", " ", -1)
s = strings.Replace(s, "^", " ", -1)
s = strings.Replace(s, "*", " ", -1)
s = strings.Replace(s, "/", ", ", -1)
return s

}

// gives the difference of two string arrays as an array of the differed element
func stringDifference(slice1 []string, slice2 []string) []string {
var diff []string
Expand Down
2 changes: 1 addition & 1 deletion service/controllers/text_to_speech.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TextToSpeech(message string, waitInSeconds int32) bool {

time.Sleep(time.Duration(waitInSeconds))
fmt.Println("here")
fmt.Println("Speaking -> ", message)
speech := htgotts.Speech{Folder: "audio", Language: "en"}
speech.Speak(message)
return true
Expand Down
241 changes: 236 additions & 5 deletions service/messages/conversations.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,272 @@
package messages

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

type response struct {
username string
message string
}

type jsonResponse struct {
Status bool `json:"status"`
Message string `json:"message"`
Show bool `json:"show"`
Result []string `json:"result"`
}

// Messages json parser for default string types
type Messages struct {
InitialGreetingsName []string `json:"initial-greetings-name"`
InitialGreetingsPlain []string `json:"initial-greetings-plain"`
Help []string `json:"help"`
About []string `json:"about"`
Age []string `json:"age"`
Birthday []string `json:"birthday"`
}

// Messagesreplies json parser for default reply string types
type Messagesreplies struct {
InitialGreetingsName []string `json:"initial-greetings-name"`
InitialGreetingsPlain []string `json:"initial-greetings-plain"`
Help []string `json:"help"`
About []string `json:"about"`
Age []string `json:"age"`
Birthday []string `json:"birthday"`
}

var (
messagesParser Messages
messagesRepliesParser Messagesreplies
resp jsonResponse
username, speak string
)

func loadJSONParsers() {
func loadJSONParsers(name string) {

fmt.Println("Loading JSON parsers....")
messagesFile, err := os.Open("messages/messages.json")
messagesRepliesFile, err2 := os.Open("messages/messages_replies.json")
bytvalMF, _ := ioutil.ReadAll(messagesFile)
bytvalMRF, _ := ioutil.ReadAll(messagesRepliesFile)
if err != nil {
panic(err)
}
if err2 != nil {
panic(err2)
}
username = name

err1 := json.Unmarshal(bytvalMF, &messagesParser)
err2 = json.Unmarshal(bytvalMRF, &messagesRepliesParser)
if err1 != nil {
panic(err1)
}
if err2 != nil {
panic(err2)
}

}

func filterForMessagesComparision(s string) (sr string) {

sr = strings.Replace(s, "?", " ", -1)
sr = strings.Replace(sr, "%", " ", -1)
sr = strings.Replace(sr, "#", " ", -1)
sr = strings.Replace(sr, "$", " ", -1)
sr = strings.Replace(sr, "@", " ", -1)
sr = strings.Replace(sr, "&", " ", -1)
sr = strings.Replace(sr, "^", " ", -1)
sr = strings.Replace(sr, "*", " ", -1)
return
}

// GeneralConvHandler handles stuff related to general conversation
func GeneralConvHandler(req response, res http.ResponseWriter) {
func GeneralConvHandler(req, name string, res http.ResponseWriter) string {

fmt.Println("General conversation...")
rand.Seed(time.Now().UnixNano())
loadJSONParsers(name)
message := filterForMessagesComparision(req)
match := false

// determine type of message
if !match {
isGreetingPlain := func(s string) bool {
for i:=0; i< len(messagesParser.InitialGreetingsPlain); i++ {
if strings.ToLower(s) == messagesParser.InitialGreetingsPlain[i] {
match = true
return true
}
}
return false
}(message)

if isGreetingPlain {
temp := greetingPlainController(message)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}


if !match {
isGreetingName := func(s string) bool {
for i:=0; i< len(messagesParser.InitialGreetingsName); i++ {
if strings.ToLower(s) == messagesParser.InitialGreetingsName[i] {
fmt.Println("contains ", strings.ToLower(s), " ", messagesParser.InitialGreetingsName[i])
match = true
return true
}
}
return false
}(message)

if isGreetingName {
temp := greetingNameController(message)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}

if !match {
isHelp := func(s string) bool {
for i:=0; i< len(messagesParser.Help); i++ {
if strings.ToLower(s) == messagesParser.Help[i] {
match = true
return true
}
}
return false
}(message)

if isHelp {
temp := helpController(message)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}

if !match {
isAbout := func(s string) bool {
for i:=0; i< len(messagesParser.About); i++ {
if strings.ToLower(s) == messagesParser.About[i] {
match = true
return true
}
}
return false
}(message)

if isAbout {
temp := aboutController(message)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}

if !match {
fmt.Println("inside")
isAge := func(s string) bool {
for i:=0; i< len(messagesParser.Age); i++ {
if strings.ToLower(s) == messagesParser.Age[i] {
match = true
return true
}
}
return false
}(message)

if isAge {
temp := ageController(message)
fmt.Println("temp age is ", temp)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}

if !match {
isBirthday := func(s string) bool {
for i:=0; i< len(messagesParser.Birthday); i++ {
if strings.ToLower(s) == messagesParser.Birthday[i] {
match = true
return true
}
}
return false
}(message)

if isBirthday {
temp := birthdayController(message)
resp = jsonResponse{true, temp, true, nil}
speak = temp
marshalled, _ := json.Marshal(resp)
res.Write(marshalled)
}
}

return speak

}

func helpController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.Help))
return messagesRepliesParser.Help[numb]

}

func greetingPlainController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.InitialGreetingsPlain))
return messagesRepliesParser.InitialGreetingsPlain[numb]

}

func greetingNameController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.InitialGreetingsName))
temp := messagesRepliesParser.InitialGreetingsName[numb]
reply := fmt.Sprintf(temp, username) // note the formatter in messages_replies used
return reply

}

func aboutController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.About))
return messagesRepliesParser.About[numb]

}

func ageController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.Age))
return messagesRepliesParser.Age[numb]

}

func birthdayController(s string) string {

numb := rand.Intn(len(messagesRepliesParser.Birthday))
return messagesRepliesParser.Birthday[numb]

}
Loading

0 comments on commit 6defef4

Please sign in to comment.