Author Topic: Output all 'When I Say' phrase combinations  (Read 6404 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Output all 'When I Say' phrase combinations
« on: January 05, 2021, 11:14:56 AM »
UPDATE

Please check out Pfeil's post in this thread. The uploaded profile improves on the functionality I originally posted.

Cheers!



There may come a time when you have a command with a complex 'When I Say' trigger. For instance...

[hello;hi;goodbye][furry;fuzzy;fluffy;][squirrel;lion;rabbit;tiger]

...produces 48 phrase combinations.

Visualizing the possible combinations may become difficult as the 'When I Say' content grows in complexity. To help make this simpler, below is a C# inline function that will output:
  • A count of all the phrase combinations
  • The text for each phrase combination

Referenced Assemblies: System.dll
Code: [Select]
using System;

public class VAInline
{
public void main()
{
string phrases = VA.Command.WhenISay(); // Retrieve all content from command's 'When I Say' input box

string[] extractedPhrases = VA.Utility.ExtractPhrases(phrases); // Extract all 'When I Say' phrase combinations

VA.WriteToLog(extractedPhrases.Length + " phrases extracted from '" + phrases + "'", "pink"); // Output info to event log

foreach (string p in extractedPhrases) // Loop through each phrase in extractedPhrases
VA.WriteToLog(p, "gray"); // Output info to event log
}
}

Here are the steps to get this working:
  • Create a new command
  • Insert a C# inline function
  • Overwrite the inline function with the above content
  • Place whatever phrase content you want in the command's 'When I Say' input box
  • Save and execute the command (via voice, keyboard, whatever)

Note that any leading/trailing spaces will NOT be shown in the output. Feel free to read more about the 'ExtractPhrases' function in the VA manual.

Enjoy :)
« Last Edit: January 19, 2021, 10:40:13 AM by Exergist »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4647
  • RTFM
Re: Output all 'When I Say' phrase combinations
« Reply #1 on: January 11, 2021, 09:32:00 AM »
For quick prototyping, it may be useful to generate this list of phrases without needing to edit and save the command each time.

This command can be used (separate from the command you actually want to use those phrases for) to generate a list, either in the log on the main window, to a file, or directly to the clipboard for pasting, based on what you type into the dialog box that pops up when the command is executed (the contents are also remembered for the duration of the session, so you can make changes without having to re-type or paste):
[output;extract] phrases [;to file;to clipboard]
Code: [Select]
Get Text Input [phrasesToExtract]
Inline C# Function: Extract Phrases, wait until execution finishes
Begin Text Compare : [~extractedPhrases] Has Been Set
    Begin Text Compare : [{CMD}] Contains 'file'
        Write (overwrite), '{TXT:~extractedPhrases}' to file '%USERPROFILE%\Desktop\extractedPhrases.txt'
    Else
        Set Windows clipboard to '{TXT:~extractedPhrases}'
    End Condition
End Condition
(note that the path to the file is valid in most cases, but if you have moved the Desktop folder you'll want to adjust for that)

Where "Extract Phrases" contains:
Code: [Select]
using System;

public class VAInline
{
public void main()
{
string phrases = VA.GetText("phrasesToExtract"); // Retrieve what was entered into the popup dialog box

try
{
string[] extractedPhrases = VA.Utility.ExtractPhrases(phrases); // Extract all 'When I Say' phrase combinations

VA.WriteToLog(extractedPhrases.Length + " phrases extracted from '" + phrases + "'", "pink"); // Output info to event log

if (VA.Command.Action() == "Spoken" && !VA.Command.Name().EndsWith("phrases"))
{
VA.SetText("~extractedPhrases", String.Join("\r\n", extractedPhrases));
}
else
{
foreach (string p in extractedPhrases) // Loop through each phrase in extractedPhrases
{
VA.WriteToLog(p, "gray"); // Output info to event log
}
}
}
catch(Exception ex)
{
VA.WriteToLog("Extracting phrases failed: " + ex.Message, "red");
if (ex.Message.Contains("Memory"))
{
VA.WriteToLog("The system ran out of memory trying to extrapolate all phrases;");
VA.WriteToLog("Try reducing the number of phrase variations");
}
}
}
}
« Last Edit: January 19, 2021, 09:21:28 AM by Pfeil »

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Output all 'When I Say' phrase combinations
« Reply #2 on: January 19, 2021, 10:41:37 AM »
Very nice improvements Pfeil!