-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big internal engine upgrade/reorganization
- The project is now the commands detection module itself only. If more module are coded in Go, I might just put them all in a folder and compile to a unique library file, but still all from different projects. - No more infinite arrays to describe each command. Now there's only one array of a commandInfo struct, which contains all command information. - Also no more need to have them hard-coded here. Now they can be provided to the library and processed once only when the library is loaded. - With these changes, some flexibility in the commands creation has been lost. But if it's ever needed more flexibility, it will be added back somehow, even if some rollback is needed in part. - The NLPAnalyzer no longer deletes the original "it" word after replacing it: "shut down the phone and reboot computer", now all it does is replace "it" with "phone" and not also delete the word, which would stop the "shut down" command from being detected (this sentence still doesn't work well though, but that's another matter).
- Loading branch information
Showing
26 changed files
with
1,245 additions
and
2,236 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* Copyright 2021 DADi590 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package AdvancedCommandsDetection | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// The value of each TYPE constant is its index on the cmds_types_keywords array | ||
|
||
const CMDi_TYPE_MANUAL string = "0" | ||
const CMDi_TYPE_TURN_ONFF string = "1" | ||
const CMDi_TYPE_ASK string = "2" | ||
const CMDi_TYPE_STOP string = "3" | ||
const CMDi_TYPE_ANSWER string = "4" | ||
const CMDi_TYPE_SHUT_DOWN string = "5" | ||
const CMDi_TYPE_REBOOT string = "6" | ||
const CMDi_TYPE_REPEAT_SPEECH string = "7" | ||
const CMDi_TYPE_RECORD string = "8" | ||
|
||
// Each list of type keywords can have at most 2 arrays inside it (if more are needed, change the implementation, maybe | ||
// even generalize it - for now it's made for case of 1 array and case of 2 arrays). | ||
// The 2 arrays are of words that must be mixed with the command keywords to create the command ("turn", "on" + "wifi", | ||
// for example). | ||
var cmds_types_keywords = [...][][]string{ | ||
{}, // 0 - Ignored | ||
{ // 1 | ||
{"turn", "get", "switch", "put"}, // Default main words for this type to put be on the main_words array | ||
{"on", "off"}, // Other optional words to continue the first ones if necessary (turn... what? something. what? | ||
// on or off) | ||
}, | ||
{ // 2 | ||
{"what's", "tell", "say"}, | ||
}, | ||
{ // 3 | ||
{"stop", "end", "finish", "cease", "conclude", "terminate"}, | ||
}, | ||
{ // 4 | ||
{"answer", "reply", "respond", "acknowledge"}, | ||
}, | ||
{ // 5 | ||
{"shut", "power"}, | ||
{"down", "off"}, | ||
}, | ||
{ // 6 | ||
{"reboot", "restart"}, | ||
}, | ||
{ // 7 | ||
{"what", "say", "come", "go", "repeat"}, | ||
}, | ||
{ // 8 | ||
{"record"}, | ||
}, | ||
} | ||
|
||
func PrepareCmdsArray(commands_str string) { | ||
// Reset the commands array | ||
cmds_GL = nil | ||
|
||
var commands_info []string = strings.Split(commands_str, "\\") | ||
|
||
for i := 0; i < len(commands_info); i++ { | ||
var cmd_info []string = strings.Split(commands_info[i], "||") | ||
|
||
cmd_id, _ := strconv.Atoi(cmd_info[0]) | ||
|
||
cmds_GL = append(cmds_GL, commandInfo{ | ||
cmd_id: cmd_id, | ||
main_words: nil, | ||
main_words_ret_conds: nil, | ||
words_list: nil, | ||
left_intervs: nil, | ||
right_intervs: nil, | ||
init_indexes_sub_verifs: nil, | ||
exclude_word_found_group: nil, | ||
ignore_repets_cmds: false, | ||
exclude_mutually_exclusive_words: true, | ||
}) | ||
|
||
prepareCmdArray(&cmds_GL[i], strings.Split(cmd_info[1], " "), strings.Split(cmd_info[2], " "), cmd_info[3], | ||
strings.Split(cmd_info[4], "|")) | ||
} | ||
|
||
//log.Println(len(cmds_GL)) | ||
//log.Println("===========") | ||
} | ||
|
||
func prepareCmdArray(cmd_info_GL *commandInfo, types_str []string, main_words_alt []string, | ||
main_words_ret_conds_str string, words_list_param []string) { | ||
var types_int []int = nil | ||
for _, j := range types_str { | ||
type_int, _ := strconv.Atoi(j) | ||
types_int = append(types_int, type_int) | ||
} | ||
|
||
///////////////////////////////// | ||
// Arrays processing | ||
|
||
// main_words | ||
for i, j := range types_str { | ||
if CMDi_TYPE_MANUAL == j { | ||
cmd_info_GL.main_words = append(cmd_info_GL.main_words, main_words_alt...) | ||
} else { | ||
cmd_info_GL.main_words = append(cmd_info_GL.main_words, cmds_types_keywords[types_int[i]][0]...) | ||
} | ||
} | ||
//log.Println(cmd_info_GL.main_words) | ||
|
||
// words_list | ||
// "device/phone safe mode|device/phone recovery|device/phone" | ||
var words_list [][][][]interface{} = nil | ||
for condition_str_num, condition_str := range words_list_param { | ||
words_list = append(words_list, nil) | ||
for ii, words_group := range strings.Split(condition_str, " ") { | ||
words_list[condition_str_num] = append(words_list[condition_str_num], nil) | ||
words_list[condition_str_num][ii] = append(words_list[condition_str_num][ii], []interface{}{-1}) | ||
words_list[condition_str_num][ii] = append(words_list[condition_str_num][ii], nil) | ||
for _, word := range strings.Split(words_group, "/") { | ||
words_list[condition_str_num][ii][1] = append(words_list[condition_str_num][ii][1], word) | ||
} | ||
} | ||
} | ||
for i, j := range types_str { | ||
switch j { | ||
case CMDi_TYPE_TURN_ONFF: | ||
{ | ||
words_list[0] = append(words_list[0], nil) | ||
var words_list_0_len int = len(words_list[0]) | ||
words_list[0][words_list_0_len-1] = append(words_list[0][words_list_0_len-1], []interface{}{-1}) | ||
words_list[0][words_list_0_len-1] = append(words_list[0][words_list_0_len-1], []interface{}{"on"}) | ||
|
||
words_list = append(words_list, nil) | ||
copySlice(&words_list[1], words_list[0]) | ||
words_list[1][words_list_0_len-1][1][0] = "off" | ||
} | ||
default: | ||
{ | ||
if 2 == len(cmds_types_keywords[types_int[i]]) { | ||
for ii := 0; ii < len(words_list); ii++ { | ||
words_list[ii] = append(words_list[ii], nil) | ||
var words_list_i_len int = len(words_list[ii]) | ||
words_list[ii][words_list_i_len-1] = append(words_list[ii][words_list_i_len-1], | ||
[]interface{}{-1}) | ||
words_list[ii][words_list_i_len-1] = append(words_list[ii][words_list_i_len-1], nil) | ||
for _, j := range cmds_types_keywords[types_int[i]][1] { | ||
words_list[ii][words_list_i_len-1][1] = append(words_list[ii][words_list_i_len-1][1], j) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
cmd_info_GL.words_list = words_list | ||
//log.Println(cmd_info_GL.words_list) | ||
|
||
// main_words_ret_conds | ||
if "" == main_words_ret_conds_str { | ||
var words_list_len int = len(cmd_info_GL.words_list) | ||
for i := 0; i < words_list_len; i++ { | ||
cmd_info_GL.main_words_ret_conds = append(cmd_info_GL.main_words_ret_conds, []string{ANY_MAIN_WORD}) | ||
} | ||
} else { | ||
for _, j := range strings.Split(main_words_ret_conds_str, "|") { | ||
cmd_info_GL.main_words_ret_conds = append(cmd_info_GL.main_words_ret_conds, strings.Split(j, "/")) | ||
} | ||
} | ||
//log.Println(cmd_info_GL.main_words_ret_conds) | ||
|
||
// exclude_word_found | ||
cmd_info_GL.exclude_word_found_group = append(cmd_info_GL.exclude_word_found_group, ALL_SUB_VERIFS_INT) | ||
|
||
//log.Println("---------") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2021 DADi590 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package AdvancedCommandsDetection | ||
|
||
//////////////////////////////////////////////////////////////////////////////////// | ||
// Constants of the various commands in the arrays below and in the other parts | ||
// | ||
// --- WARNING --- | ||
// The command ID 0 is reserved for function-related processing!!! (Want to know for what? Read the comment about | ||
// MARK_TERMINATION_FLOAT32 on TaskChecker.) All negative values are also reserved for special commands. | ||
// | ||
// Note: all RET_-started constants must be a float32 in a string which starts by the number on the corresponding | ||
// CMD_-started constant and must advance by increments of 0.01 (means 100 sub-commands at most). The first float can't | ||
// end in .0 (99 then, not 100). No reason in specific, it's just in case it's ever needed to use the main integer. So | ||
// start for example with 1.01 and 2.01. | ||
|
||
var cmds_GL []commandInfo = nil | ||
|
||
// commandInfo represents a command that this module detects. For use with wordsVerificationFunction() - check the | ||
// meaning of each attribute there. | ||
type commandInfo struct { | ||
// Positive (1+) integer | ||
cmd_id int | ||
main_words []string | ||
|
||
/* | ||
Example of how it could be for some commands: | ||
{ // 4 | ||
{";ANY;"}, | ||
{";ANY;"}, | ||
}, | ||
{ // 14 | ||
{";ANY;"}, | ||
{";ANY;"}, | ||
{"reboot restart"}, | ||
{";ANY;"}, | ||
}, | ||
*/ | ||
main_words_ret_conds [][]string | ||
|
||
/* | ||
Example of how it could be for some commands (it now includes the old conditions_continue and conditions_return): | ||
{ // 4 | ||
{{{-1}, {"on"}}, {{-1}, {"wifi", "wi-fi"}}}, | ||
{{{-1}, {"off"}}, {{-1}, {"wifi", "wi-fi"}}}, | ||
}, | ||
{ // 14 | ||
{{{-1}, {"device", "phone"}}, {{-1}, {"safe"}}, {{-1}, {"mode"}}}, | ||
{{{-1}, {"device", "phone"}}, {{-1}, {"recovery"}}}, | ||
{{{-1}, {"device", "phone"}}}, | ||
{{{-1}, {"device", "phone"}}}, | ||
}, | ||
{ // 16 | ||
{{{-1}, {NONE, "rear"}}, {{-1}, {"video"}}}, | ||
{{{-1}, {"frontal"}}, {{-1}, {"video"}}}, | ||
{{{-1}, {"audio"}}}, | ||
}, | ||
{ // 17 | ||
{{-1}, {{"again", "said", "say"}}}, | ||
}, | ||
{ // 19 | ||
{{{-1}, {"on"}}, {-1: {"battery", "power"}}, {{-1}, {"saver"}}}, | ||
{{{-1}, {"off"}}, {-1: {"battery", "power"}}, {{-1}, {"saver"}}}, | ||
}, | ||
*/ | ||
words_list [][][][]interface{} | ||
|
||
left_intervs map[int]int | ||
right_intervs map[int]int | ||
init_indexes_sub_verifs map[int]string | ||
exclude_word_found_group []int | ||
ignore_repets_cmds bool | ||
exclude_main_words bool | ||
exclude_mutually_exclusive_words bool | ||
} | ||
|
||
// Special WARN_-started commands returned by the sentenceCmdsDetector() - must not collide with spec_-started constants | ||
// on TaskChecker!!! | ||
|
||
// WARN_WHATS_IT is the constant that signals that an "it" was said but there seems to be nothing that it refers to, so | ||
// the assistant warns it didn't understand the meaning of the "it". | ||
const WARN_WHATS_IT string = "-10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.