Skip to content

Commit

Permalink
Print intents from recognized text.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Sep 20, 2019
1 parent 9f80b69 commit 5ac5cbf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 71 deletions.
133 changes: 66 additions & 67 deletions src/Interfaces/Victor.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,88 +115,87 @@ static void Main(string[] args)
}
#endregion

#region Methods
static void Recognize()
#region Methods
static void Recognize()
{
JuliusSession s = new JuliusSession();
if (!s.Initialized)
{
JuliusSession s = new JuliusSession();
if (!s.Initialized)
{
Error("Could not initialize Julius session.");
Exit(ExitResult.UNKNOWN_ERROR);
}
SnipsNLUEngine engine = new SnipsNLUEngine();
if (!engine.Initialized)
{
Error("Could not initialize SnipsNLU engine.");
Exit(ExitResult.UNKNOWN_ERROR);
}
s.Recognized += (text) =>
{
engine.GetIntents(text, out string intent, out string json, out string error);
if (!string.IsNullOrEmpty(intent))
{
Info("Intent: {0}", intent);
if (!string.IsNullOrEmpty(json))
{
Info("Slots: {0}", json);
}
}
};
s.Start();
s.WaitForExit();
Error("Could not initialize Julius session.");
Exit(ExitResult.UNKNOWN_ERROR);
}

private static void S_Recognized(string sentence)
SnipsNLUEngine engine = new SnipsNLUEngine();
if (!engine.Initialized)
{
throw new NotImplementedException();
Error("Could not initialize SnipsNLU engine.");
Exit(ExitResult.UNKNOWN_ERROR);
}

static void Exit(ExitResult result)
s.Recognized += (text) =>
{

if (Cts != null)
engine.GetIntents(text, out string[] intents, out string json, out string error);
if (intents.Length > 0)
{
Cts.Cancel();
Cts.Dispose();
Info("Intents: {0}", intents);
if (!string.IsNullOrEmpty(json))
{
Info("Slots: {0}", json);
}
}
};
s.Start();
s.WaitForExit();
}

Environment.Exit((int)result);
}
private static void S_Recognized(string sentence)
{
throw new NotImplementedException();
}

static int ExitWithCode(ExitResult result)
{
return (int)result;
}
static void Exit(ExitResult result)
{

static HelpText GetAutoBuiltHelpText(ParserResult<object> result)
if (Cts != null)
{
return HelpText.AutoBuild(result, h =>
{
h.AddOptions(result);
return h;
},
e =>
{
return e;
});
Cts.Cancel();
Cts.Dispose();
}
#endregion

#region Event Handlers
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Error((Exception)e.ExceptionObject, "Error occurred during operation. Victor CLI will shutdown.");
Exit(ExitResult.UNHANDLED_EXCEPTION);
}
Environment.Exit((int)result);
}

static int ExitWithCode(ExitResult result)
{
return (int)result;
}

private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
static HelpText GetAutoBuiltHelpText(ParserResult<object> result)
{
return HelpText.AutoBuild(result, h =>
{
Info("Ctrl-C pressed. Exiting.");
Cts.Cancel();
Exit(ExitResult.SUCCESS);
}
#endregion
h.AddOptions(result);
return h;
},
e =>
{
return e;
});
}
#endregion

#region Event Handlers
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Error((Exception)e.ExceptionObject, "Error occurred during operation. Victor CLI will shutdown.");
Exit(ExitResult.UNHANDLED_EXCEPTION);
}


private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Info("Ctrl-C pressed. Exiting.");
Cts.Cancel();
Exit(ExitResult.SUCCESS);
}
#endregion
}
}
13 changes: 9 additions & 4 deletions src/NLU/Snips/SnipsNLUEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ public SnipsNLUEngine() : this(Cts.Token) { }
#endregion

#region Methods
public void GetIntents(string input, out string intent, out string json, out string error)
public void GetIntents(string input, out string[] intents, out string json, out string error)
{
ThrowIfNotInitialized();
error = "";
intent = "";
intents = Array.Empty<string>();
json = "";
if (SnipsApi.GetIntents(EnginePtr, input, out libsnips.CIntentClassifierResult[] results))
if (SnipsApi.GetIntents(EnginePtr, input, out libsnips.CIntentClassifierResult[] results) && results.Count() > 0)
{
SnipsApi.GetSlotsIntoJson(EnginePtr, input, results.First().intent_name, out json, out error);
intents = results.Select(r => (string.IsNullOrEmpty(r.intent_name) ? "None" : r.intent_name) + ":" + r.confidence_score.ToString("N2")).ToArray();
var topIntent = results.OrderByDescending(r => r.confidence_score).First();
if (!string.IsNullOrEmpty(topIntent.intent_name))
{
SnipsApi.GetSlotsIntoJson(EnginePtr, input, topIntent.intent_name, out json, out error);
}
}
}
#endregion
Expand Down

0 comments on commit 5ac5cbf

Please sign in to comment.