aboutsummaryrefslogblamecommitdiffhomepage
path: root/inc/3rdparty/libraries/language-detect/LanguageDetect.php
blob: 09b115467121eb3e2ad79d022772a1290118b92e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635


































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                
<?php

/**
 * Detects the language of a given piece of text.
 *
 * Attempts to detect the language of a sample of text by correlating ranked
 * 3-gram frequencies to a table of 3-gram frequencies of known languages.
 *
 * Implements a version of a technique originally proposed by Cavnar & Trenkle 
 * (1994): "N-Gram-Based Text Categorization" 
 *
 * PHP versions 4 and 5
 *
 * @category   Text
 * @package    Text_LanguageDetect
 * @author     Nicholas Pisarro <infinityminusnine+pear@gmail.com>
 * @copyright  2005-2006 Nicholas Pisarro
 * @license    http://www.debian.org/misc/bsd.license BSD
 * @version    CVS: $Id: LanguageDetect.php,v 1.20 2008/07/01 02:09:15 taak Exp $
 * @link       http://pear.php.net/package/Text_LanguageDetect/
 * @link       http://langdetect.blogspot.com/
 */

//require_once 'PEAR.php';
require_once 'Parser.php';

/**
 * Language detection class
 *
 * Requires the langauge model database (lang.dat) that should have
 * accompanied this class definition in order to be instantiated.
 *
 * Example usage:
 *
 * <code>
 * require_once 'Text/LanguageDetect.php';
 *
 * $l = new Text_LanguageDetect;
 *
 * $stdin = fopen('php://stdin', 'r');
 *
 * echo "Supported languages:\n";
 *
 * $langs = $l->getLanguages();
 * if (PEAR::isError($langs)) {
 *     die($langs->getMessage());
 * }
 *
 * sort($langs);
 * echo join(', ', $langs);
 *
 * while ($line = fgets($stdin)) {
 *     print_r($l->detect($line, 4));
 * }
 * </code>
 *
 * @category   Text
 * @package    Text_LanguageDetect
 * @author     Nicholas Pisarro <infinityminusnine+pear@gmail.com>
 * @copyright  2005 Nicholas Pisarro
 * @license    http://www.debian.org/misc/bsd.license BSD
 * @version    Release: @package_version@
 * @todo       allow users to generate their own language models
 */
 
class Text_LanguageDetect
{
    /** 
     * The filename that stores the trigram data for the detector
     *
     * If this value starts with a slash (/) or a dot (.) the value of 
     * $this->_data_dir will be ignored
     * 
     * @var      string
     * @access   private
     */
    var $_db_filename = './lang.dat';

    /**
     * The filename that stores the unicode block definitions
     *
     * If this value starts with a slash (/) or a dot (.) the value of 
     * $this->_data_dir will be ignored
     * 
     * @var string
     * @access private
     */
    var $_unicode_db_filename = './unicode_blocks.dat';

    /**
     * The data directory
     *
     * Should be set by PEAR installer
     *
     * @var      string
     * @access   private
     */
    var $_data_dir = '@data_dir@';

    /**
     * The trigram data for comparison
     * 
     * Will be loaded on start from $this->_db_filename
     *
     * May be set to a PEAR_Error object if there is an error during its 
     * initialization
     *
     * @var      array
     * @access   private
     */
    var $_lang_db = array();

    /**
     * stores the map of the trigram data to unicode characters
     *
     * @access private
     * @var array
     */
    var $_unicode_map;

    /**
     * The size of the trigram data arrays
     * 
     * @var      int
     * @access   private
     */
    var $_threshold = 300;

    /**
     * the maximum possible score.
     *
     * needed for score normalization. Different depending on the
     * perl compatibility setting
     *
     * @access  private
     * @var     int
     * @see     setPerlCompatible()
     */
    var $_max_score = 0;

    /**
     * Whether or not to simulate perl's Language::Guess exactly
     * 
     * @access  private
     * @var     bool
     * @see     setPerlCompatible()
     */
    var $_perl_compatible = false;

    /**
     * Whether to use the unicode block detection to speed up processing
     *
     * @access private
     * @var bool
     */
    var $_use_unicode_narrowing = true;

    /**
     * stores the result of the clustering operation
     *
     * @access  private
     * @var     array
     * @see     clusterLanguages()
     */
    var $_clusters;

    /**
     * Constructor
     *
     * Will attempt to load the language database. If it fails, you will get
     * a PEAR_Error object returned when you try to use detect()
     *
     */
    function Text_LanguageDetect($db=null, $unicode_db=null)
    {
		if (isset($db)) $this->_db_filename = $db;
		if (isset($unicode_db)) $this->_unicode_db_filename = $unicode_db;
		
        $data = $this->_readdb($this->_db_filename);
        $this->_lang_db = $data['trigram'];

        if (isset($data['trigram-unicodemap'])) {
            $this->_unicode_map = $data['trigram-unicodemap'];
        }

        // Not yet implemented:
        if (isset($data['trigram-clusters'])) {
            $this->_clusters = $data['trigram-clusters'];
        }		
    }

    /**
     * Returns the path to the location of the database
     *
     * @access    private
     * @return    string    expected path to the language model database
     */
    function _get_data_loc($fname)
    {
        return $fname;
    }

    /**
     * Loads the language trigram database from filename
     *
     * Trigram datbase should be a serialize()'d array
     * 
     * @access    private
     * @param     string      $fname   the filename where the data is stored
     * @return    array                the language model data
     * @throws    PEAR_Error
     */
    function _readdb($fname)
    {
        // finds the correct data dir
        $fname = $this->_get_data_loc($fname);

        // input check
        if (!file_exists($fname)) {
            throw new Exception('Language database does not exist.');
        } elseif (!is_readable($fname)) {
            throw new Exception('Language database is not readable.');
        }

        if (function_exists('file_get_contents')) {
            return unserialize(file_get_contents($fname));
        } else {
            // if you don't have file_get_contents(), 
            // then this is the next fastest way
            ob_start();
            readfile($fname);
            $contents = ob_get_contents();
            ob_end_clean();
            return unserialize($contents);
        }
    }


    /**
     * Checks if this object is ready to detect languages
     * 
     * @access   private
     * @param    mixed   &$err  error object to be returned by reference, if any
     * @return   bool           true if no errors
     */
    function _setup_ok(&$err)
    {
        if (!is_array($this->_lang_db)) {
            if (ini_get('magic_quotes_runtime')) {
                throw new Exception('Error loading database. Try turning magic_quotes_runtime off.');
            } else {
                throw new Exception('Language database is not an array.');
            }
            return false;

        } elseif (empty($this->_lang_db)) {
            throw new Exception('Language database has no elements.');
            return false;

        } else {
            return true;
        }
    }

    /**
     * Omits languages
     *
     * Pass this function the name of or an array of names of 
     * languages that you don't want considered
     *
     * If you're only expecting a limited set of languages, this can greatly 
     * speed up processing
     *
     * @access   public
     * @param    mixed  $omit_list      language name or array of names to omit
     * @param    bool   $include_only   if true will include (rather than 
     *                                  exclude) only those in the list
     * @return   int                    number of languages successfully deleted
     * @throws   PEAR_Error
     */
    function omitLanguages($omit_list, $include_only = false)
    {

        // setup check
        if (!$this->_setup_ok($err)) {
            return $err;
        }

        $deleted = 0;

        // deleting the given languages
        if (!$include_only) {
            if (!is_array($omit_list)) {
                $omit_list = strtolower($omit_list); // case desensitize
                if (isset($this->_lang_db[$omit_list])) {
                    unset($this->_lang_db[$omit_list]);
                    $deleted++;
                }
            } else {
                foreach ($omit_list as $omit_lang) {
                    if (isset($this->_lang_db[$omit_lang])) {
                        unset($this->_lang_db[$omit_lang]);
                        $deleted++;
                    } 
                }
            }

        // deleting all except the given languages
        } else {
            if (!is_array($omit_list)) {
                $omit_list = array($omit_list);
            }

            // case desensitize
            foreach ($omit_list as $key => $omit_lang) {
                $omit_list[$key] = strtolower($omit_lang);
            }

            foreach (array_keys($this->_lang_db) as $lang) {
                if (!in_array($lang, $omit_list)) {
                    unset($this->_lang_db[$lang]);
                    $deleted++;
                }
            }
        }

        // reset the cluster cache if the number of languages changes
        // this will then have to be recalculated
        if (isset($this->_clusters) && $deleted > 0) {
            unset($this->_clusters);
        }

        return $deleted;
    }


    /**
     * Returns the number of languages that this object can detect
     *
     * @access public
     * @return int            the number of languages
     * @throws PEAR_Error
     */
    function getLanguageCount()
    {
        if (!$this->_setup_ok($err)) {
            return $err;
        } else {
            return count($this->_lang_db);
        }
    }

    /**
     * Returns true if a given language exists
     *
     * If passed an array of names, will return true only if all exist
     *
     * @access    public
     * @param     mixed       $lang    language name or array of language names
     * @return    bool                 true if language model exists
     * @throws    PEAR_Error
     */
    function languageExists($lang)
    {
        if (!$this->_setup_ok($err)) {
            return $err;
        } else {
            // string
            if (is_string($lang)) {
                return isset($this->_lang_db[strtolower($lang)]);

            // array
            } elseif (is_array($lang)) {
                foreach ($lang as $test_lang) {
                    if (!isset($this->_lang_db[strtolower($test_lang)])) {
                        return false;
                    } 
                }
                return true;

            // other (error)
            } else {
                throw new Exception('Unknown type passed to languageExists()');
            }
        }
    }

    /**
     * Returns the list of detectable languages
     *
     * @access public
     * @return array        the names of the languages known to this object
     * @throws PEAR_Error
     */
    function getLanguages()
    {
        if (!$this->_setup_ok($err)) {
            return $err;
        } else {
            return array_keys($this->_lang_db);
        }
    }

    /**
     * Make this object behave like Language::Guess
     * 
     * @access    public
     * @param     bool     $setting     false to turn off perl compatibility
     */
    function setPerlCompatible($setting = true)
    {
        if (is_bool($setting)) { // input check
            $this->_perl_compatible = $setting;

            if ($setting == true) {
                $this->_max_score = $this->_threshold;
            } else {
                $this->_max_score = 0;
            }
        }

    }

    /**
     * Whether to use unicode block ranges in detection
     *
     * Should speed up most detections if turned on (detault is on). In some
     * circumstances it may be slower, such as for large text samples (> 10K)
     * in languages that use latin scripts. In other cases it should speed up
     * detection noticeably.
     *
     * @access  public
     * @param   bool    $setting    false to turn off
     */
    function useUnicodeBlocks($setting = true)
    {
        if (is_bool($setting)) {
            $this->_use_unicode_narrowing = $setting;
        }
    }

    /**
     * Converts a piece of text into trigrams
     *
     * Superceded by the Text_LanguageDetect_Parser class 
     *
     * @access    private
     * @param     string    $text    text to convert
     * @return    array              array of trigram frequencies
     */
    function _trigram($text)
    {
        $s = new Text_LanguageDetect_Parser($text, $this->_db_filename, $this->_unicode_db_filename);
        $s->prepareTrigram();
        $s->prepareUnicode(false);
        $s->setPadStart(!$this->_perl_compatible);
        $s->analyze();
        return $s->getTrigramFreqs();
    }

    /**
     * Converts a set of trigrams from frequencies to ranks
     *
     * Thresholds (cuts off) the list at $this->_threshold
     *
     * @access    protected
     * @param     array     $arr     array of trgram 
     * @return    array              ranks of trigrams
     */
    function _arr_rank(&$arr)
    {

        // sorts alphabetically first as a standard way of breaking rank ties
        $this->_bub_sort($arr);

        // below might also work, but seemed to introduce errors in testing
        //ksort($arr);
        //asort($arr);

        $rank = array();

        $i = 0;
        foreach ($arr as $key => $value) {
            $rank[$key] = $i++;

            // cut off at a standard threshold
            if ($i >= $this->_threshold) {
                break;
            }
        }

        return $rank;
    }

    /**
     * Sorts an array by value breaking ties alphabetically
     * 
     * @access   private
     * @param    array     &$arr     the array to sort
     */
    function _bub_sort(&$arr)
    {
        // should do the same as this perl statement:
        // sort { $trigrams{$b} == $trigrams{$a} ?  $a cmp $b : $trigrams{$b} <=> $trigrams{$a} }

        // needs to sort by both key and value at once
        // using the key to break ties for the value

        // converts array into an array of arrays of each key and value
        // may be a better way of doing this
        $combined = array();

        foreach ($arr as $key => $value) {
            $combined[] = array($key, $value);
        }

        usort($combined, array($this, '_sort_func'));

        $replacement = array();
        foreach ($combined as $key => $value) {
            list($new_key, $new_value) = $value;
            $replacement[$new_key] = $new_value;
        }

        $arr = $replacement;
    }

    /**
     * Sort function used by bubble sort
     *
     * Callback function for usort(). 
     *
     * @access   private
     * @param    array        first param passed by usort()
     * @param    array        second param passed by usort()
     * @return   int          1 if $a is greater, -1 if not
     * @see      _bub_sort()
     */
    function _sort_func($a, $b)
    {
        // each is actually a key/value pair, so that it can compare using both
        list($a_key, $a_value) = $a;
        list($b_key, $b_value) = $b;

        // if the values are the same, break ties using the key
        if ($a_value == $b_value) {
            return strcmp($a_key, $b_key);

        // if not, just sort normally
        } else {
            if ($a_value > $b_value) {
                return -1;
            } else {
                return 1;
            }
        }

        // 0 should not be possible because keys must be unique
    }

    /**
     * Calculates a linear rank-order distance statistic between two sets of 
     * ranked trigrams
     *
     * Sums the differences in rank for each trigram. If the trigram does not 
     * appear in both, consider it a difference of $this->_threshold.
     *
     * This distance measure was proposed by Cavnar & Trenkle (1994). Despite
     * its simplicity it has been shown to be highly accurate for language
     * identification tasks.
     *
     * @access  private
     * @param   array    $arr1  the reference set of trigram ranks
     * @param   array    $arr2  the target set of trigram ranks
     * @return  int             the sum of the differences between the ranks of
     *                          the two trigram sets
     */
    function _distance(&$arr1, &$arr2)
    {
        $sumdist = 0;

        foreach ($arr2 as $key => $value) {
            if (isset($arr1[$key])) {
                $distance = abs($value - $arr1[$key]);
            } else {
                // $this->_threshold sets the maximum possible distance value
                // for any one pair of trigrams
                $distance = $this->_threshold;
            }
            $sumdist += $distance;
        }

        return $sumdist;

        // todo: there are other distance statistics to try, e.g. relative
        //       entropy, but they're probably more costly to compute
    }

    /**
     * Normalizes the score returned by _distance()
     * 
     * Different if perl compatible or not
     *
     * @access    private
     * @param     int    $score          the score from _distance()
     * @param     int    $base_count     the number of trigrams being considered
     * @return    float                  the normalized score
     * @see       _distance()
     */
    function _normalize_score($score, $base_count = null)
    {
        if ($base_count === null) {
            $base_count = $this->_threshold;
        }

        if (!$this->_perl_compatible) {
            return 1 - ($score / $base_count / $this->_threshold);
        } else {
            return floor($score / $base_count);
        }
    }


    /**
     * Detects the closeness of a sample of text to the known languages
     *
     * Calculates the statistical difference between the text and
     * the trigrams for each language, normalizes the score then
     * returns results for all languages in sorted order
     *
     * If perl compatible, the score is 300-0, 0 being most similar.
     * Otherwise, it's 0-1 with 1 being most similar.
     * 
     * The $sample text should be at least a few sentences in length;
     * should be ascii-7 or utf8 encoded, if another and the mbstring extension
     * is present it will try to detect and convert. However, experience has
     * shown that mb_detect_encoding() *does not work very well* with at least 
     * some types of encoding.
     *
     * @access  public
     * @param   string  $sample a sample of text to compare.
     * @param   int     $limit  if specified, return an array of the most likely
     *                           $limit languages and their scores.
     * @return  mixed       sorted array of language scores, blank array if no 
     *                      useable text was found, or PEAR_Error if error 
     *                      with the object setup
     * @see     _distance()
     * @throws  PEAR_Error
     */
    function detect($sample, $limit = 0)
    {
        if (!$this->_setup_ok($err)) {
            return $err;
        }

        // input check
        if (!Text_LanguageDetect_Parser::validateString($sample)) {
            return array();
        }

        // check char encoding
        // (only if mbstring extension is compiled and PHP > 4.0.6)
        if (function_exists('mb_detect_encoding') 
            && function_exists('mb_convert_encoding')) {

            // mb_detect_encoding isn't very reliable, to say the least
            // detection should still work with a sufficient sample of ascii characters
            $encoding = mb_detect_encoding($sample);

            // mb_detect_encoding() will return FALSE if detection fails
            // don't attempt conversion if that's the case
            if ($encoding != 'ASCII' && $encoding != 'UTF-8' && $encoding !== false) {
            
                if (function_exists('mb_list_encodings')) {
 
                    // verify the encoding exists in mb_list_encodings
                    if (in_array($encoding, mb_list_encodings())) {
                        $sample = mb_convert_encoding($sample, 'UTF-8', $encoding);
                    }

                    // if the previous condition failed:
                    // somehow we detected an encoding that also we don't support

                } else {
                    // php 4 doesnt have mb_list_encodings()
                    // so attempt with error suppression
                    $sample = @mb_convert_encoding($sample, 'UTF-8', $encoding);
                }
            }
        }

        $sample_obj = new Text_LanguageDetect_Parser($sample, $this->_db_filename, $this->_unicode_db_filename);
        $sample_obj->prepareTrigram();
        if ($this->_use_unicode_narrowing) {
            $sample_obj->prepareUnicode();
        }
        $sample_obj->setPadStart(!$this->_perl_compatible);
        $sample_obj->analyze();

        $trigram_freqs =& $sample_obj->getTrigramRanks();
        $trigram_count = count($trigram_freqs);

        if ($trigram_count == 0) {
            return array();
        }

        $scores = array();

        // use unicode block detection to narrow down the possibilities
        if ($this->_use_unicode_narrowing) {
            $blocks =& $sample_obj->getUnicodeBlocks();

            if (is_array($blocks)) {
                $present_blocks = array_keys($blocks);
            } else {
                throw new Exception('Error during block detection');
            }

            $possible_langs = array();

            foreach ($present_blocks as $blockname) {
                if (isset($this->_unicode_map[$blockname])) {

                    $possible_langs = array_merge(
                        $possible_langs,
                        array_keys($this->_unicode_map[$blockname])
                    );

                    // todo: faster way to do this?
                }
            }

            // could also try an intersect operation rather than a union
            // in other words, choose languages whose trigrams contain 
            // ALL of the unicode blocks found in this sample
            // would improve speed but would be completely thrown off by an
            // unexpected character, like an umlaut appearing in english text

            $possible_langs = array_intersect(
                        array_keys($this->_lang_db),
                        array_unique($possible_langs)
            );

            // needs to intersect it with the keys of _lang_db in case 
            // languages have been omitted

        // or just try 'em all
        } else {
            $possible_langs = array_keys($this->_lang_db);
        }


        foreach ($possible_langs as $lang) {
            $scores[$lang] =
                $this->_normalize_score(
                        $this->_distance($this->_lang_db[$lang], $trigram_freqs),
                        $trigram_count);
        }

        unset($sample_obj);

        if ($this->_perl_compatible) {
            asort($scores);
        } else {
            arsort($scores);
        }

        // todo: drop languages with a score of $this->_max_score?

        // limit the number of returned scores
        if ($limit && is_numeric($limit)) {
            $limited_scores = array();

            $i = 0;

            foreach ($scores as $key => $value) {
                if ($i++ >= $limit) {
                    break;
                }

                $limited_scores[$key] = $value;
            }

            return $limited_scores;
        } else {
            return $scores;
        }
    }

    /**
     * Returns only the most similar language to the text sample
     *
     * Calls $this->detect() and returns only the top result
     * 
     * @access   public
     * @param    string    $sample    text to detect the language of
     * @return   string               the name of the most likely language
     *                                or null if no language is similar
     * @see      detect()
     * @throws   PEAR_Error
     */
    function detectSimple($sample)
    {
        $scores = $this->detect($sample, 1);

        // if top language has the maximum possible score,
        // then the top score will have been picked at random
        if (    !is_array($scores) 
                || empty($scores) 
                || current($scores) == $this->_max_score) {

            return null;

        } else {
            return ucfirst(key($scores));
        }
    }

    /**
     * Returns an array containing the most similar language and a confidence
     * rating
     * 
     * Confidence is a simple measure calculated from the similarity score
     * minus the similarity score from the next most similar language
     * divided by the highest possible score. Languages that have closely
     * related cousins (e.g. Norwegian and Danish) should generally have lower
     * confidence scores.
     *
     * The similarity score answers the question "How likely is the text the
     * returned language regardless of the other languages considered?" The 
     * confidence score is one way of answering the question "how likely is the
     * text the detected language relative to the rest of the language model
     * set?"
     *
     * To see how similar languages are a priori, see languageSimilarity()
     * 
     * @access   public
     * @param    string    $sample    text for which language will be detected
     * @return   array     most similar language, score and confidence rating
     *                     or null if no language is similar
     * @see      detect()
     * @throws   PEAR_Error
     */
    function detectConfidence($sample)
    {
        $scores = $this->detect($sample, 2);

        // if most similar language has the max score, it 
        // will have been picked at random
        if (    !is_array($scores) 
                || empty($scores) 
                || current($scores) == $this->_max_score) {

            return null;
        }

        $arr['language'] = ucfirst(key($scores));
        $arr['similarity'] = current($scores);
        if (next($scores) !== false) { // if false then no next element
            // the goal is to return a higher value if the distance between
            // the similarity of the first score and the second score is high

            if ($this->_perl_compatible) {

                $arr['confidence'] =
                    (current($scores) - $arr['similarity']) / $this->_max_score;

            } else {

                $arr['confidence'] = $arr['similarity'] - current($scores);

            }

        } else {
            $arr['confidence'] = null;
        }

        return $arr;
    }

    /**
     * Returns the distribution of unicode blocks in a given utf8 string
     *
     * For the block name of a single char, use unicodeBlockName()
     * 
     * @access public
     * @param string $str input string. Must be ascii or utf8
     * @param bool $skip_symbols if true, skip ascii digits, symbols and 
     *                           non-printing characters. Includes spaces,
     *                           newlines and common punctutation characters.
     * @return array
     * @throws PEAR_Error
     */
    function detectUnicodeBlocks($str, $skip_symbols)
    {
        // input check
        if (!is_bool($skip_symbols)) {
            throw new Exception('Second parameter must be boolean');
        } 

        if (!is_string($str)) {
            throw new Exception('First parameter was not a string');
        }

        $sample_obj = new Text_LanguageDetect_Parser($str, $this->_db_filename, $this->_unicode_db_filename);
        $sample_obj->prepareUnicode();
        $sample_obj->prepareTrigram(false);
        $sample_obj->setUnicodeSkipSymbols($skip_symbols);
        $sample_obj->analyze();
        $blocks =& $sample_obj->getUnicodeBlocks();
        unset($sample_obj);
        return $blocks;
    }

    /**
     * Returns the block name for a given unicode value
     *
     * If passed a string, will assume it is being passed a UTF8-formatted 
     * character and will automatically convert. Otherwise it will assume it
     * is being passed a numeric unicode value.
     *
     * Make sure input is of the correct type!
     *
     * @access public
     * @param mixed $unicode unicode value or utf8 char
     * @return mixed the block name string or false if not found
     * @throws PEAR_Error
     */
    function unicodeBlockName($unicode) {
        if (is_string($unicode)) {
            // assume it is being passed a utf8 char, so convert it

            // input check
            if ($this->utf8strlen($unicode) > 1) {
                throw new Exception('Pass this function only a single char');
            }

            $unicode = $this->_utf8char2unicode($unicode);

            if ($unicode == -1) {
                throw new Exception('Malformatted char');
            }

        // input check
        } elseif (!is_int($unicode)) {
            throw new Exception('Input must be of type string or int.');
        }

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

        $result = $this->_unicode_block_name($unicode, $blocks);

        if ($result == -1) {
            return false;
        } else {
            return $result[2];
        }
    }

    /**
     * Searches the unicode block database
     *
     * Returns the block name for a given unicode value. unicodeBlockName() is
     * the public interface for this function, which does input checks which
     * this function omits for speed.
     *
     * @access  protected
     * @param   int     $unicode the unicode value
     * @param   array   &$blocks the block database
     * @param   int     $block_count the number of defined blocks in the database
     * @see     unicodeBlockName()
     */
    function _unicode_block_name($unicode, &$blocks, $block_count = -1) {
        // for a reference, see 
        // http://www.unicode.org/Public/UNIDATA/Blocks.txt

        // assume that ascii characters are the most common
        // so try it first for efficiency
        if ($unicode <= $blocks[0][1]) {
            return $blocks[0];
        }

        // the optional $block_count param is for efficiency
        // so we this function doesn't have to run count() every time
        if ($block_count != -1) {
            $high = $block_count - 1;
        } else {
            $high = count($blocks) - 1;
        }

        $low = 1; // start with 1 because ascii was 0

        // your average binary search algorithm
        while ($low <= $high) {
            $mid = floor(($low + $high) / 2);

            // if it's lower than the lower bound
            if ($unicode < $blocks[$mid][0]) {
                $high = $mid - 1;

            // if it's higher than the upper bound
            } elseif ($unicode > $blocks[$mid][1]) {
                $low = $mid + 1;

            // found it
            } else {
                return $blocks[$mid];
            }
        }

        // failed to find the block 
        return -1;

        // todo: differentiate when it's out of range or when it falls 
        //       into an unassigned range?
    }

    /**
     * Brings up the unicode block database
     *
     * @access protected
     * @return array the database of unicode block definitions
     * @throws PEAR_Error
     */
    function &_read_unicode_block_db() {
        // since the unicode definitions are always going to be the same,
        // might as well share the memory for the db with all other instances
        // of this class
        static $data;

        if (!isset($data)) {
            $data = $this->_readdb($this->_unicode_db_filename);
        }

        return $data;
    }

    /**
     * Calculate the similarities between the language models
     * 
     * Use this function to see how similar languages are to each other.
     *
     * If passed 2 language names, will return just those languages compared.
     * If passed 1 language name, will return that language compared to
     * all others.
     * If passed none, will return an array of every language model compared 
     * to every other one.
     *
     * @access  public
     * @param   string   $lang1   the name of the first language to be compared
     * @param   string   $lang2   the name of the second language to be compared
     * @return  array    scores of every language compared
     *                   or the score of just the provided languages
     *                   or null if one of the supplied languages does not exist
     * @throws  PEAR_Error
     */
    function languageSimilarity($lang1 = null, $lang2 = null)
    {
        if (!$this->_setup_ok($err)) {
            return $err;
        }

        if ($lang1 != null) {
            $lang1 = strtolower($lang1);

            // check if language model exists
            if (!isset($this->_lang_db[$lang1])) {
                return null;
            }

            if ($lang2 != null) {

                // can't only set the second param
                if ($lang1 == null) {
                    return null;
                // check if language model exists
                } elseif (!isset($this->_lang_db[$lang2])) {
                    return null;
                }

                $lang2 = strtolower($lang2);

                // compare just these two languages
                return $this->_normalize_score(
                    $this->_distance(
                        $this->_lang_db[$lang1],
                        $this->_lang_db[$lang2]
                    )
                );


            // compare just $lang1 to all languages
            } else {
                $return_arr = array();
                foreach ($this->_lang_db as $key => $value) {
                    if ($key != $lang1) { // don't compare a language to itself
                        $return_arr[$key] = $this->_normalize_score(
                            $this->_distance($this->_lang_db[$lang1], $value));
                    }
                }
                asort($return_arr);

                return $return_arr;
            }


        // compare all languages to each other
        } else {
            $return_arr = array();
            foreach (array_keys($this->_lang_db) as $lang1) {
                foreach (array_keys($this->_lang_db) as $lang2) {

                    // skip comparing languages to themselves
                    if ($lang1 != $lang2) { 
                    
                        // don't re-calculate what's already been done
                        if (isset($return_arr[$lang2][$lang1])) {

                            $return_arr[$lang1][$lang2] =
                                $return_arr[$lang2][$lang1];

                        // calculate
                        } else {

                            $return_arr[$lang1][$lang2] = 
                                $this->_normalize_score(
                                        $this->_distance(
                                            $this->_lang_db[$lang1],
                                            $this->_lang_db[$lang2]
                                        )
                                );

                        }
                    }
                }
            }
            return $return_arr;
        }
    }

    /**
     * Cluster known languages according to languageSimilarity()
     *
     * WARNING: this method is EXPERIMENTAL. It is not recommended for common
     * use, and it may disappear or its functionality may change in future
     * releases without notice.
     *
     * Uses a nearest neighbor technique to generate the maximum possible
     * number of dendograms from the similarity data.
     *
     * @access      public
     * @return      array language cluster data
     * @throws      PEAR_Error
     * @see         languageSimilarity()
     * @deprecated  this function will eventually be removed and placed into 
     *              the model generation class
     */
    function clusterLanguages()
    {
        // todo: set the maximum number of clusters

        // setup check
        if (!$this->_setup_ok($err)) {
            return $err;
        }

        // return cached result, if any
        if (isset($this->_clusters)) {
            return $this->_clusters;
        }

        $langs = array_keys($this->_lang_db);

        $arr = $this->languageSimilarity();

        sort($langs);

        foreach ($langs as $lang) {
            if (!isset($this->_lang_db[$lang])) {
                throw new Exception("missing $lang!\n");
            }
        }

        // http://www.psychstat.missouristate.edu/multibook/mlt04m.html
        foreach ($langs as $old_key => $lang1) {
            $langs[$lang1] = $lang1;
            unset($langs[$old_key]);
        }
        
        $i = 0;
        while (count($langs) > 2 && $i++ < 200) {
            $highest_score = -1;
            $highest_key1 = '';
            $highest_key2 = '';
            foreach ($langs as $lang1) {
                foreach ($langs as $lang2) {
                    if (    $lang1 != $lang2 
                            && $arr[$lang1][$lang2] > $highest_score) {
                        $highest_score = $arr[$lang1][$lang2];
                        $highest_key1 = $lang1;
                        $highest_key2 = $lang2;
                    }
                }
            }
            
            if (!$highest_key1) {
                // should not ever happen
                throw new Exception("no highest key? (step: $i)");
            }

            if ($highest_score == 0) {
                // languages are perfectly dissimilar
                break;
            }

            // $highest_key1 and $highest_key2 are most similar
            $sum1 = array_sum($arr[$highest_key1]);
            $sum2 = array_sum($arr[$highest_key2]);

            // use the score for the one that is most similar to the rest of 
            // the field as the score for the group
            // todo: could try averaging or "centroid" method instead
            // seems like that might make more sense
            // actually nearest neighbor may be better for binary searching


            // for "Complete Linkage"/"furthest neighbor"
            // sign should be <
            // for "Single Linkage"/"nearest neighbor" method
            // should should be >
            // results seem to be pretty much the same with either method

            // figure out which to delete and which to replace
            if ($sum1 > $sum2) {
                $replaceme = $highest_key1;
                $deleteme = $highest_key2;
            } else {
                $replaceme = $highest_key2;
                $deleteme = $highest_key1;
            }

            $newkey = $replaceme . ':' . $deleteme;

            // $replaceme is most similar to remaining languages
            // replace $replaceme with '$newkey', deleting $deleteme

            // keep a record of which fork is really which language
            $really_lang = $replaceme;
            while (isset($really_map[$really_lang])) {
                $really_lang = $really_map[$really_lang];
            } 
            $really_map[$newkey] = $really_lang;


            // replace the best fitting key, delete the other
            foreach ($arr as $key1 => $arr2) {
                foreach ($arr2 as $key2 => $value2) {
                    if ($key2 == $replaceme) {
                        $arr[$key1][$newkey] = $arr[$key1][$key2];
                        unset($arr[$key1][$key2]);
                        // replacing $arr[$key1][$key2] with $arr[$key1][$newkey]
                    } 
                    
                    if ($key1 == $replaceme) {
                        $arr[$newkey][$key2] = $arr[$key1][$key2];
                        unset($arr[$key1][$key2]);
                        // replacing $arr[$key1][$key2] with $arr[$newkey][$key2]
                    }

                    if ($key1 == $deleteme || $key2 == $deleteme) {
                        // deleting $arr[$key1][$key2]
                        unset($arr[$key1][$key2]);
                    }
                }
            }
                        

            unset($langs[$highest_key1]);
            unset($langs[$highest_key2]);
            $langs[$newkey] = $newkey;


            // some of these may be overkill
            $result_data[$newkey] = array(
                                'newkey' => $newkey,
                                'count' => $i,
                                'diff' => abs($sum1 - $sum2),
                                'score' => $highest_score,
                                'bestfit' => $replaceme,
                                'otherfit' => $deleteme,
                                'really' => $really_lang,
                            );
        }

        $return_val = array(
                'open_forks' => $langs, 
                        // the top level of clusters
                        // clusters that are mutually exclusive
                        // or specified by a specific maximum

                'fork_data' => $result_data,
                        // data for each split

                'name_map' => $really_map,
                        // which cluster is really which language
                        // using the nearest neighbor technique, the cluster
                        // inherits all of the properties of its most-similar member
                        // this keeps track
            );


        // saves the result in the object
        $this->_clusters = $return_val;

        return $return_val;
    }


    /**
     * Perform an intelligent detection based on clusterLanguages()
     *
     * WARNING: this method is EXPERIMENTAL. It is not recommended for common
     * use, and it may disappear or its functionality may change in future
     * releases without notice.
     *
     * This compares the sample text to top the top level of clusters. If the 
     * sample is similar to the cluster it will drop down and compare it to the
     * languages in the cluster, and so on until it hits a leaf node.
     *
     * this should find the language in considerably fewer compares 
     * (the equivalent of a binary search), however clusterLanguages() is costly
     * and the loss of accuracy from this technique is significant.
     *
     * This method may need to be 'fuzzier' in order to become more accurate.
     *
     * This function could be more useful if the universe of possible languages
     * was very large, however in such cases some method of Bayesian inference
     * might be more helpful.
     *
     * @see     clusterLanguages()
     * @access  public
     * @param   string $str input string
     * @return  array language scores (only those compared)
     * @throws  PEAR_Error
     */
    function clusteredSearch($str)
    {

        // input check
        if (!Text_LanguageDetect_Parser::validateString($str)) {
            return array();
        }

        // clusterLanguages() will return a cached result if possible
        // so it's safe to call it every time
        $result = $this->clusterLanguages();

        $dendogram_start = $result['open_forks'];
        $dendogram_data  = $result['fork_data'];
        $dendogram_alias = $result['name_map'];

        $sample_obj = new Text_LanguageDetect_Parser($str, $this->_db_filename, $this->_unicode_db_filename);
        $sample_obj->prepareTrigram();
        $sample_obj->setPadStart(!$this->_perl_compatible);
        $sample_obj->analyze();
        $sample_result = $sample_obj->getTrigramRanks();
        $sample_count  = count($sample_result);

        // input check
        if ($sample_count == 0) {
            return array();
        }

        $i = 0; // counts the number of steps
        
        foreach ($dendogram_start as $lang) {
            if (isset($dendogram_alias[$lang])) {
                $lang_key = $dendogram_alias[$lang];
            } else {
                $lang_key = $lang;
            }

            $scores[$lang] = $this->_normalize_score(
                $this->_distance($this->_lang_db[$lang_key], $sample_result),
                $sample_count);

            $i++;
        }

        if ($this->_perl_compatible) {
            asort($scores);
        } else {
            arsort($scores);
        }

        $top_score = current($scores);
        $top_key = key($scores);

        // of starting forks, $top_key is the most similar to the sample

        $cur_key = $top_key;
        while (isset($dendogram_data[$cur_key])) {
            $lang1 = $dendogram_data[$cur_key]['bestfit'];
            $lang2 = $dendogram_data[$cur_key]['otherfit'];
            foreach (array($lang1, $lang2) as $lang) {
                if (isset($dendogram_alias[$lang])) {
                    $lang_key = $dendogram_alias[$lang];
                } else {
                    $lang_key = $lang;
                }

                $scores[$lang] = $this->_normalize_score(
                    $this->_distance($this->_lang_db[$lang_key], $sample_result),
                    $sample_count);

                //todo: does not need to do same comparison again
            }

            $i++;

            if ($scores[$lang1] > $scores[$lang2]) {
                $cur_key = $lang1;
                $loser_key = $lang2;
            } else {
                $cur_key = $lang2;
                $loser_key = $lang1;
            }

            $diff = $scores[$cur_key] - $scores[$loser_key];

            // $cur_key ({$dendogram_alias[$cur_key]}) wins 
            // over $loser_key ({$dendogram_alias[$loser_key]}) 
            // with a difference of $diff
        }

        // found result in $i compares

        // rather than sorting the result, preserve it so that you can see
        // which paths the algorithm decided to take along the tree

        // but sometimes the last item is only the second highest
        if (   ($this->_perl_compatible  && (end($scores) > prev($scores)))
            || (!$this->_perl_compatible && (end($scores) < prev($scores)))) {

            $real_last_score = current($scores);
            $real_last_key = key($scores);

            // swaps the 2nd-to-last item for the last item
            unset($scores[$real_last_key]);
            $scores[$real_last_key] = $real_last_score;
        }
            

        if (!$this->_perl_compatible) {
            $scores = array_reverse($scores, true);
            // second param requires php > 4.0.3
        }

        return $scores;
    }

    /**
     * ut8-safe strlen()
     *
     * Returns the numbers of characters (not bytes) in a utf8 string
     *
     * @static
     * @access  public
     * @param   string $str string to get the length of
     * @return  int         number of chars
     */
    function utf8strlen($str)
    {
        // utf8_decode() will convert unknown chars to '?', which is actually
        // ideal for counting.

        return strlen(utf8_decode($str));

        // idea stolen from dokuwiki
    }

    /**
     * Returns the unicode value of a utf8 char
     *
     * @access  protected
     * @param   string $char a utf8 (possibly multi-byte) char
     * @return  int          unicode value or -1 if malformatted
     */
    function _utf8char2unicode($char) {

        // strlen() here will actually get the binary length of a single char
        switch (strlen($char)) {

            // for a reference, see http://en.wikipedia.org/wiki/UTF-8

            case 1:
                // normal ASCII-7 byte
                // 0xxxxxxx -->  0xxxxxxx
                return ord($char{0});

            case 2:
                // 2 byte unicode
                // 110zzzzx 10xxxxxx --> 00000zzz zxxxxxxx
                $z = (ord($char{0}) & 0x000001F) << 6;
                $x = (ord($char{1}) & 0x0000003F);

                return ($z | $x);

            case 3:
                // 3 byte unicode
                // 1110zzzz 10zxxxxx 10xxxxxx --> zzzzzxxx xxxxxxxx 
                $z =  (ord($char{0}) & 0x0000000F) << 12;
                $x1 = (ord($char{1}) & 0x0000003F) << 6;
                $x2 = (ord($char{2}) & 0x0000003F);

                return ($z | $x1 | $x2);

            case 4:
                // 4 byte unicode
                // 11110zzz 10zzxxxx 10xxxxxx 10xxxxxx -->
                // 000zzzzz xxxxxxxx xxxxxxxx
                $z1 = (ord($char{0}) & 0x00000007) << 18;
                $z2 = (ord($char{1}) & 0x0000003F) << 12;
                $x1 = (ord($char{2}) & 0x0000003F) << 6;
                $x2 = (ord($char{3}) & 0x0000003F);

                return ($z1 | $z2 | $x1 | $x2);

            default:
                // error: malformatted char?
                return -1;
        }
    }

    /**
     * utf8-safe fast character iterator
     *
     * Will get the next character starting from $counter, which will then be
     * incremented. If a multi-byte char the bytes will be concatenated and 
     * $counter will be incremeted by the number of bytes in the char.
     *
     * @access  private
     * @param   string  &$str        the string being iterated over
     * @param   int     &$counter    the iterator, will increment by reference
     * @param   bool    $special_convert whether to do special conversions
     * @return  char    the next (possibly multi-byte) char from $counter
     */
    function _next_char(&$str, &$counter, $special_convert = false)
    {

        $char = $str{$counter++};
        $ord = ord($char);

        // for a description of the utf8 system see
        // http://www.phpclasses.org/browse/file/5131.html

        // normal ascii one byte char
        if ($ord <= 127) {

            // special conversions needed for this package
            // (that only apply to regular ascii characters)
            // lower case, and convert all non-alphanumeric characters
            // other than "'" to space
            if ($special_convert && $char != ' ' && $char != "'") {
                if ($ord >= 65 && $ord <= 90) { // A-Z
                    $char = chr($ord + 32); // lower case
                } elseif ($ord < 97 || $ord > 122) { // NOT a-z
                    $char = ' '; // convert to space
                }
            }

            return $char;

        // multi-byte chars
        } elseif ($ord >> 5 == 6) { // two-byte char
            $nextchar = $str{$counter++}; // get next byte

            // lower-casing of non-ascii characters is still incomplete

            if ($special_convert) {
                // lower case latin accented characters
                if ($ord == 195) {
                    $nextord = ord($nextchar);
                    $nextord_adj = $nextord + 64;
                    // for a reference, see 
                    // http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html

                    // &Agrave; - &THORN; but not &times;
                    if (    $nextord_adj >= 192
                            && $nextord_adj <= 222 
                            && $nextord_adj != 215) {

                        $nextchar = chr($nextord + 32); 
                    }

                // lower case cyrillic alphabet
                } elseif ($ord == 208) {
                    $nextord = ord($nextchar);
                    // if A - Pe
                    if ($nextord >= 144 && $nextord <= 159) {
                        // lower case
                        $nextchar = chr($nextord + 32);

                    // if Er - Ya
                    } elseif ($nextord >= 160 && $nextord <= 175) {
                        // lower case
                        $char = chr(209); // == $ord++
                        $nextchar = chr($nextord - 32);
                    }
                }
            }

            // tag on next byte
            return $char . $nextchar; 

        } elseif ($ord >> 4  == 14) { // three-byte char
            
            // tag on next 2 bytes
            return $char . $str{$counter++} . $str{$counter++}; 

        } elseif ($ord >> 3 == 30) { // four-byte char

            // tag on next 3 bytes
            return $char . $str{$counter++} . $str{$counter++} . $str{$counter++};

        } else {
            // error?
        }
    }

}

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

?>