From 9d71214620629ed84073647c483d30757a9e3f77 Mon Sep 17 00:00:00 2001 From: muskankhedia Date: Fri, 12 Apr 2019 20:36:36 +0530 Subject: [PATCH 1/5] added priority list --- service/controllers/messages.go | 158 +++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 52 deletions(-) diff --git a/service/controllers/messages.go b/service/controllers/messages.go index 341b15a..ffd91c0 100644 --- a/service/controllers/messages.go +++ b/service/controllers/messages.go @@ -98,66 +98,98 @@ func routes(routeObject response, w http.ResponseWriter) { /*message := routeObject.message messageArr := strings.Split(message, " ") // messageTemp := message - var firstPars string + var matchPars string if strings.Contains(message, " ") { - firstPars = message[:strings.Index(message, " ")] + matchPars = message[:strings.Index(message, " ")] } else { - firstPars = message + matchPars = message } - strArr := strings.Split(firstPars, " ") + strArr := strings.Split(matchPars, " ") strArrDiff := strings.Split(message, " ") remainingString := strings.Join(stringDifference(strArr, strArrDiff), " ")*/ // lastParsArr := strings.Split(messageTemp, " ") // lastPars := lastParsArr[len(lastParsArr) - 1] - message := routeObject.message - messageArr := strings.Split(message, " ") - var firstPars string - var secondPars string - var thirdPars string - if len(messageArr)>=3 { - firstPars=messageArr[0] - secondPars = messageArr[1] - thirdPars = messageArr[2] - } else if len(messageArr)==2 { - firstPars=messageArr[0] - secondPars = messageArr[1] - } else if(len(messageArr)==1) { - firstPars=messageArr[0] - } - var remainingString string - var strArr []string - var strArr0 string - var strArrDiff []string - strArrDiff = strings.Split(message, " ") - if strings.ToLower(firstPars) == "google" || strings.ToLower(firstPars) =="yahoo" || strings.ToLower(firstPars) =="bing" || strings.ToLower(firstPars) =="search" || strings.ToLower(firstPars) =="youtube"|| strings.ToLower(firstPars) == "watch"||strings.ToLower(firstPars) =="videos" || strings.ToLower(firstPars) =="images" || strings.ToLower(firstPars) =="image" || strings.ToLower(firstPars) =="meaning"{ - if strings.ToLower(secondPars) == "search" || strings.ToLower(secondPars) =="google" ||strings.ToLower(secondPars) =="yahoo" ||strings.ToLower(secondPars) =="bing" ||strings.ToLower(secondPars) =="videos" ||strings.ToLower(secondPars) =="youtube"||strings.ToLower(secondPars) =="for" || strings.ToLower(secondPars) =="of"{ - if strings.ToLower(thirdPars) == "for" || strings.ToLower(thirdPars) =="videos" { - strArr0=firstPars+" "+secondPars+" "+thirdPars - strArr=strings.Split(strArr0," ") - remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - fmt.Println(remainingString) - } else { - strArr0=firstPars+" "+secondPars - strArr=strings.Split(strArr0," ") - remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - fmt.Println(remainingString) - } - } else { - strArr = strings.Split(firstPars, " ") - strArrDiff = strings.Split(message, " ") - remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - fmt.Println(remainingString) + // message := routeObject.message + // messageArr := strings.Split(message, " ") + // var matchPars string + // var secondPars string + // var thirdPars string + // if len(messageArr)>=3 { + // matchPars=messageArr[0] + // secondPars = messageArr[1] + // thirdPars = messageArr[2] + // } else if len(messageArr)==2 { + // matchPars=messageArr[0] + // secondPars = messageArr[1] + // } else if(len(messageArr)==1) { + // matchPars=messageArr[0] + // } + + // var remainingString string + // var strArr []string + // var strArr0 string + // var strArrDiff []string + // strArrDiff = strings.Split(message, " ") + // if strings.ToLower(matchPars) == "google" || strings.ToLower(matchPars) =="yahoo" || strings.ToLower(matchPars) =="bing" || strings.ToLower(matchPars) =="search" || strings.ToLower(matchPars) =="youtube"|| strings.ToLower(matchPars) == "watch"||strings.ToLower(matchPars) =="videos" || strings.ToLower(matchPars) =="images" || strings.ToLower(matchPars) =="image" || strings.ToLower(matchPars) =="meaning"{ + // if strings.ToLower(secondPars) == "search" || strings.ToLower(secondPars) =="google" ||strings.ToLower(secondPars) =="yahoo" ||strings.ToLower(secondPars) =="bing" ||strings.ToLower(secondPars) =="videos" ||strings.ToLower(secondPars) =="youtube"||strings.ToLower(secondPars) =="for" || strings.ToLower(secondPars) =="of"{ + // if strings.ToLower(thirdPars) == "for" || strings.ToLower(thirdPars) =="videos" { + // strArr0=matchPars+" "+secondPars+" "+thirdPars + // strArr=strings.Split(strArr0," ") + // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") + // fmt.Println(remainingString) + // } else { + // strArr0=matchPars+" "+secondPars + // strArr=strings.Split(strArr0," ") + // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") + // fmt.Println(remainingString) + // } + // } else { + // strArr = strings.Split(matchPars, " ") + // strArrDiff = strings.Split(message, " ") + // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") + // fmt.Println(remainingString) + // } + // } + + message := routeObject.message + messageArr := strings.Fields(message) + var a []string + priority := []string{"images", "image", "video", "videos", "symptoms", "medicine", "weather", + "meaning", "google", "yahoo", "bing", "search"} + fmt.Println("Priority: ", priority ) + for i := 0; i < len(messageArr); i++ { + for j := 0; j < len(priority); j++ { + fmt.Println("i: ",i , " ", messageArr[i], "j: ", j, " ", priority[j]) + if (messageArr[i] == priority[j]) { + a = append(a, messageArr[i]) + if i < len(messageArr) { + messageArr = append(messageArr[:i], messageArr[i+1:]...) + i = i - 1 + } else { + fmt.Println("Position Invalid") + } + break; } + } } + + sort := customSort(a, priority, len(a), len(priority)) + matchPars := sort[0] + remainingString := strings.Join(messageArr[:]," ") + messageArr = append([]string{matchPars}, messageArr...) + fmt.Println("messageArr:", messageArr) + fmt.Println("Remaining String:", remainingString) + fmt.Println("matchPars: ", matchPars) + // single word operations if Connected() { - if strings.ToLower(firstPars) == "google" || strings.ToLower(secondPars) == "google" { // for google search + if strings.ToLower(matchPars) == "google" { // for google search query := "" if len(remainingString) == 0 { query = "https://www.google.co.in/search?q=google" @@ -179,7 +211,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(responseJSON.Message, 0) - } else if strings.ToLower(firstPars) == "yahoo" || strings.ToLower(secondPars) == "yahoo"{ + } else if strings.ToLower(matchPars) == "yahoo" { query := "" if len(remainingString) == 0 { query = "https://in.search.yahoo.com/search?p=yahoo" @@ -201,7 +233,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(responseJSON.Message, 0) - } else if strings.ToLower(firstPars) == "bing" || strings.ToLower(secondPars)=="bing"{ + } else if strings.ToLower(matchPars) == "bing" { query := "" if len(remainingString) == 0 { query = "https://www.bing.com/search?q=bing" @@ -223,7 +255,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(responseJSON.Message, 0) - } else if strings.ToLower(firstPars) == "youtube" || strings.ToLower(firstPars) == "videos" || strings.ToLower(firstPars) == "watch" || strings.ToLower(secondPars) == "youtube"{ + } else if strings.ToLower(matchPars) == "youtube" || strings.ToLower(matchPars) == "videos" || strings.ToLower(matchPars) == "watch" { query := "" if len(remainingString) == 0 { query = "https://www.youtube.com/results?search_query=youtube" @@ -245,7 +277,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(responseJSON.Message, 0) - } else if strings.ToLower(firstPars) == "images" || strings.ToLower(firstPars) =="image" { + } else if strings.ToLower(matchPars) == "images" || strings.ToLower(matchPars) =="image" { query := "" if len(remainingString) == 0 { query = "https://www.google.co.in/search?q="+"images"+"&source=lnms&tbm=isch" @@ -266,7 +298,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(responseJSON.Message, 0) - } else if strings.ToLower(firstPars) == "weather" { + } else if strings.ToLower(matchPars) == "weather" { if len(messageArr) == 1 || len(messageArr) < 3 { w.Write([]byte(`{"status": "success", "message": "ENTER: weather ", "result": ""}`)) @@ -284,7 +316,7 @@ func routes(routeObject response, w http.ResponseWriter) { w.Write(jData) TextToSpeech(response.Message + city + " " + state, 0) } - } else if strings.ToLower(firstPars) == "meaning" { + } else if strings.ToLower(matchPars) == "meaning" { if len(messageArr) == 1 { w.Write([]byte(`{"status": "success", "message": "ENTER: meaning ", "result": ""}`)) @@ -325,7 +357,7 @@ func routes(routeObject response, w http.ResponseWriter) { } } - } else if strings.ToLower(firstPars) == "medicine" { + } else if strings.ToLower(matchPars) == "medicine" { if len(messageArr) <= 1 { w.Write([]byte(`{"status": "success", "message": "ENTER: medicine ", "result": ""}`)) @@ -334,7 +366,7 @@ func routes(routeObject response, w http.ResponseWriter) { result := messages.HealthMedController(med, w) TextToSpeech(result, 0) } - } else if strings.ToLower(firstPars) == "symptoms" { + } else if strings.ToLower(matchPars) == "symptoms" { // add support for multiple symptoms at once and use ML to determine the best medicine suited if len(messageArr) < 2 { @@ -365,7 +397,7 @@ func routes(routeObject response, w http.ResponseWriter) { } } 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" || strings.ToLower(firstPars) == "medicine" || strings.ToLower(firstPars) == "symptoms" { + if strings.ToLower(matchPars) == "google" || strings.ToLower(matchPars) == "yahoo" || strings.ToLower(matchPars) == "bing" || strings.ToLower(matchPars) == "youtube" || strings.ToLower(matchPars) == "image" || strings.ToLower(matchPars) == "weather" || strings.ToLower(matchPars) == "medicine" || strings.ToLower(matchPars) == "symptoms" { 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 if strings.HasPrefix(strings.ToLower(message),"set reminder") { @@ -390,6 +422,28 @@ func routes(routeObject response, w http.ResponseWriter) { } +func customSort(arr1 []string, arr2 []string, m,n int) []string{ + freq := make(map[string]int) + + for i := 0; i < m; i++ { + freq[arr1[i]]++; + } + + index := 0 + + for i := 0; i < n; i++ { + for freq[arr2[i]] > 0 { + arr1[index] = arr2[i] + index++ + freq[arr2[i]]-- + } + delete(freq, arr2[i]) + } + fmt.Println(arr1) + + return arr1 +} + func filterForSpeech(s string) string { s = strings.Replace(s, "?", " ", -1) From 147ce7f6dbd23012cabd3c8e2af8ec876a910874 Mon Sep 17 00:00:00 2001 From: muskankhedia Date: Fri, 12 Apr 2019 20:56:59 +0530 Subject: [PATCH 2/5] fixed bugs --- service/controllers/messages.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/service/controllers/messages.go b/service/controllers/messages.go index ffd91c0..daf27b6 100644 --- a/service/controllers/messages.go +++ b/service/controllers/messages.go @@ -175,14 +175,20 @@ func routes(routeObject response, w http.ResponseWriter) { } } } + matchPars := "" + remainingString := "" + if len(a) > 0 { + sort := customSort(a, priority, len(a), len(priority)) + matchPars = sort[0] + remainingString = strings.Join(messageArr[:]," ") + messageArr = append([]string{matchPars}, messageArr...) + fmt.Println("messageArr:", messageArr) + fmt.Println("Remaining String:", remainingString) + fmt.Println("matchPars: ", matchPars) + } else { + remainingString = strings.Join(messageArr[:], " ") + } - sort := customSort(a, priority, len(a), len(priority)) - matchPars := sort[0] - remainingString := strings.Join(messageArr[:]," ") - messageArr = append([]string{matchPars}, messageArr...) - fmt.Println("messageArr:", messageArr) - fmt.Println("Remaining String:", remainingString) - fmt.Println("matchPars: ", matchPars) // single word operations From 7b09a36e4a0e27359036726d32613b08dd4c43c0 Mon Sep 17 00:00:00 2001 From: muskankhedia Date: Fri, 12 Apr 2019 21:26:29 +0530 Subject: [PATCH 3/5] added more conditions --- service/controllers/messages.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service/controllers/messages.go b/service/controllers/messages.go index daf27b6..4cae324 100644 --- a/service/controllers/messages.go +++ b/service/controllers/messages.go @@ -157,7 +157,7 @@ func routes(routeObject response, w http.ResponseWriter) { message := routeObject.message messageArr := strings.Fields(message) var a []string - priority := []string{"images", "image", "video", "videos", "symptoms", "medicine", "weather", + priority := []string{"images", "image", "video", "videos", "watch", "youtube", "symptoms", "medicine", "weather", "meaning", "google", "yahoo", "bing", "search"} fmt.Println("Priority: ", priority ) for i := 0; i < len(messageArr); i++ { @@ -195,7 +195,7 @@ func routes(routeObject response, w http.ResponseWriter) { if Connected() { - if strings.ToLower(matchPars) == "google" { // for google search + if strings.ToLower(matchPars) == "google" || strings.ToLower(matchPars) == "search" { // for google search query := "" if len(remainingString) == 0 { query = "https://www.google.co.in/search?q=google" From 52fadf289261fa8450fcd1cacc63e9cd9cba6881 Mon Sep 17 00:00:00 2001 From: muskankhedia Date: Fri, 12 Apr 2019 21:31:46 +0530 Subject: [PATCH 4/5] minor changes --- service/controllers/messages.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/service/controllers/messages.go b/service/controllers/messages.go index 4cae324..215f6a0 100644 --- a/service/controllers/messages.go +++ b/service/controllers/messages.go @@ -159,10 +159,8 @@ func routes(routeObject response, w http.ResponseWriter) { var a []string priority := []string{"images", "image", "video", "videos", "watch", "youtube", "symptoms", "medicine", "weather", "meaning", "google", "yahoo", "bing", "search"} - fmt.Println("Priority: ", priority ) for i := 0; i < len(messageArr); i++ { for j := 0; j < len(priority); j++ { - fmt.Println("i: ",i , " ", messageArr[i], "j: ", j, " ", priority[j]) if (messageArr[i] == priority[j]) { a = append(a, messageArr[i]) if i < len(messageArr) { @@ -182,11 +180,13 @@ func routes(routeObject response, w http.ResponseWriter) { matchPars = sort[0] remainingString = strings.Join(messageArr[:]," ") messageArr = append([]string{matchPars}, messageArr...) - fmt.Println("messageArr:", messageArr) fmt.Println("Remaining String:", remainingString) fmt.Println("matchPars: ", matchPars) } else { remainingString = strings.Join(messageArr[:], " ") + fmt.Println("messageArr:", messageArr) + fmt.Println("Remaining String:", remainingString) + fmt.Println("matchPars: ", matchPars) } @@ -445,8 +445,6 @@ func customSort(arr1 []string, arr2 []string, m,n int) []string{ } delete(freq, arr2[i]) } - fmt.Println(arr1) - return arr1 } From 952ceacbc0198f3e3ac6df952ef37472483614f6 Mon Sep 17 00:00:00 2001 From: muskankhedia Date: Fri, 12 Apr 2019 22:39:58 +0530 Subject: [PATCH 5/5] minor changes --- service/controllers/messages.go | 71 ++------------------------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/service/controllers/messages.go b/service/controllers/messages.go index 215f6a0..3c6d144 100644 --- a/service/controllers/messages.go +++ b/service/controllers/messages.go @@ -95,73 +95,14 @@ func MessagesController(w http.ResponseWriter, r *http.Request) { func routes(routeObject response, w http.ResponseWriter) { - /*message := routeObject.message - messageArr := strings.Split(message, " ") - // messageTemp := message - var matchPars string - if strings.Contains(message, " ") { - matchPars = message[:strings.Index(message, " ")] - } else { - matchPars = message - } - - strArr := strings.Split(matchPars, " ") - strArrDiff := strings.Split(message, " ") - - remainingString := strings.Join(stringDifference(strArr, strArrDiff), " ")*/ - // lastParsArr := strings.Split(messageTemp, " ") - // lastPars := lastParsArr[len(lastParsArr) - 1] - - // message := routeObject.message - // messageArr := strings.Split(message, " ") - // var matchPars string - // var secondPars string - // var thirdPars string - // if len(messageArr)>=3 { - // matchPars=messageArr[0] - // secondPars = messageArr[1] - // thirdPars = messageArr[2] - // } else if len(messageArr)==2 { - // matchPars=messageArr[0] - // secondPars = messageArr[1] - // } else if(len(messageArr)==1) { - // matchPars=messageArr[0] - // } - - // var remainingString string - // var strArr []string - // var strArr0 string - // var strArrDiff []string - // strArrDiff = strings.Split(message, " ") - // if strings.ToLower(matchPars) == "google" || strings.ToLower(matchPars) =="yahoo" || strings.ToLower(matchPars) =="bing" || strings.ToLower(matchPars) =="search" || strings.ToLower(matchPars) =="youtube"|| strings.ToLower(matchPars) == "watch"||strings.ToLower(matchPars) =="videos" || strings.ToLower(matchPars) =="images" || strings.ToLower(matchPars) =="image" || strings.ToLower(matchPars) =="meaning"{ - // if strings.ToLower(secondPars) == "search" || strings.ToLower(secondPars) =="google" ||strings.ToLower(secondPars) =="yahoo" ||strings.ToLower(secondPars) =="bing" ||strings.ToLower(secondPars) =="videos" ||strings.ToLower(secondPars) =="youtube"||strings.ToLower(secondPars) =="for" || strings.ToLower(secondPars) =="of"{ - // if strings.ToLower(thirdPars) == "for" || strings.ToLower(thirdPars) =="videos" { - // strArr0=matchPars+" "+secondPars+" "+thirdPars - // strArr=strings.Split(strArr0," ") - // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - // fmt.Println(remainingString) - // } else { - // strArr0=matchPars+" "+secondPars - // strArr=strings.Split(strArr0," ") - // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - // fmt.Println(remainingString) - // } - // } else { - // strArr = strings.Split(matchPars, " ") - // strArrDiff = strings.Split(message, " ") - // remainingString=strings.Join(stringDifference(strArr,strArrDiff)," ") - // fmt.Println(remainingString) - // } - // } - message := routeObject.message messageArr := strings.Fields(message) var a []string priority := []string{"images", "image", "video", "videos", "watch", "youtube", "symptoms", "medicine", "weather", - "meaning", "google", "yahoo", "bing", "search"} + "meaning", "google", "yahoo", "bing", "search"} for i := 0; i < len(messageArr); i++ { - for j := 0; j < len(priority); j++ { - if (messageArr[i] == priority[j]) { + for _, prior := range priority { + if (messageArr[i] == prior) { a = append(a, messageArr[i]) if i < len(messageArr) { messageArr = append(messageArr[:i], messageArr[i+1:]...) @@ -180,13 +121,8 @@ func routes(routeObject response, w http.ResponseWriter) { matchPars = sort[0] remainingString = strings.Join(messageArr[:]," ") messageArr = append([]string{matchPars}, messageArr...) - fmt.Println("Remaining String:", remainingString) - fmt.Println("matchPars: ", matchPars) } else { remainingString = strings.Join(messageArr[:], " ") - fmt.Println("messageArr:", messageArr) - fmt.Println("Remaining String:", remainingString) - fmt.Println("matchPars: ", matchPars) } @@ -428,6 +364,7 @@ func routes(routeObject response, w http.ResponseWriter) { } +// customSort() to sort an array according to the order defined by another array func customSort(arr1 []string, arr2 []string, m,n int) []string{ freq := make(map[string]int)