0

I have created srgs file for semantic recognition, now i want to udate myGrammar file,Now How to i update my_Grammar.xml file and add more cities in item tag from textbox. helping material regarding this will be appreciated and thanks in advance.

     <grammar version="1.0" xml:lang="en-US" mode="voice" root="destination" 
            xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
          <rule id="Source">
               <one-of>
              <item> Karachi </item>
              <item> Lahore </item>
              <item> Abbottabad </item>
 <item> Murree </item>
            </one-of>
          </rule>
 <rule id="destination">
               <one-of>
              <item> Karachi </item>
              <item> Lahore </item>
              <item> Islamabad </item>
            </one-of>
          </rule>
 <rule id="Article">
               <one-of>
              <item> to</item>
             </one-of>
          </rule>
        </grammar>

          SrgsRule SrcRule = new SrgsRule("id_Source");
          SrgsOneOf SrcList = new SrgsOneOf(new string[] { "Lahore","Karachi",Abbottabad ,"Murree"});
          SrcRule.Add(SrcList);

          SrgsRule ArticleRule = new SrgsRule("id_Article");
          SrgsOneOf ArticleList = new SrgsOneOf(new string[] { "to" });
          ArticleRule.Add(ArticleList);

          SrgsRule desRule = new SrgsRule("id_Destination");
          SrgsOneOf desList = new SrgsOneOf(new string[] { "Islamabad","Lahore","Karachi",Abbottabad ,"Murree"});
          desRule.Add(desList);

          SrgsRule rootRule = new SrgsRule("Src_Article_des");
          rootRule.Scope = SrgsRuleScope.Public;

          SrgsRuleRef SrcRef = new SrgsRuleRef(SrcRule, "theSource");
          rootRule.Add(SrcRef);

          SrgsRuleRef ArticleRef = new SrgsRuleRef(ArticleRule, "theArticle");
          rootRule.Add(ArticleRef);

          SrgsRuleRef desRef = new SrgsRuleRef(desRule, "theDestination");
          rootRule.Add(desRef);

          SrgsDocument document = new SrgsDocument();
          document.Rules.Add(new SrgsRule[] { rootRule, SrcRule, ArticleRule, desRule });        
          document.Root = rootRule;
          Grammar g = new Grammar(document, "Src_Article_des");
          sr.LoadGrammar(g);
          System.Xml.XmlWriter writer =
            System.Xml.XmlWriter.Create("c:\\test\\myGrammar.xml");
          document.WriteSrgs(writer);
          writer.Close();
  • Posting irrelevant code won't prevent closing the question as unclear. SRGS is just XML and can be easily modified at runtime. Did you try that? Did you have a specific problem? Have you tried something? Are you asking for something else perhaps, eg how to replace the currently executing grammar with a new one? – Panagiotis Kanavos Jun 01 '15 at 08:25
  • yeah right. i want to replace currently executing grammar with a new one. how to do this ? – Taimoor Hassan Jun 01 '15 at 09:49
  • Repost the question asking what you really want, with an appropriate title, tags and the *relevant* code. The title will not attract people that know about SRGS. This question has acquired so many close votes that it's unlikely to be answered. Make sure you include what you've already tried. – Panagiotis Kanavos Jun 01 '15 at 09:56

4 Answers4

1
  public void AddItemToRule(string inFile, string outFile, string ruleId, string itemVal)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(inFile); // loads xml file
            if (AddItemToRule(xmlDoc, ruleId, item))
            {
                xmlDoc.Save(outFile);
                MessageBox.Show(@"Inserted Successfully");
            }
        }

        public bool AddItemToRule(XmlDocument xmlDoc, string ruleId, string itemVal)
        {
            string ns = "http://www.w3.org/2001/06/grammar";
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsMgr.AddNamespace("g", ns);
            System.Diagnostics.Debug.WriteLine(nsMgr.DefaultNamespace);
            XmlNode foundNode = xmlDoc.SelectSingleNode("//g:rule[@id='" + ruleId + "']/g:one-of", nsMgr);
            if (foundNode != null)
            {
                XmlElement eleItem = xmlDoc.CreateElement("item");


                var text = xmlDoc.CreateTextNode(item);
                eleItem.AppendChild(text);

                foundNode.AppendChild(eleItem);
                return true;
            }
            return false;
        }

     AddItemToRule(path, path, "id_Source", itemValue);
     AddItemToRule(path, path, "id_Article", itemValue);
     AddItemToRule(path, path, "id_Destination", itemValue);
0

Try this. I used XML Linq, but you can also do same using straight XML.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication32
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
              "<one-of>" +
                  "<item>" + 
        //add item dynamically. // i want to add item here
                  "</item>" +
                  "<item> " +
       //add new items.// i want to add item here
                  "</item>" +
              "</one-of>";

            XDocument doc = XDocument.Parse(input);

            List<XElement> items = doc.Descendants().Where(x => x.Name == "item").ToList();
            foreach (XElement item in items)
            {
                XElement newElement = new XElement("newItem", "i want to add item here");
                item.Add(newElement);
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Also, you can create simple grxml file (not use programming way) and then just update a file by adds new elements.

user2545071
  • 1,408
  • 2
  • 24
  • 46
0

Okay, I know this is a bit late, but as I didn't found any proper answer to this question, I wanted to drop my solution, using the SrgsDocument class:

public void UpdateVoiceCommand(string ruleId, SrgsRule updatedRule, string grammarFileName)
{
    SrgsDocument grammarXmlDoc = new SrgsDocument(grammarFileName);

    SrgsRulesCollection rules;
    rules = grammarXmlDoc.Rules;

    if (rules.Contains(ruleId))
        rules.Remove(ruleId);   //Remove old grammar rule
    rules.Add(newRule);     //Add replacement rule

    saveSrgsDocument(GrammarFileName, grammarXmlDoc);
}

This will place the new rule at the end of the grammar file, but by using the Insert method, you can put it anywhere you want:

    if (rules.Contains(ruleId))          //First check if the rule exists
    {
        SrgsRule oldRule = rules[ruleId];    //Get the old rule
        int index = rules.IndexOf(oldRule);  //Find it's position
        rules.Remove(ruleId);                //Remove it
        rules.Insert(index, newRule);        //Insert new rule at the position of the old rule
    }
    else
        rules.Add(newRule);                  //Append a new rule at the end
Louk
  • 1
  • 2