aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/pear/text_languagedetect/docs/example_web.php
blob: bee8f51a44e53e9a62db918375121afe9f1d14a8 (plain) (blame)
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
<?php

/**
 * example usage (web)
 *
 * @package Text_LanguageDetect
 * @version CVS: $Id$
 */

// browsers will encode multi-byte characters wrong unless they think the page is utf8-encoded
header('Content-type: text/html; charset=utf-8', true);

require_once 'Text/LanguageDetect.php';

$l = new Text_LanguageDetect;
if (isset($_REQUEST['q'])) {
    $q = stripslashes($_REQUEST['q']);
}

?>
<html>
<head>
<title>Text_LanguageDetect demonstration</title>
</head>
<body>
<h2>Text_LanguageDetect</h2>
<?
echo "<small>Supported languages:\n";
$langs = $l->getLanguages();
sort($langs);
foreach ($langs as $lang) {
    echo ucfirst($lang), ', ';
    $i++;
}

echo "<br />total $i</small><br /><br />";

?>
<form method="post">
Enter text to identify language (at least a couple of sentences):<br />
<textarea name="q" wrap="virtual" cols="80" rows="8"><?= $q ?></textarea>
<br />
<input type="submit" value="Submit" />
</form>
<?
if (isset($q) && strlen($q)) {
    $len = $l->utf8strlen($q);
    if ($len < 20) { // this value picked somewhat arbitrarily
        echo "Warning: string not very long ($len chars)<br />\n";
    }

    $result = $l->detectConfidence($q);

    if ($result == null) {
        echo "Text_LanguageDetect cannot identify this piece of text. <br /><br />\n";
    } else {
        echo "Text_LanguageDetect thinks this text is written in <b>{$result['language']}</b> ({$result['similarity']}, {$result['confidence']})<br /><br />\n";
    }

    $result = $l->detectUnicodeBlocks($q, false);
    if (!empty($result)) {
        arsort($result);
        echo "Unicode blocks present: ", join(', ', array_keys($result)), "\n<br /><br />";
    }
}

unset($l);

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

?>
</body></html>