本文介绍了如何在C#中更新srgs语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了用于语义识别的srgs文件,现在我想对myGrammar文件进行审核,现在如何更新my_Grammar.xml文件并在文本框中的项目标签中添加更多城市.有关的帮助材料将不胜感激,并在此先感谢.
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();
推荐答案
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);
这篇关于如何在C#中更新srgs语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!