<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>VoiceBot RSS: [Script] Generic script for recognition, after launch.</title>
<atom:link href="https://www.voicebot.net/Discussions/RSS/?TopicID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340" rel="self" type="application/rss+xml" />
<link>https://www.voicebot.net/Discussions/RSS/?TopicID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340</link>
<description>VoiceBot RSS: [Script] Generic script for recognition, after launch.</description>
<lastBuildDate>Thu, 17 Sep 2026 04:27:04 GMT</lastBuildDate>
<language>en</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>https://www.voicebot.net/Discussions/RSS/?TopicID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340</generator>
<item>
<title>RE: [Script] Generic script for recognition, after launch.</title>
<link>https://www.voicebot.net/Discussions/View/script-generic-script-for-recognition-after-launch/?ID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340#2</link>
<pubDate>Tue, 15 Aug 2017 19:40:37 GMT</pubDate>
<dc:creator>Binary Fortress Software</dc:creator>
<guid isPermaLink="false">https://www.voicebot.net/Discussions/View/script-generic-script-for-recognition-after-launch/?ID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340#2</guid>
<category>VoiceBot</category>
<description><![CDATA[Thanks for sharing this! We've posted it up to the repository]]></description>
<content:encoded><![CDATA[<div class="CTDiscussions">
Thanks for sharing this! We've posted it up to the repository <img src="https://www.voicebot.net/MediaCommon/SVGs/FontAwesome/face-smile.light.svg" alt=":)" style="box-sizing:border-box;position:relative;overflow:hidden;vertical-align:middle !important;width:16px;height:16px;" HelpButtonData=":)" />
</div>
]]></content:encoded>
</item>
<item>
<title>[Script] Generic script for recognition, after launch.</title>
<link>https://www.voicebot.net/Discussions/View/script-generic-script-for-recognition-after-launch/?ID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340</link>
<pubDate>Sat, 29 Jul 2017 12:02:27 GMT</pubDate>
<dc:creator>Binary Fortress Software</dc:creator>
<guid isPermaLink="false">https://www.voicebot.net/Discussions/View/script-generic-script-for-recognition-after-launch/?ID=da5fd5f7-0f6c-4d26-9fd1-75a7a59ea340</guid>
<category>VoiceBot</category>
<description><![CDATA[This script allows to recognize a speech after it has started. That can be used in a variety of situations, such as adjusting the volume of the sound, adjusting the brightness of the monitor, making any selection from two and more options, and many more.
Code
Copy
Select All
using System;&#xA...]]></description>
<content:encoded><![CDATA[<div class="CTDiscussions">
This script allows to recognize a speech after it has started. That can be used in a variety of situations, such as adjusting the volume of the sound, adjusting the brightness of the monitor, making any selection from two and more options, and many more.<br/>
<br/>
<div class="col-md-12 BoxWrap"><div class="Box table-responsive"><a name="code" style="width:0; height:0;"></a><h2 class="TableTitle" style="border:0"><div class="TableTitleText">Code</div><div class="TitleButtons"><div class="TableTitleButton"><a href="#" onclick="return false;" data-clipboard-target="#code01a0ad9e3dfe763ab73c5a87ec51917c" class="ClipboardCopyControl"><img src="https://www.voicebot.net/MediaCommon/SVGs/FontAwesome/clone.blue.svg" style="box-sizing:border-box;position:relative;overflow:hidden;width:auto;max-width:16px;height:16px;" /><span class="Text">Copy</span></a></div><div class="TableTitleButton"><a href="#" onclick="bfs.util.codeEditorSelectAll('code01a0ad9e3dfe763ab73c5a87ec51917cJs'); return false;"><img src="https://www.voicebot.net/MediaCommon/SVGs/FontAwesome/square-check.blue.svg" style="box-sizing:border-box;position:relative;overflow:hidden;width:auto;max-width:16px;height:16px;" /><span class="Text">Select All</span></a></div></div></h2><div class="TableTitleContent table-responsive"><div class="AceEditorWrapper" style="border-top:solid 1px var(--color-default-border);padding:0"><pre id="code01a0ad9e3dfe763ab73c5a87ec51917cJs" name="code01a0ad9e3dfe763ab73c5a87ec51917cJs" contenteditable="true" spellcheck="true" class="skiptranslate" style="width:100%;min-height:75px">using System;&#xA;using System.Collections.Generic;&#xA;using System.Globalization;&#xA;using System.IO;&#xA;using System.Linq;&#xA;using System.Speech.Recognition;&#xA;using System.Speech.Recognition.SrgsGrammar;&#xA;using System.Xml;&#xA;using System.Xml.Linq;&#xA;&#xA;// Add to references&#xA;// | System.Xml.Linq.dll | System.Core.dll | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.Speech.dll&#xA;&#xA;public static class VoiceBotScript&#xA;{&#xA;    public static void Run()&#xA;    {&#xA;        // Depending on the type you want, set the return type. E.g. &amp;lt;string&amp;gt; or &amp;lt;float&amp;gt; or &amp;lt;int&amp;gt; or other.&#xA;        var recogResult = SrHelper&amp;lt;int&amp;gt;();&#xA;        &#xA;        // Show\Say what recognize. Need for feedback. Can be removed.&#xA;        ///BFS.Dialog.ShowMessageInfo(recogResult.ToString(CultureInfo.InvariantCulture));&#xA;        // or&#xA;        BFS.Speech.TextToSpeech(string.Format(&quot;I recognized {0}&quot;, recogResult));&#xA;&#xA;        // your code goes here&#xA;    }&#xA;&#xA;    private static T SrHelper&amp;lt;T&amp;gt;()&#xA;        where T : IConvertible&#xA;    {&#xA;        // Set Path to file&#xA;        var pathToFile = @&quot;C:\VBotFiles\Recognizer\&quot;;&#xA;&#xA;        // Set file name&#xA;        var fileName = &quot;Volume&quot;;&#xA;&#xA;        // Set your recognition language. Support languages: en-GB, en-US, de-DE, es-ES, fr-FR, ja-JP, zh-CN, zh-TW.&#xA;        // Selected language pack must be installed.&#xA;        var culture = new CultureInfo(&quot;en-US&quot;);&#xA;        &#xA;        // Check whether the selected culture is installed for selected speech recognition.&#xA;        if (!TestInstalledLanguages.TestLanguages(culture))&#xA;            return (T)Convert.ChangeType(null, typeof(T));&#xA;        &#xA;        // If the recognition does not recognize the first time set the flag to true&#xA;        // and set the number of recognition tries&#xA;        var flag = false;&#xA;        var maxTries = 3;&#xA;            &#xA;        // Set the range&#xA;        var rangeStart = 0;&#xA;        var rangeCount = 100;&#xA;&#xA;        // Create list with range 0-100. Ex. for set volume in audio app.&#xA;        ///var valList = Enumerable.Range(rangeStart, rangeCount).ToList();&#xA;&#xA;        // or create list with range 0-10-20...100&#xA;        var valList = Enumerable.Range(rangeStart, rangeCount &#x2B; 1).Where(x =&amp;gt; x % 10 == 0).ToList();&#xA;&#xA;        // or create string list&#xA;        ///var valList = new List&amp;lt;string&amp;gt;&#xA;        ///{&#xA;        ///     &quot;Add&quot;, &quot;the&quot;, &quot;recognition&quot;, &quot;commands&quot;, &quot;you&quot;, &quot;want&quot;&#xA;        ///};&#xA;&#xA;        var ext = &quot;.xml&quot;;&#xA;        var fullFileName = fileName &#x2B; ext;&#xA;        &#xA;        // Open\save srgs file&#xA;        var srgsFile = SrgsDocumentCreator.GetSrgsFile(pathToFile, fileName, fullFileName, culture, valList);&#xA;&#xA;        // Get recognition result&#xA;        var result = SpeechRecognition.Recognition(srgsFile, culture, flag, maxTries);&#xA;        &#xA;        // Return a nullable type not default(T)&#xA;        if (string.IsNullOrEmpty(result))&#xA;            return (T)Convert.ChangeType(null, typeof(T));&#xA;&#xA;        return (T)Convert.ChangeType(result, typeof(T));&#xA;    }&#xA;}&#xA;&#xA;// Speech recognition. Initialize SpeechRecognitionEngine with selected language and return recognition result.&#xA;internal class SpeechRecognition&#xA;{&#xA;    public static string Recognition(string srgsFile, CultureInfo culture, bool flag, int maxTries)&#xA;    {&#xA;        RecognitionResult result;&#xA;        var recogResult = string.Empty;&#xA;        var sre = new SpeechRecognitionEngine(culture);&#xA;        var grammar = new Grammar(srgsFile);&#xA;        var tries = 0;&#xA;&#xA;        sre.LoadGrammar(grammar);&#xA;        try&#xA;        {&#xA;            sre.SetInputToDefaultAudioDevice();&#xA;            &#xA;            if(flag == false)&#xA;            {&#xA;                result = sre.Recognize();&#xA;                &#xA;                if (result != null)&#xA;                    recogResult = result.Text;&#xA;                else&#xA;                    BFS.Speech.TextToSpeech(&quot;I can&#x27;t recognize what you say.&quot;);&#xA;            }&#xA;        &#xA;            // If the recognition hasn&#x27;t been successful the first time, try more.&#xA;            do&#xA;            {&#xA;                result = sre.Recognize();&#xA;                &#xA;                if (result != null)&#xA;                {&#xA;                    recogResult = result.Text;&#xA;                    break;&#xA;                    &#xA;                }&#xA;                tries&#x2B;&#x2B;;&#xA;                BFS.Speech.TextToSpeech(&quot;I can&#x27;t recognize what you say. Please try again&quot;);&#xA;                continue;&#xA;            }&#xA;            while (tries &amp;lt; maxTries);&#xA;        }&#xA;&#xA;        catch (Exception ex)&#xA;        {&#xA;            BFS.Dialog.ShowMessageError(ex.Message);&#xA;        }&#xA;&#xA;        finally&#xA;        {&#xA;            sre.UnloadGrammar(grammar);&#xA;        }&#xA;&#xA;        return recogResult;&#xA;    }&#xA;}&#xA;&#xA;// Create Srgs file&#xA;internal class SrgsDocumentCreator&#xA;{&#xA;    public static string GetSrgsFile&amp;lt;T&amp;gt;(string pathToFile, string fileName, string fullFileName, CultureInfo culture, List&amp;lt;T&amp;gt; valList)&#xA;    {&#xA;        // Check directory exist&#xA;        if (!Directory.Exists(pathToFile))&#xA;            Directory.CreateDirectory(pathToFile);&#xA;&#xA;        var srgsFile = Path.Combine(pathToFile, fullFileName);&#xA;&#xA;        // Check file exist and xml:lang value equals selected culture&#xA;        if (File.Exists(srgsFile))&#xA;        {&#xA;            var xDoc = XDocument.Load(srgsFile);&#xA;            if (xDoc.Root != null &amp;&amp; xDoc.Root.FirstAttribute.Value == culture.Name) &#xA;                return srgsFile;&#xA;        }&#xA;&#xA;        // Create SrgsOneOf&#xA;        var values = new SrgsOneOf();&#xA;&#xA;        // Add SrgsItem to SrgsOneOf&#xA;        foreach (var val in valList)&#xA;            values.Add(new SrgsItem(val.ToString()));&#xA;&#xA;        // Create root rule&#xA;        var ruleVolume = new SrgsRule(fileName, values) {Scope = SrgsRuleScope.Public};&#xA;&#xA;        // Create SrgsDocument with selected culture&#xA;        var srgsDoc = new SrgsDocument {Culture = culture};&#xA;&#xA;        // Add rules to document&#xA;        srgsDoc.Rules.Add(ruleVolume);&#xA;&#xA;        // Set document root&#xA;        srgsDoc.Root = ruleVolume;&#xA;&#xA;        // Save created srgsDoc to xml file&#xA;        var xmlWriter = XmlWriter.Create(srgsFile);&#xA;        srgsDoc.WriteSrgs(xmlWriter);&#xA;        xmlWriter.Close();&#xA;&#xA;        return srgsFile;&#xA;    }&#xA;}&#xA;&#xA;internal class TestInstalledLanguages&#xA;{&#xA;    public static bool TestLanguages(CultureInfo culture)&#xA;    {&#xA;        // Compare the selected speech recognition language with the installed&#xA;        var info = SpeechRecognitionEngine.InstalledRecognizers()&#xA;            .FirstOrDefault(x =&amp;gt; string.Equals(x.Culture.Name, culture.Name, StringComparison.InvariantCultureIgnoreCase));&#xA;&#xA;        if (info != null)&#xA;            return true;&#xA;        BFS.Dialog.ShowMessageError(string.Format(&quot;The selected language \&quot;{0}\&quot; is not installed&quot;, culture.Name));&#xA;        return false;&#xA;    }&#xA;}</pre><textarea id="code01a0ad9e3dfe763ab73c5a87ec51917c" name="code01a0ad9e3dfe763ab73c5a87ec51917c" style="position:absolute;top:0;left:-999999px;width:1px;height:1px"></textarea></div>
</div></div></div>
</div>
]]></content:encoded>
</item>
</channel>
</rss>