/phpmorphy/src-bak/fsa/access/fsa_tree_mem.php

https://github.com/Oreolek/Togataltu · PHP · 271 lines · 169 code · 63 blank · 39 comment · 38 complexity · 775c1beff2cd6486062841bacbae0185 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of phpMorphy library
  4. *
  5. * Copyright c 2007-2008 Kamaev Vladimir <heromantor@users.sourceforge.net>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. /**
  23. * This file is autogenerated at Wed, 14 Oct 2009 01:33:59 +0400, don`t change it!
  24. */
  25. class phpMorphy_Fsa_Tree_Mem extends phpMorphy_Fsa {
  26. function walk($trans, $word, $readAnnot = true) {
  27. $__mem = $this->resource; $fsa_start = $this->fsa_start;
  28. for($i = 0, $c = $GLOBALS['__phpmorphy_strlen']($word); $i < $c; $i++) {
  29. $prev_trans = $trans;
  30. $char = ord($word[$i]);
  31. /////////////////////////////////
  32. // find char in state begin
  33. // tree version
  34. $result = true;
  35. $start_offset = $fsa_start + ((($trans >> 11) & 0x1FFFFF) << 2);
  36. // read first trans in state
  37. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $start_offset, 4));
  38. // If first trans is term(i.e. pointing to annot) then skip it
  39. if(($trans & 0x0100)) {
  40. // When this is single transition in state then break
  41. if(($trans & 0x0200) && ($trans & 0x0400)) {
  42. $result = false;
  43. } else {
  44. $start_offset += 4;
  45. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $start_offset, 4));
  46. }
  47. }
  48. // if all ok process rest transitions in state
  49. if($result) {
  50. // walk through state
  51. for($idx = 1, $j = 0; ; $j++) {
  52. $attr = ($trans & 0xFF);
  53. if($attr == $char) {
  54. $result = true;
  55. break;
  56. } else if($attr > $char) {
  57. if(($trans & 0x0200)) {
  58. $result = false;
  59. break;
  60. }
  61. $idx = $idx << 1;
  62. } else {
  63. if(($trans & 0x0400)) {
  64. $result = false;
  65. break;
  66. }
  67. $idx = ($idx << 1) + 1;
  68. }
  69. if($j > 255) {
  70. throw new phpMorphy_Exception('Infinite recursion possible');
  71. }
  72. // read next trans
  73. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $start_offset + (($idx - 1) << 2), 4));
  74. }
  75. }
  76. // find char in state end
  77. /////////////////////////////////
  78. if(!$result) {
  79. $trans = $prev_trans;
  80. break;
  81. }
  82. }
  83. $annot = null;
  84. $result = false;
  85. $prev_trans = $trans;
  86. if($i >= $c) {
  87. // Read annotation when we walked all chars in word
  88. $result = true;
  89. if($readAnnot) {
  90. // read annot trans
  91. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $fsa_start + ((($trans >> 11) & 0x1FFFFF) << 2), 4));
  92. if(0 == ($trans & 0x0100)) {
  93. $result = false;
  94. } else {
  95. $annot = $this->getAnnot($trans);
  96. }
  97. }
  98. }
  99. return array(
  100. 'result' => $result,
  101. 'last_trans' => $trans,
  102. 'word_trans' => $prev_trans,
  103. 'walked' => $i,
  104. 'annot' => $annot
  105. );
  106. }
  107. function collect($startNode, $callback, $readAnnot = true, $path = '') {
  108. $total = 0;
  109. $stack = array();
  110. $stack_idx = array();
  111. $start_idx = 0;
  112. array_push($stack, null);
  113. array_push($stack_idx, null);
  114. $state = $this->readState((($startNode) >> 11) & 0x1FFFFF);
  115. do {
  116. for($i = $start_idx, $c = count($state); $i < $c; $i++) {
  117. $trans = $state[$i];
  118. if(($trans & 0x0100)) {
  119. $total++;
  120. if($readAnnot) {
  121. $annot = $this->getAnnot($trans);
  122. } else {
  123. $annot = $trans;
  124. }
  125. if(!call_user_func($callback, $path, $annot)) {
  126. return $total;
  127. }
  128. } else {
  129. $path .= chr(($trans & 0xFF));
  130. array_push($stack, $state);
  131. array_push($stack_idx, $i + 1);
  132. $state = $this->readState((($trans) >> 11) & 0x1FFFFF);
  133. $start_idx = 0;
  134. break;
  135. }
  136. }
  137. if($i >= $c) {
  138. $state = array_pop($stack);
  139. $start_idx = array_pop($stack_idx);
  140. $path = $GLOBALS['__phpmorphy_substr']($path, 0, -1);
  141. }
  142. } while(!empty($stack));
  143. return $total;
  144. }
  145. function readState($index) {
  146. $__mem = $this->resource; $fsa_start = $this->fsa_start;
  147. $result = array();
  148. $offset = $fsa_start + (($index) << 2);
  149. // read first trans
  150. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $offset, 4));
  151. // check if first trans is pointer to annot, and not single in state
  152. if(($trans & 0x0100) && !(($trans & 0x0200) || ($trans & 0x0400))) {
  153. $result[] = $trans;
  154. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $offset, 4));
  155. $offset += 4;
  156. }
  157. // read rest
  158. for($expect = 1; $expect; $expect--) {
  159. if(!($trans & 0x0200)) $expect++;
  160. if(!($trans & 0x0400)) $expect++;
  161. $result[] = $trans;
  162. if($expect > 1) {
  163. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $offset, 4));
  164. $offset += 4;
  165. }
  166. }
  167. return $result;
  168. }
  169. function unpackTranses($rawTranses) {
  170. settype($rawTranses, 'array');
  171. $result = array();
  172. foreach($rawTranses as $rawTrans) {
  173. $result[] = array(
  174. 'term' => ($rawTrans & 0x0100) ? true : false,
  175. 'llast' => ($rawTrans & 0x0200) ? true : false,
  176. 'rlast' => ($rawTrans & 0x0400) ? true : false,
  177. 'attr' => ($rawTrans & 0xFF),
  178. 'dest' => (($rawTrans) >> 11) & 0x1FFFFF,
  179. );
  180. }
  181. return $result;
  182. }
  183. protected function readRootTrans() {
  184. $__mem = $this->resource; $fsa_start = $this->fsa_start;
  185. list(, $trans) = unpack('V', $GLOBALS['__phpmorphy_substr']($__mem, $fsa_start + 0, 4));
  186. return $trans;
  187. }
  188. protected function readAlphabet() {
  189. $__mem = $this->resource; $fsa_start = $this->fsa_start;
  190. return $GLOBALS['__phpmorphy_substr']($__mem, $this->header['alphabet_offset'], $this->header['alphabet_size']);
  191. }
  192. function getAnnot($trans) {
  193. if(!($trans & 0x0100)) {
  194. return null;
  195. }
  196. $__mem = $this->resource; $fsa_start = $this->fsa_start;
  197. $offset =
  198. $this->header['annot_offset'] +
  199. ((($trans & 0xFF) << 21) | (($trans >> 11) & 0x1FFFFF));
  200. $len = ord($GLOBALS['__phpmorphy_substr']($__mem, $offset, 1));
  201. if($len) {
  202. $annot = $GLOBALS['__phpmorphy_substr']($__mem, $offset + 1, $len);
  203. } else {
  204. $annot = null;
  205. }
  206. return $annot;
  207. }
  208. }