Skip to content

Commit 2739ca3

Browse files
committed
Avoid isError
git-svn-id: http://svn.php.net/repository/pear/packages/Text_LanguageDetect/trunk@322305 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 27c3602 commit 2739ca3

File tree

4 files changed

+75
-153
lines changed

4 files changed

+75
-153
lines changed

Text/LanguageDetect.php

Lines changed: 64 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
*
4444
* echo "Supported languages:\n";
4545
*
46-
* $langs = $l->getLanguages();
47-
* if (PEAR::isError($langs)) {
48-
* die($langs->getMessage());
46+
* try {
47+
* $langs = $l->getLanguages();
48+
* } catch (Text_LanguageDetect_Exception $e) {
49+
* die($e->getMessage());
4950
* }
5051
*
5152
* sort($langs);
@@ -104,9 +105,6 @@ class Text_LanguageDetect
104105
*
105106
* Will be loaded on start from $this->_db_filename
106107
*
107-
* May be set to a PEAR_Error object if there is an error during its
108-
* initialization
109-
*
110108
* @var array
111109
* @access private
112110
*/
@@ -120,14 +118,6 @@ class Text_LanguageDetect
120118
*/
121119
var $_unicode_map;
122120

123-
/**
124-
* stores any errors during setup
125-
*
126-
* @access private
127-
* @var PEAR_Error
128-
*/
129-
var $_setup_error;
130-
131121
/**
132122
* The size of the trigram data arrays
133123
*
@@ -186,29 +176,26 @@ class Text_LanguageDetect
186176
/**
187177
* Constructor
188178
*
189-
* Will attempt to load the language database. If it fails, you will get
190-
* a PEAR_Error object returned when you try to use detect()
179+
* Will attempt to load the language database.
191180
*
181+
* @throws Text_LanguageDetect_Exception
182+
* @todo Avoid work in the constructor
192183
*/
193184
function Text_LanguageDetect()
194185
{
195186
$data = $this->_readdb($this->_db_filename);
196-
if (PEAR::isError($data)) {
197-
// if error, save the error message
198-
$this->_setup_error = $data;
199-
200-
} else {
201-
$this->_lang_db = $data['trigram'];
187+
188+
$this->_lang_db = $data['trigram'];
202189

203-
if (isset($data['trigram-unicodemap'])) {
204-
$this->_unicode_map = $data['trigram-unicodemap'];
205-
}
190+
if (isset($data['trigram-unicodemap'])) {
191+
$this->_unicode_map = $data['trigram-unicodemap'];
192+
}
206193

207-
// Not yet implemented:
208-
if (isset($data['trigram-clusters'])) {
209-
$this->_clusters = $data['trigram-clusters'];
210-
}
194+
// Not yet implemented:
195+
if (isset($data['trigram-clusters'])) {
196+
$this->_clusters = $data['trigram-clusters'];
211197
}
198+
212199
}
213200

214201
/**
@@ -244,7 +231,7 @@ function _get_data_loc($fname)
244231
* @access private
245232
* @param string $fname the filename where the data is stored
246233
* @return array the language model data
247-
* @throws PEAR_Error
234+
* @throws Text_LanguageDetect_Exception
248235
*/
249236
function _readdb($fname)
250237
{
@@ -276,18 +263,12 @@ function _readdb($fname)
276263
* Checks if this object is ready to detect languages
277264
*
278265
* @access private
279-
* @param mixed &$err error object to be returned by reference, if any
280266
* @return bool true if no errors
267+
* @throws Text_LanguageDetect_Exception
281268
*/
282-
function _setup_ok(&$err)
269+
function _setup_ok()
283270
{
284-
if (PEAR::isError($this->_setup_error)) {
285-
// if there was an error from when the language database was loaded
286-
// then return that error
287-
$err = $this->_setup_error;
288-
return false;
289-
290-
} elseif (!is_array($this->_lang_db)) {
271+
if (!is_array($this->_lang_db)) {
291272
if (ini_get('magic_quotes_runtime')) {
292273
throw new Text_LanguageDetect_Exception('Error loading database. Try turning magic_quotes_runtime off.');
293274
} else {
@@ -314,15 +295,11 @@ function _setup_ok(&$err)
314295
* @param bool $include_only if true will include (rather than
315296
* exclude) only those in the list
316297
* @return int number of languages successfully deleted
317-
* @throws PEAR_Error
298+
* @throws Text_LanguageDetect_Exception
318299
*/
319300
function omitLanguages($omit_list, $include_only = false)
320301
{
321-
322-
// setup check
323-
if (!$this->_setup_ok($err)) {
324-
return $err;
325-
}
302+
$this->_setup_ok();
326303

327304
$deleted = 0;
328305

@@ -379,15 +356,13 @@ function omitLanguages($omit_list, $include_only = false)
379356
*
380357
* @access public
381358
* @return int the number of languages
382-
* @throws PEAR_Error
359+
* @throws Text_LanguageDetect_Exception
383360
*/
384361
function getLanguageCount()
385362
{
386-
if (!$this->_setup_ok($err)) {
387-
return $err;
388-
} else {
389-
return count($this->_lang_db);
390-
}
363+
$this->_setup_ok();
364+
365+
return count($this->_lang_db);
391366
}
392367

393368
/**
@@ -398,31 +373,29 @@ function getLanguageCount()
398373
* @access public
399374
* @param mixed $lang language name or array of language names
400375
* @return bool true if language model exists
401-
* @throws PEAR_Error
376+
* @throws Text_LanguageDetect_Exception
402377
*/
403378
function languageExists($lang)
404379
{
405-
if (!$this->_setup_ok($err)) {
406-
return $err;
407-
} else {
408-
$lang = $this->_convertFromNameMode($lang);
409-
// string
410-
if (is_string($lang)) {
411-
return isset($this->_lang_db[strtolower($lang)]);
412-
413-
// array
414-
} elseif (is_array($lang)) {
415-
foreach ($lang as $test_lang) {
416-
if (!isset($this->_lang_db[strtolower($test_lang)])) {
417-
return false;
418-
}
419-
}
420-
return true;
421-
422-
// other (error)
423-
} else {
424-
throw new Text_LanguageDetect_Exception('Unknown type passed to languageExists()');
380+
$this->_setup_ok();
381+
382+
$lang = $this->_convertFromNameMode($lang);
383+
// string
384+
if (is_string($lang)) {
385+
return isset($this->_lang_db[strtolower($lang)]);
386+
387+
// array
388+
} elseif (is_array($lang)) {
389+
foreach ($lang as $test_lang) {
390+
if (!isset($this->_lang_db[strtolower($test_lang)])) {
391+
return false;
392+
}
425393
}
394+
return true;
395+
396+
// other (error)
397+
} else {
398+
throw new Text_LanguageDetect_Exception('Unknown type passed to languageExists()');
426399
}
427400
}
428401

@@ -431,17 +404,15 @@ function languageExists($lang)
431404
*
432405
* @access public
433406
* @return array the names of the languages known to this object
434-
* @throws PEAR_Error
407+
* @throws Text_LanguageDetect_Exception
435408
*/
436409
function getLanguages()
437410
{
438-
if (!$this->_setup_ok($err)) {
439-
return $err;
440-
} else {
441-
return $this->_convertToNameMode(
442-
array_keys($this->_lang_db)
443-
);
444-
}
411+
$this->_setup_ok();
412+
413+
return $this->_convertToNameMode(
414+
array_keys($this->_lang_db)
415+
);
445416
}
446417

447418
/**
@@ -700,16 +671,13 @@ function _normalize_score($score, $base_count = null)
700671
* @param int $limit if specified, return an array of the most likely
701672
* $limit languages and their scores.
702673
* @return mixed sorted array of language scores, blank array if no
703-
* useable text was found, or PEAR_Error if error
704-
* with the object setup
674+
* useable text was found
705675
* @see _distance()
706-
* @throws PEAR_Error
676+
* @throws Text_LanguageDetect_Exception
707677
*/
708678
function detect($sample, $limit = 0)
709679
{
710-
if (!$this->_setup_ok($err)) {
711-
return $err;
712-
}
680+
$this->_setup_ok();
713681

714682
// input check
715683
if (!Text_LanguageDetect_Parser::validateString($sample)) {
@@ -855,16 +823,12 @@ function detect($sample, $limit = 0)
855823
* @return string the name of the most likely language
856824
* or null if no language is similar
857825
* @see detect()
858-
* @throws PEAR_Error
826+
* @throws Text_LanguageDetect_Exception
859827
*/
860828
function detectSimple($sample)
861829
{
862830
$scores = $this->detect($sample, 1);
863831

864-
if (PEAR::isError($scores)) {
865-
return $scores;
866-
}
867-
868832
// if top language has the maximum possible score,
869833
// then the top score will have been picked at random
870834
if ( !is_array($scores)
@@ -901,16 +865,12 @@ function detectSimple($sample)
901865
* @return array most similar language, score and confidence rating
902866
* or null if no language is similar
903867
* @see detect()
904-
* @throws PEAR_Error
868+
* @throws Text_LanguageDetect_Exception
905869
*/
906870
function detectConfidence($sample)
907871
{
908872
$scores = $this->detect($sample, 2);
909873

910-
if (PEAR::isError($scores)) {
911-
return $scores;
912-
}
913-
914874
// if most similar language has the max score, it
915875
// will have been picked at random
916876
if ( !is_array($scores)
@@ -955,7 +915,7 @@ function detectConfidence($sample)
955915
* non-printing characters. Includes spaces,
956916
* newlines and common punctutation characters.
957917
* @return array
958-
* @throws PEAR_Error
918+
* @throws Text_LanguageDetect_Exception
959919
*/
960920
function detectUnicodeBlocks($str, $skip_symbols)
961921
{
@@ -990,7 +950,7 @@ function detectUnicodeBlocks($str, $skip_symbols)
990950
* @access public
991951
* @param mixed $unicode unicode value or utf8 char
992952
* @return mixed the block name string or false if not found
993-
* @throws PEAR_Error
953+
* @throws Text_LanguageDetect_Exception
994954
*/
995955
function unicodeBlockName($unicode) {
996956
if (is_string($unicode)) {
@@ -1014,11 +974,6 @@ function unicodeBlockName($unicode) {
1014974

1015975
$blocks =& $this->_read_unicode_block_db();
1016976

1017-
// there might have been a setup error for the block database
1018-
if (PEAR::isError($blocks)) {
1019-
return $blocks;
1020-
}
1021-
1022977
$result = $this->_unicode_block_name($unicode, $blocks);
1023978

1024979
if ($result == -1) {
@@ -1091,7 +1046,7 @@ function _unicode_block_name($unicode, &$blocks, $block_count = -1) {
10911046
*
10921047
* @access protected
10931048
* @return array the database of unicode block definitions
1094-
* @throws PEAR_Error
1049+
* @throws Text_LanguageDetect_Exception
10951050
*/
10961051
function &_read_unicode_block_db() {
10971052
// since the unicode definitions are always going to be the same,
@@ -1123,13 +1078,11 @@ function &_read_unicode_block_db() {
11231078
* @return array scores of every language compared
11241079
* or the score of just the provided languages
11251080
* or null if one of the supplied languages does not exist
1126-
* @throws PEAR_Error
1081+
* @throws Text_LanguageDetect_Exception
11271082
*/
11281083
function languageSimilarity($lang1 = null, $lang2 = null)
11291084
{
1130-
if (!$this->_setup_ok($err)) {
1131-
return $err;
1132-
}
1085+
$this->_setup_ok();
11331086

11341087
$lang1 = $this->_convertFromNameMode($lang1);
11351088
$lang2 = $this->_convertFromNameMode($lang2);
@@ -1223,7 +1176,7 @@ function languageSimilarity($lang1 = null, $lang2 = null)
12231176
*
12241177
* @access public
12251178
* @return array language cluster data
1226-
* @throws PEAR_Error
1179+
* @throws Text_LanguageDetect_Exception
12271180
* @see languageSimilarity()
12281181
* @deprecated this function will eventually be removed and placed into
12291182
* the model generation class
@@ -1233,9 +1186,7 @@ function clusterLanguages()
12331186
// todo: set the maximum number of clusters
12341187

12351188
// setup check
1236-
if (!$this->_setup_ok($err)) {
1237-
return $err;
1238-
}
1189+
$this->_setup_ok();
12391190

12401191
// return cached result, if any
12411192
if (isset($this->_clusters)) {
@@ -1414,7 +1365,7 @@ function clusterLanguages()
14141365
* @access public
14151366
* @param string $str input string
14161367
* @return array language scores (only those compared)
1417-
* @throws PEAR_Error
1368+
* @throws Text_LanguageDetect_Exception
14181369
*/
14191370
function clusteredSearch($str)
14201371
{
@@ -1428,10 +1379,6 @@ function clusteredSearch($str)
14281379
// so it's safe to call it every time
14291380
$result = $this->clusterLanguages();
14301381

1431-
if (PEAR::isError($result)) {
1432-
return $result;
1433-
}
1434-
14351382
$dendogram_start = $result['open_forks'];
14361383
$dendogram_data = $result['fork_data'];
14371384
$dendogram_alias = $result['name_map'];

Text/LanguageDetect/Parser.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,7 @@ function analyze()
222222
if ($this->_compile_unicode) {
223223
$blocks =& $this->_read_unicode_block_db();
224224

225-
if (PEAR::isError($blocks)) {
226-
$this->_compile_unicode = false;
227-
228-
// will be returned to the user when getUnicodeBlocks() is called
229-
$this->_unicode_blocks = $blocks;
230-
231-
} else {
232-
$block_count = count($blocks);
233-
}
225+
$block_count = count($blocks);
234226

235227
$skipped_count = 0;
236228
$unicode_chars = array();

0 commit comments

Comments
 (0)