PageRenderTime 373ms CodeModel.GetById 18ms RepoModel.GetById 2ms app.codeStats 0ms

/BETA/ClassLibrary1/AIMLbot/Utils/AIMLLoader.cs

https://bitbucket.org/VahidN/aimlbot4glider
C# | 350 lines | 223 code | 32 blank | 95 comment | 29 complexity | 40adeb81861dfa7615864c6e197976a3 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.IO;
  5. using System.Text;
  6. namespace AIMLbot.Utils
  7. {
  8. /// <summary>
  9. /// A utility class for loading AIML files from disk into the graphmaster structure that
  10. /// forms an AIML bot's "brain"
  11. /// </summary>
  12. public class AIMLLoader
  13. {
  14. #region Attributes
  15. /// <summary>
  16. /// The bot whose brain is being processed
  17. /// </summary>
  18. private AIMLbot.Bot bot;
  19. #endregion
  20. /// <summary>
  21. /// Ctor
  22. /// </summary>
  23. /// <param name="bot">The bot whose brain is being processed</param>
  24. public AIMLLoader(AIMLbot.Bot bot)
  25. {
  26. this.bot = bot;
  27. }
  28. #region Methods
  29. /// <summary>
  30. /// Loads the AIML from files found in the bot's AIMLpath into the bot's brain
  31. /// </summary>
  32. public void loadAIML()
  33. {
  34. this.loadAIML(this.bot.PathToAIML);
  35. }
  36. /// <summary>
  37. /// Loads the AIML from files found in the path
  38. /// </summary>
  39. /// <param name="path"></param>
  40. public void loadAIML(string path)
  41. {
  42. if (Directory.Exists(path))
  43. {
  44. // process the AIML
  45. this.bot.writeToLog("Starting to process AIML files found in the directory " + path);
  46. string[] fileEntries = Directory.GetFiles(path, "*.aiml");
  47. if (fileEntries.Length > 0)
  48. {
  49. foreach (string filename in fileEntries)
  50. {
  51. this.loadAIMLFile(filename);
  52. }
  53. this.bot.writeToLog("Finished processing the AIML files. " + Convert.ToString(this.bot.Size) + " categories processed.");
  54. }
  55. else
  56. {
  57. throw new FileNotFoundException("Could not find any .aiml files in the specified directory (" + path + "). Please make sure that your aiml file end in a lowercase aiml extension, for example - myFile.aiml is valid but myFile.AIML is not.");
  58. }
  59. }
  60. else
  61. {
  62. throw new FileNotFoundException("The directory specified as the path to the AIML files (" + path + ") cannot be found by the AIMLLoader object. Please make sure the directory where you think the AIML files are to be found is the same as the directory specified in the settings file.");
  63. }
  64. }
  65. /// <summary>
  66. /// Given the name of a file in the AIML path directory, attempts to load it into the
  67. /// graphmaster
  68. /// </summary>
  69. /// <param name="filename">The name of the file to process</param>
  70. public void loadAIMLFile(string filename)
  71. {
  72. this.bot.writeToLog("Processing AIML file: " + filename);
  73. // load the document
  74. XmlDocument doc = new XmlDocument();
  75. doc.Load(filename);
  76. this.loadAIMLFromXML(doc, filename);
  77. }
  78. /// <summary>
  79. /// Given an XML document containing valid AIML, attempts to load it into the graphmaster
  80. /// </summary>
  81. /// <param name="doc">The XML document containing the AIML</param>
  82. /// <param name="filename">Where the XML document originated</param>
  83. public void loadAIMLFromXML(XmlDocument doc, string filename)
  84. {
  85. // Get a list of the nodes that are children of the <aiml> tag
  86. // these nodes should only be either <topic> or <category>
  87. // the <topic> nodes will contain more <category> nodes
  88. XmlNodeList rootChildren = doc.DocumentElement.ChildNodes;
  89. // process each of these child nodes
  90. foreach (XmlNode currentNode in rootChildren)
  91. {
  92. if (currentNode.Name == "topic")
  93. {
  94. this.processTopic(currentNode, filename);
  95. }
  96. else if (currentNode.Name == "category")
  97. {
  98. this.processCategory(currentNode, filename);
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Given a "topic" node, processes all the categories for the topic and adds them to the
  104. /// graphmaster "brain"
  105. /// </summary>
  106. /// <param name="node">the "topic" node</param>
  107. /// <param name="filename">the file from which this node is taken</param>
  108. private void processTopic(XmlNode node, string filename)
  109. {
  110. // find the name of the topic or set to default "*"
  111. string topicName="*";
  112. if((node.Attributes.Count==1)&(node.Attributes[0].Name=="name"))
  113. {
  114. topicName = node.Attributes["name"].Value;
  115. }
  116. // process all the category nodes
  117. foreach (XmlNode thisNode in node.ChildNodes)
  118. {
  119. if (thisNode.Name == "category")
  120. {
  121. processCategory(thisNode, topicName, filename);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Adds a category to the graphmaster structure using the default topic ("*")
  127. /// </summary>
  128. /// <param name="node">the XML node containing the category</param>
  129. /// <param name="filename">the file from which this category was taken</param>
  130. private void processCategory(XmlNode node, string filename)
  131. {
  132. this.processCategory(node, "*", filename);
  133. }
  134. /// <summary>
  135. /// Adds a category to the graphmaster structure using the given topic
  136. /// </summary>
  137. /// <param name="node">the XML node containing the category</param>
  138. /// <param name="topicName">the topic to be used</param>
  139. /// <param name="filename">the file from which this category was taken</param>
  140. private void processCategory(XmlNode node, string topicName, string filename)
  141. {
  142. // reference and check the required nodes
  143. XmlNode pattern = this.FindNode("pattern", node);
  144. XmlNode template = this.FindNode("template", node);
  145. if (object.Equals(null, pattern))
  146. {
  147. throw new XmlException("Missing pattern tag in a node found in " + filename);
  148. }
  149. if (object.Equals(null, template))
  150. {
  151. throw new XmlException("Missing template tag in the node with pattern: " + pattern.InnerText + " found in " + filename);
  152. }
  153. string categoryPath = this.generatePath(node, topicName, false);
  154. // o.k., add the processed AIML to the GraphMaster structure
  155. if (categoryPath.Length > 0)
  156. {
  157. try
  158. {
  159. this.bot.Graphmaster.addCategory(categoryPath, template.OuterXml, filename);
  160. // keep count of the number of categories that have been processed
  161. this.bot.Size++;
  162. }
  163. catch
  164. {
  165. this.bot.writeToLog("ERROR! Failed to load a new category into the graphmaster where the path = " + categoryPath + " and template = " + template.OuterXml + " produced by a category in the file: " + filename);
  166. }
  167. }
  168. else
  169. {
  170. this.bot.writeToLog("WARNING! Attempted to load a new category with an empty pattern where the path = " + categoryPath + " and template = " + template.OuterXml + " produced by a category in the file: " + filename);
  171. }
  172. }
  173. /// <summary>
  174. /// Generates a path from a category XML node and topic name
  175. /// </summary>
  176. /// <param name="node">the category XML node</param>
  177. /// <param name="topicName">the topic</param>
  178. /// <param name="isUserInput">marks the path to be created as originating from user input - so
  179. /// normalize out the * and _ wildcards used by AIML</param>
  180. /// <returns>The appropriately processed path</returns>
  181. public string generatePath(XmlNode node, string topicName, bool isUserInput)
  182. {
  183. // get the nodes that we need
  184. XmlNode pattern = this.FindNode("pattern", node);
  185. XmlNode that = this.FindNode("that", node);
  186. string patternText;
  187. string thatText = "*";
  188. if (object.Equals(null, pattern))
  189. {
  190. patternText = string.Empty;
  191. }
  192. else
  193. {
  194. patternText = pattern.InnerText;
  195. }
  196. if (!object.Equals(null, that))
  197. {
  198. thatText = that.InnerText;
  199. }
  200. return this.generatePath(patternText, thatText, topicName, isUserInput);
  201. }
  202. /// <summary>
  203. /// Given a name will try to find a node named "name" in the childnodes or return null
  204. /// </summary>
  205. /// <param name="name">The name of the node</param>
  206. /// <param name="node">The node whose children need searching</param>
  207. /// <returns>The node (or null)</returns>
  208. private XmlNode FindNode(string name, XmlNode node)
  209. {
  210. foreach(XmlNode child in node.ChildNodes)
  211. {
  212. if (child.Name == name)
  213. {
  214. return child;
  215. }
  216. }
  217. return null;
  218. }
  219. /// <summary>
  220. /// Generates a path from the passed arguments
  221. /// </summary>
  222. /// <param name="pattern">the pattern</param>
  223. /// <param name="that">the that</param>
  224. /// <param name="topicName">the topic</param>
  225. /// <param name="isUserInput">marks the path to be created as originating from user input - so
  226. /// normalize out the * and _ wildcards used by AIML</param>
  227. /// <returns>The appropriately processed path</returns>
  228. public string generatePath(string pattern, string that, string topicName, bool isUserInput)
  229. {
  230. // to hold the normalized path to be entered into the graphmaster
  231. StringBuilder normalizedPath = new StringBuilder();
  232. string normalizedPattern = string.Empty;
  233. string normalizedThat = "*";
  234. string normalizedTopic = "*";
  235. if ((this.bot.TrustAIML)&(!isUserInput))
  236. {
  237. normalizedPattern = pattern.Trim();
  238. normalizedThat = that.Trim();
  239. normalizedTopic = topicName.Trim();
  240. }
  241. else
  242. {
  243. normalizedPattern = this.Normalize(pattern, isUserInput).Trim();
  244. normalizedThat = this.Normalize(that, isUserInput).Trim();
  245. normalizedTopic = this.Normalize(topicName, isUserInput).Trim();
  246. }
  247. // check sizes
  248. if (normalizedPattern.Length > 0)
  249. {
  250. if (normalizedThat.Length == 0)
  251. {
  252. normalizedThat = "*";
  253. }
  254. if (normalizedTopic.Length == 0)
  255. {
  256. normalizedTopic = "*";
  257. }
  258. // This check is in place to avoid huge "that" elements having to be processed by the
  259. // graphmaster.
  260. if (normalizedThat.Length > this.bot.MaxThatSize)
  261. {
  262. normalizedThat = "*";
  263. }
  264. // o.k. build the path
  265. normalizedPath.Append(normalizedPattern);
  266. normalizedPath.Append(" <that> ");
  267. normalizedPath.Append(normalizedThat);
  268. normalizedPath.Append(" <topic> ");
  269. normalizedPath.Append(normalizedTopic);
  270. return normalizedPath.ToString();
  271. }
  272. else
  273. {
  274. return string.Empty;
  275. }
  276. }
  277. /// <summary>
  278. /// Given an input, provide a normalized output
  279. /// </summary>
  280. /// <param name="input">The string to be normalized</param>
  281. /// <param name="isUserInput">True if the string being normalized is part of the user input path -
  282. /// flags that we need to normalize out * and _ chars</param>
  283. /// <returns>The normalized string</returns>
  284. public string Normalize(string input, bool isUserInput)
  285. {
  286. StringBuilder result = new StringBuilder();
  287. // objects for normalization of the input
  288. Normalize.ApplySubstitutions substitutor = new AIMLbot.Normalize.ApplySubstitutions(this.bot);
  289. Normalize.StripIllegalCharacters stripper = new AIMLbot.Normalize.StripIllegalCharacters(this.bot);
  290. string substitutedInput = substitutor.Transform(input);
  291. // split the pattern into it's component words
  292. string[] substitutedWords = substitutedInput.Split(" \r\n\t".ToCharArray());
  293. // Normalize all words unless they're the AIML wildcards "*" and "_" during AIML loading
  294. foreach (string word in substitutedWords)
  295. {
  296. string normalizedWord;
  297. if (isUserInput)
  298. {
  299. normalizedWord = stripper.Transform(word);
  300. }
  301. else
  302. {
  303. if ((word == "*") || (word == "_"))
  304. {
  305. normalizedWord = word;
  306. }
  307. else
  308. {
  309. normalizedWord = stripper.Transform(word);
  310. }
  311. }
  312. result.Append(normalizedWord.Trim() + " ");
  313. }
  314. return result.ToString().Replace(" "," "); // make sure the whitespace is neat
  315. }
  316. #endregion
  317. }
  318. }