Parser.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * This class represents a text sample to be parsed.
  4. *
  5. * @category Text
  6. * @package Text_LanguageDetect
  7. * @author Nicholas Pisarro
  8. * @copyright 2006
  9. * @license BSD
  10. * @version CVS: $Id: Parser.php 322327 2012-01-15 17:55:59Z cweiske $
  11. * @link http://pear.php.net/package/Text_LanguageDetect/
  12. * @link http://langdetect.blogspot.com/
  13. */
  14. /**
  15. * This class represents a text sample to be parsed.
  16. *
  17. * This separates the analysis of a text sample from the primary LanguageDetect
  18. * class. After a new profile has been built, the data can be retrieved using
  19. * the accessor functions.
  20. *
  21. * This class is intended to be used by the Text_LanguageDetect class, not
  22. * end-users.
  23. *
  24. * @category Text
  25. * @package Text_LanguageDetect
  26. * @author Nicholas Pisarro
  27. * @copyright 2006
  28. * @license BSD
  29. * @version release: 0.3.0
  30. */
  31. class Text_LanguageDetect_Parser extends Text_LanguageDetect
  32. {
  33. /**
  34. * the piece of text being parsed
  35. *
  36. * @access private
  37. * @var string
  38. */
  39. var $_string;
  40. /**
  41. * stores the trigram frequencies of the sample
  42. *
  43. * @access private
  44. * @var string
  45. */
  46. var $_trigrams = array();
  47. /**
  48. * stores the trigram ranks of the sample
  49. *
  50. * @access private
  51. * @var array
  52. */
  53. var $_trigram_ranks = array();
  54. /**
  55. * stores the unicode blocks of the sample
  56. *
  57. * @access private
  58. * @var array
  59. */
  60. var $_unicode_blocks = array();
  61. /**
  62. * Whether the parser should compile the unicode ranges
  63. *
  64. * @access private
  65. * @var bool
  66. */
  67. var $_compile_unicode = false;
  68. /**
  69. * Whether the parser should compile trigrams
  70. *
  71. * @access private
  72. * @var bool
  73. */
  74. var $_compile_trigram = false;
  75. /**
  76. * Whether the trigram parser should pad the beginning of the string
  77. *
  78. * @access private
  79. * @var bool
  80. */
  81. var $_trigram_pad_start = false;
  82. /**
  83. * Whether the unicode parser should skip non-alphabetical ascii chars
  84. *
  85. * @access private
  86. * @var bool
  87. */
  88. var $_unicode_skip_symbols = true;
  89. /**
  90. * Constructor
  91. *
  92. * @access private
  93. * @param string $string string to be parsed
  94. */
  95. function Text_LanguageDetect_Parser($string) {
  96. $this->_string = $string;
  97. }
  98. /**
  99. * Returns true if a string is suitable for parsing
  100. *
  101. * @param string $str input string to test
  102. * @return bool true if acceptable, false if not
  103. */
  104. public static function validateString($str) {
  105. if (!empty($str) && strlen($str) > 3 && preg_match('/\S/', $str)) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * turn on/off trigram counting
  113. *
  114. * @access public
  115. * @param bool $bool true for on, false for off
  116. */
  117. function prepareTrigram($bool = true)
  118. {
  119. $this->_compile_trigram = $bool;
  120. }
  121. /**
  122. * turn on/off unicode block counting
  123. *
  124. * @access public
  125. * @param bool $bool true for on, false for off
  126. */
  127. function prepareUnicode($bool = true)
  128. {
  129. $this->_compile_unicode = $bool;
  130. }
  131. /**
  132. * turn on/off padding the beginning of the sample string
  133. *
  134. * @access public
  135. * @param bool $bool true for on, false for off
  136. */
  137. function setPadStart($bool = true)
  138. {
  139. $this->_trigram_pad_start = $bool;
  140. }
  141. /**
  142. * Should the unicode block counter skip non-alphabetical ascii chars?
  143. *
  144. * @access public
  145. * @param bool $bool true for on, false for off
  146. */
  147. function setUnicodeSkipSymbols($bool = true)
  148. {
  149. $this->_unicode_skip_symbols = $bool;
  150. }
  151. /**
  152. * Returns the trigram ranks for the text sample
  153. *
  154. * @access public
  155. * @return array trigram ranks in the text sample
  156. */
  157. function &getTrigramRanks()
  158. {
  159. return $this->_trigram_ranks;
  160. }
  161. /**
  162. * Return the trigram freqency table
  163. *
  164. * only used in testing to make sure the parser is working
  165. *
  166. * @access public
  167. * @return array trigram freqencies in the text sample
  168. */
  169. function &getTrigramFreqs()
  170. {
  171. return $this->_trigram;
  172. }
  173. /**
  174. * returns the array of unicode blocks
  175. *
  176. * @access public
  177. * @return array unicode blocks in the text sample
  178. */
  179. function &getUnicodeBlocks()
  180. {
  181. return $this->_unicode_blocks;
  182. }
  183. /**
  184. * Executes the parsing operation
  185. *
  186. * Be sure to call the set*() functions to set options and the
  187. * prepare*() functions first to tell it what kind of data to compute
  188. *
  189. * Afterwards the get*() functions can be used to access the compiled
  190. * information.
  191. *
  192. * @access public
  193. */
  194. function analyze()
  195. {
  196. $len = strlen($this->_string);
  197. $byte_counter = 0;
  198. // unicode startup
  199. if ($this->_compile_unicode) {
  200. $blocks = $this->_read_unicode_block_db();
  201. $block_count = count($blocks);
  202. $skipped_count = 0;
  203. $unicode_chars = array();
  204. }
  205. // trigram startup
  206. if ($this->_compile_trigram) {
  207. // initialize them as blank so the parser will skip the first two
  208. // (since it skips trigrams with more than 2 contiguous spaces)
  209. $a = ' ';
  210. $b = ' ';
  211. // kludge
  212. // if it finds a valid trigram to start and the start pad option is
  213. // off, then set a variable that will be used to reduce this
  214. // trigram after parsing has finished
  215. if (!$this->_trigram_pad_start) {
  216. $a = $this->_next_char($this->_string, $byte_counter, true);
  217. if ($a != ' ') {
  218. $b = $this->_next_char($this->_string, $byte_counter, true);
  219. $dropone = " $a$b";
  220. }
  221. $byte_counter = 0;
  222. $a = ' ';
  223. $b = ' ';
  224. }
  225. }
  226. while ($byte_counter < $len) {
  227. $char = $this->_next_char($this->_string, $byte_counter, true);
  228. // language trigram detection
  229. if ($this->_compile_trigram) {
  230. if (!($b == ' ' && ($a == ' ' || $char == ' '))) {
  231. if (!isset($this->_trigram[$a . $b . $char])) {
  232. $this->_trigram[$a . $b . $char] = 1;
  233. } else {
  234. $this->_trigram[$a . $b . $char]++;
  235. }
  236. }
  237. $a = $b;
  238. $b = $char;
  239. }
  240. // unicode block detection
  241. if ($this->_compile_unicode) {
  242. if ($this->_unicode_skip_symbols
  243. && strlen($char) == 1
  244. && ($char < 'A' || $char > 'z'
  245. || ($char > 'Z' && $char < 'a'))
  246. && $char != "'") { // does not skip the apostrophe
  247. // since it's included in the language
  248. // models
  249. $skipped_count++;
  250. continue;
  251. }
  252. // build an array of all the characters
  253. if (isset($unicode_chars[$char])) {
  254. $unicode_chars[$char]++;
  255. } else {
  256. $unicode_chars[$char] = 1;
  257. }
  258. }
  259. // todo: add byte detection here
  260. }
  261. // unicode cleanup
  262. if ($this->_compile_unicode) {
  263. foreach ($unicode_chars as $utf8_char => $count) {
  264. $search_result = $this->_unicode_block_name(
  265. $this->_utf8char2unicode($utf8_char), $blocks, $block_count);
  266. if ($search_result != -1) {
  267. $block_name = $search_result[2];
  268. } else {
  269. $block_name = '[Malformatted]';
  270. }
  271. if (isset($this->_unicode_blocks[$block_name])) {
  272. $this->_unicode_blocks[$block_name] += $count;
  273. } else {
  274. $this->_unicode_blocks[$block_name] = $count;
  275. }
  276. }
  277. }
  278. // trigram cleanup
  279. if ($this->_compile_trigram) {
  280. // pad the end
  281. if ($b != ' ') {
  282. if (!isset($this->_trigram["$a$b "])) {
  283. $this->_trigram["$a$b "] = 1;
  284. } else {
  285. $this->_trigram["$a$b "]++;
  286. }
  287. }
  288. // perl compatibility; Language::Guess does not pad the beginning
  289. // kludge
  290. if (isset($dropone)) {
  291. if ($this->_trigram[$dropone] == 1) {
  292. unset($this->_trigram[$dropone]);
  293. } else {
  294. $this->_trigram[$dropone]--;
  295. }
  296. }
  297. if (!empty($this->_trigram)) {
  298. $this->_trigram_ranks = $this->_arr_rank($this->_trigram);
  299. } else {
  300. $this->_trigram_ranks = array();
  301. }
  302. }
  303. }
  304. }
  305. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  306. ?>