]> git.mxchange.org Git - friendica.git/blob - library/langdet/docs/example_web.php
1e155fef2cbdabea3d2a4d786e7d35dfc4a2a48c
[friendica.git] / library / langdet / docs / example_web.php
1 <?php
2
3 /**
4  * example usage (web)
5  *
6  * @package Text_LanguageDetect
7  * @version CVS: $Id: example_web.php 205493 2006-01-18 00:26:57Z taak $
8  */
9
10 // browsers will encode multi-byte characters wrong unless they think the page is utf8-encoded
11 header('Content-type: text/html; charset=utf-8', true);
12
13 require_once 'Text/LanguageDetect.php';
14
15 $l = new Text_LanguageDetect;
16 if (isset($_REQUEST['q'])) {
17     $q = stripslashes($_REQUEST['q']);
18 }
19
20 ?>
21 <html>
22 <head>
23 <title>Text_LanguageDetect demonstration</title>
24 </head>
25 <body>
26 <h2>Text_LanguageDetect</h2>
27 <?
28 echo "<small>Supported languages:\n";
29 $langs = $l->getLanguages();
30 sort($langs);
31 foreach ($langs as $lang) {
32     echo ucfirst($lang), ', ';
33     $i++;
34 }
35
36 echo "<br />total $i</small><br /><br />";
37
38 ?>
39 <form method="post">
40 Enter text to identify language (at least a couple of sentences):<br />
41 <textarea name="q" wrap="virtual" cols="80" rows="8"><?= $q ?></textarea>
42 <br />
43 <input type="submit" value="Submit" />
44 </form>
45 <?
46 if (isset($q) && strlen($q)) {
47     $len = $l->utf8strlen($q);
48     if ($len < 20) { // this value picked somewhat arbitrarily
49         echo "Warning: string not very long ($len chars)<br />\n";
50     }
51
52     $result = $l->detectConfidence($q);
53
54     if ($result == null) {
55         echo "Text_LanguageDetect cannot identify this piece of text. <br /><br />\n";
56     } else {
57         echo "Text_LanguageDetect thinks this text is written in <b>{$result['language']}</b> ({$result['similarity']}, {$result['confidence']})<br /><br />\n";
58     }
59
60     $result = $l->detectUnicodeBlocks($q, false);
61     if (!empty($result)) {
62         arsort($result);
63         echo "Unicode blocks present: ", join(', ', array_keys($result)), "\n<br /><br />";
64     }
65 }
66
67 unset($l);
68
69 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
70
71 ?>
72 </body></html>