4 * This class represents a text sample to be parsed.
7 * @package Text_LanguageDetect
8 * @author Nicholas Pisarro
11 * @version CVS: $Id: Parser.php 322327 2012-01-15 17:55:59Z cweiske $
12 * @link http://pear.php.net/package/Text_LanguageDetect/
13 * @link http://langdetect.blogspot.com/
17 * This class represents a text sample to be parsed.
19 * This separates the analysis of a text sample from the primary LanguageDetect
20 * class. After a new profile has been built, the data can be retrieved using
21 * the accessor functions.
23 * This class is intended to be used by the Text_LanguageDetect class, not
27 * @package Text_LanguageDetect
28 * @author Nicholas Pisarro
31 * @version release: 0.3.0
33 class Text_LanguageDetect_Parser extends Text_LanguageDetect
36 * the piece of text being parsed
44 * stores the trigram frequencies of the sample
49 var $_trigrams = array();
52 * stores the trigram ranks of the sample
57 var $_trigram_ranks = array();
60 * stores the unicode blocks of the sample
65 var $_unicode_blocks = array();
68 * Whether the parser should compile the unicode ranges
73 var $_compile_unicode = false;
76 * Whether the parser should compile trigrams
81 var $_compile_trigram = false;
84 * Whether the trigram parser should pad the beginning of the string
89 var $_trigram_pad_start = false;
92 * Whether the unicode parser should skip non-alphabetical ascii chars
97 var $_unicode_skip_symbols = true;
103 * @param string $string string to be parsed
105 function Text_LanguageDetect_Parser($string) {
106 $this->_string = $string;
110 * Returns true if a string is suitable for parsing
112 * @param string $str input string to test
113 * @return bool true if acceptable, false if not
115 public static function validateString($str) {
116 if (!empty($str) && strlen($str) > 3 && preg_match('/\S/', $str)) {
124 * turn on/off trigram counting
127 * @param bool $bool true for on, false for off
129 function prepareTrigram($bool = true)
131 $this->_compile_trigram = $bool;
135 * turn on/off unicode block counting
138 * @param bool $bool true for on, false for off
140 function prepareUnicode($bool = true)
142 $this->_compile_unicode = $bool;
146 * turn on/off padding the beginning of the sample string
149 * @param bool $bool true for on, false for off
151 function setPadStart($bool = true)
153 $this->_trigram_pad_start = $bool;
157 * Should the unicode block counter skip non-alphabetical ascii chars?
160 * @param bool $bool true for on, false for off
162 function setUnicodeSkipSymbols($bool = true)
164 $this->_unicode_skip_symbols = $bool;
168 * Returns the trigram ranks for the text sample
171 * @return array trigram ranks in the text sample
173 function &getTrigramRanks()
175 return $this->_trigram_ranks;
179 * Return the trigram freqency table
181 * only used in testing to make sure the parser is working
184 * @return array trigram freqencies in the text sample
186 function &getTrigramFreqs()
188 return $this->_trigram;
192 * returns the array of unicode blocks
195 * @return array unicode blocks in the text sample
197 function &getUnicodeBlocks()
199 return $this->_unicode_blocks;
203 * Executes the parsing operation
205 * Be sure to call the set*() functions to set options and the
206 * prepare*() functions first to tell it what kind of data to compute
208 * Afterwards the get*() functions can be used to access the compiled
215 $len = strlen($this->_string);
220 if ($this->_compile_unicode) {
221 $blocks = $this->_read_unicode_block_db();
222 $block_count = count($blocks);
225 $unicode_chars = array();
229 if ($this->_compile_trigram) {
230 // initialize them as blank so the parser will skip the first two
231 // (since it skips trigrams with more than 2 contiguous spaces)
236 // if it finds a valid trigram to start and the start pad option is
237 // off, then set a variable that will be used to reduce this
238 // trigram after parsing has finished
239 if (!$this->_trigram_pad_start) {
240 $a = $this->_next_char($this->_string, $byte_counter, true);
243 $b = $this->_next_char($this->_string, $byte_counter, true);
253 while ($byte_counter < $len) {
254 $char = $this->_next_char($this->_string, $byte_counter, true);
257 // language trigram detection
258 if ($this->_compile_trigram) {
259 if (!($b == ' ' && ($a == ' ' || $char == ' '))) {
260 if (!isset($this->_trigram[$a . $b . $char])) {
261 $this->_trigram[$a . $b . $char] = 1;
263 $this->_trigram[$a . $b . $char]++;
271 // unicode block detection
272 if ($this->_compile_unicode) {
273 if ($this->_unicode_skip_symbols
274 && strlen($char) == 1
275 && ($char < 'A' || $char > 'z'
276 || ($char > 'Z' && $char < 'a'))
277 && $char != "'") { // does not skip the apostrophe
278 // since it's included in the language
285 // build an array of all the characters
286 if (isset($unicode_chars[$char])) {
287 $unicode_chars[$char]++;
289 $unicode_chars[$char] = 1;
293 // todo: add byte detection here
297 if ($this->_compile_unicode) {
298 foreach ($unicode_chars as $utf8_char => $count) {
299 $search_result = $this->_unicode_block_name(
300 $this->_utf8char2unicode($utf8_char), $blocks, $block_count);
302 if ($search_result != -1) {
303 $block_name = $search_result[2];
305 $block_name = '[Malformatted]';
308 if (isset($this->_unicode_blocks[$block_name])) {
309 $this->_unicode_blocks[$block_name] += $count;
311 $this->_unicode_blocks[$block_name] = $count;
318 if ($this->_compile_trigram) {
321 if (!isset($this->_trigram["$a$b "])) {
322 $this->_trigram["$a$b "] = 1;
324 $this->_trigram["$a$b "]++;
328 // perl compatibility; Language::Guess does not pad the beginning
330 if (isset($dropone)) {
331 if ($this->_trigram[$dropone] == 1) {
332 unset($this->_trigram[$dropone]);
334 $this->_trigram[$dropone]--;
338 if (!empty($this->_trigram)) {
339 $this->_trigram_ranks = $this->_arr_rank($this->_trigram);
341 $this->_trigram_ranks = array();
347 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */