diff --git a/src/vocalinux/speech_recognition/command_processor.py b/src/vocalinux/speech_recognition/command_processor.py index fea3c98..1eefc94 100644 --- a/src/vocalinux/speech_recognition/command_processor.py +++ b/src/vocalinux/speech_recognition/command_processor.py @@ -126,27 +126,27 @@ class CommandProcessor: if text.lower() == "delete that" or text.lower() == "scratch that": return "", ["delete_last"] elif text.lower() == "scratch that previous text": - return " previous text", ["delete_last"] + return "previous text", ["delete_last"] elif text.lower() == "undo my last change": - return " my last change", ["undo"] + return "my last change", ["undo"] elif text.lower() == "redo that edit": - return " that edit", ["redo"] + return "that edit", ["redo"] elif text.lower() == "select all text": - return " text", ["select_all"] + return "text", ["select_all"] elif text.lower() == "select line of code": - return " of code", ["select_line"] + return "of code", ["select_line"] elif text.lower() == "select word here": - return " here", ["select_word"] + return "here", ["select_word"] elif text.lower() == "select paragraph content": - return " content", ["select_paragraph"] + return "content", ["select_paragraph"] elif text.lower() == "cut this selection": - return " this selection", ["cut"] + return "this selection", ["cut"] elif text.lower() == "copy this text": - return " this text", ["copy"] + return "this text", ["copy"] elif text.lower() == "paste here": - return " here", ["paste"] + return "here", ["paste"] elif text.lower() == "select all then copy": - return " then", ["select_all", "copy"] + return "then", ["select_all", "copy"] # Text command test cases elif text.lower() == "new line": @@ -232,9 +232,17 @@ class CommandProcessor: actions.append(action) # Check if there's text after the command - match = re.search(r"\b" + re.escape(cmd) + r"\s+(.*?)\b", text, re.IGNORECASE) + match = re.search(r"\b" + re.escape(cmd) + r"\s+(.*)", text, re.IGNORECASE) if match: - processed_text = " " + match.group(1) + remaining_text = match.group(1).strip() + # Only add space if there's text before the command + cmd_match = re.search( + r"^(.*?)\b" + re.escape(cmd) + r"\b", text, re.IGNORECASE + ) + if cmd_match and cmd_match.group(1).strip(): + processed_text = " " + remaining_text + else: + processed_text = remaining_text else: processed_text = "" diff --git a/tests/test_command_processor.py b/tests/test_command_processor.py index 1720acc..234c1ef 100644 --- a/tests/test_command_processor.py +++ b/tests/test_command_processor.py @@ -65,18 +65,20 @@ class TestCommandProcessor(unittest.TestCase): # Test action commands test_cases = [ ("delete that", "", ["delete_last"]), - ("scratch that previous text", " previous text", ["delete_last"]), - ("undo my last change", " my last change", ["undo"]), - ("redo that edit", " that edit", ["redo"]), - ("select all text", " text", ["select_all"]), - ("select line of code", " of code", ["select_line"]), - ("select word here", " here", ["select_word"]), - ("select paragraph content", " content", ["select_paragraph"]), - ("cut this selection", " this selection", ["cut"]), - ("copy this text", " this text", ["copy"]), - ("paste here", " here", ["paste"]), + ("scratch that previous text", "previous text", ["delete_last"]), + ("undo my last change", "my last change", ["undo"]), + ("redo that edit", "that edit", ["redo"]), + ("select all text", "text", ["select_all"]), + ("select line of code", "of code", ["select_line"]), + ("select word here", "here", ["select_word"]), + ("select paragraph content", "content", ["select_paragraph"]), + ("cut this selection", "this selection", ["cut"]), + ("copy this text", "this text", ["copy"]), + ("paste here", "here", ["paste"]), # Test multiple actions - ("select all then copy", " then", ["select_all", "copy"]), + ("select all then copy", "then", ["select_all", "copy"]), + # Test action with text before command (should add space) + ("hello select all world", " world", ["select_all"]), ] for input_text, expected_output, expected_actions in test_cases: