Source for file retrieve.inc.php

Documentation is available at retrieve.inc.php

  1. <?php
  2.  
  3. /*
  4. Program E related AIML knowledgebase tools
  5. Copyright 2004, Anne Kootstra [anne@aiml.info]
  6. http://www.AIML.info
  7. Program E is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. Program E is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Program E; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21.  
  22.  
  23. /**
  24. * Given a specific template ID retrieve the corresponding PATTERN,
  25. * THAT and TOPIC.
  26. *
  27. * Retrieve the Pattern, That and Topic using only the ID of the corresponding template.
  28. * This function has also been adapted to make it possible to substitute the template ID
  29. * for a parent ID and retrieve the pattern-words that came before that ID. To do this set
  30. * $pat to 1.
  31. *
  32. * @uses findmainnode()
  33. * @uses findParent()
  34. * @uses findPatternPart()
  35. *
  36. * @param integer $botid The bot's ID.
  37. * @param integer $templateID The category's ID.
  38. * @param boolean $pat To use a parent ID from the pattern table instead of template ID
  39. *
  40. * @returns array [pattern],[that],[topic].
  41. */
  42. function findCategoryPattern($botid, $templateID, $pat=0) {
  43.  
  44. /**
  45. * @var array contains the final pattern
  46. */
  47. $final_pattern;
  48. // retrieve the pattern ID of the main node
  49. $mainNode= findmainnode($botid);
  50. if($pat==0) {
  51. // retrieve the corresponding parentID i.e. next word's patternID.
  52. $pattern_parent = findParent($templateID);
  53. }elseif($pat==1){
  54. $pattern_parent = $templateID;
  55. }
  56. // storage for this category's pattern, that and topic
  57. $final_pattern = array();
  58. $final_word_index = 0;
  59.  
  60. // loop ends when the path from the leaf node (template reference)
  61. // to the main node (i.e. <input>) is completely walked.
  62. while($mainNode <= $pattern_parent) {
  63. // collect the current word, and next word's pattern ID
  64. $pattern_details = findPatternPart($pattern_parent);
  65. // save current word into a seperate array element
  66. $final_pattern[$final_word_index] = $pattern_details['word'];
  67. $final_word_index++;
  68. // set up for processing next word.
  69. $pattern_parent = $pattern_details['parent'];
  70. }
  71.  
  72. // the tree is walked backwards, so the category's pattern
  73. // is also stored into the array backwards, This reverses is.
  74. $final_pattern_reverse = array_reverse($final_pattern);
  75. // process each individual word and put them into one of
  76. // [pattern], [that] or [topic] array elements.
  77. while (list($key, $val) = each($final_pattern_reverse)) {
  78. if($val == "<input>"){
  79. $cat_section = "pattern";
  80. }elseif($val == "<that>"){
  81. $cat_section = "that";
  82. }elseif($val == "<topic>") {
  83. $cat_section = "topic";
  84. }
  85.  
  86. if(!($val=="<input>" || $val=="<that>" || $val=="<topic>")) {
  87. $final_category[$cat_section] .= $val." ";
  88. }
  89.  
  90. }
  91. // return the completed three piece array.
  92. return $final_category;
  93.  
  94. }
  95.  
  96.  
  97. /**
  98. * Get the word and the parent ID using the known pattern ID.
  99. *
  100. * Because the word tree is traveled backwards, from leaf-node (i.e. the
  101. * template reference) to the trunk (i.e. the <input> node), there needs to
  102. * be a reference to the previous word, this is the parent ID in the parent
  103. * column. Both the word (including the * and _) and the parent ID are returned.
  104. *
  105. * @param string $patternID The pattern word ID.
  106. *
  107. * @return array 'parent', 'id and 'word'.
  108. *
  109. */
  110. function findPatternPart($patternID){
  111.  
  112. /*
  113. The ordera column in the pattern table is used to identify
  114. the order of the 'word' in the matching process. An underscore
  115. has the order value of 1, a word (atomic match) of 2 and the
  116. * has a value of 3. This fact is used to recreate those values.
  117. */
  118. $query = "select ifnull(if(word='<input>','AIML ROOT',word),if(ordera='1','_','*')) as word, parent, id from patterns where id = ".$patternID."";
  119. //debugger($query,2);
  120. $selectcode = mysql_query($query);
  121. if ($selectcode){
  122. if(!mysql_numrows($selectcode)){
  123. return "";
  124. }
  125. else{
  126. while ($q = mysql_fetch_array($selectcode)){
  127. $pattern['word'] = $q[0];
  128. $pattern['parent'] = $q[1];
  129. $pattern['id'] = $q[2];
  130. return $pattern;
  131. }
  132. }
  133. }
  134.  
  135. return "";
  136. }
  137.  
  138. ?>

Documentation generated on Tue, 11 Jan 2005 18:41:10 +0100 by phpDocumentor 1.3.0RC3