function test_unicode_off() { // see what happens when you turn the unicode setting off $myobj = new TextLanguageDetect(); $str = 'This is a delightful sample of English text'; $myobj->useUnicodeBlocks(true); $result1 = $myobj->detectConfidence($str); $myobj->useUnicodeBlocks(false); $result2 = $myobj->detectConfidence($str); $this->assertEquals($result1, $result2); // note this test doesn't tell if unicode narrowing was actually used or not }
<?php /** * Demonstrates how to use ISO language codes. * * The "name mode" changes the way languages are accepted and returned. */ spl_autoload_register(function ($class) { $file = __DIR__ . '/../lib/' . strtr($class, '\\', '/') . '.php'; if (file_exists($file)) { require $file; return true; } }); use TextLanguageDetect; $l = new TextLanguageDetect\TextLanguageDetect(); //will output the ISO 639-1 two-letter language code // "de" $l->setNameMode(2); echo $l->detectSimple('Das ist ein kleiner Text') . "\n"; //will output the ISO 639-2 three-letter language code // "deu" $l->setNameMode(3); echo $l->detectSimple('Das ist ein kleiner Text') . "\n";
/** * example usage (CLI) * * @package Text_LanguageDetect * @version CVS: $Id: example_clui.php 322305 2012-01-15 00:04:17Z clockwerx $ */ spl_autoload_register(function ($class) { $file = __DIR__ . '/../lib/' . strtr($class, '\\', '/') . '.php'; if (file_exists($file)) { require $file; return true; } }); use TextLanguageDetect; $l = new TextLanguageDetect\TextLanguageDetect(); $stdin = fopen('php://stdin', 'r'); echo "Supported languages:\n"; $langs = $l->getLanguages(); sort($langs); echo join(', ', $langs); echo "\ntotal ", count($langs), "\n\n"; while ($line = fgets($stdin)) { $result = $l->detect($line, 4); print_r($result); $blocks = $l->detectUnicodeBlocks($line, true); print_r($blocks); } fclose($stdin); unset($l); /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
function testOmitLanguages() { $str = 'This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.'; $myobj = new TextLanguageDetect(); $method = new \ReflectionProperty('\\TextLanguageDetect\\TextLanguageDetect', '_use_unicode_narrowing'); $method->setAccessible(TRUE); $method->setValue($myobj, false); $count = $myobj->getLanguageCount(); $returnval = $myobj->omitLanguages('english'); $newcount = $myobj->getLanguageCount(); $this->assertEquals(1, $returnval); $this->assertEquals(1, $count - $newcount); $result = strtolower($myobj->detectSimple($str)); $this->assertTrue($result != 'english', $result); $myobj = new TextLanguageDetect(); $count = $myobj->getLanguageCount(); $returnval = $myobj->omitLanguages(array('danish', 'italian'), true); $newcount = $myobj->getLanguageCount(); $this->assertEquals($count - $newcount, $returnval); $this->assertEquals($count - $returnval, $newcount); $result = strtolower($myobj->detectSimple($str)); $this->assertTrue($result == 'danish' || $result == 'italian', $result); $result = $myobj->detect($str); $this->assertEquals(2, count($result)); $this->assertTrue(isset($result['danish'])); $this->assertTrue(isset($result['italian'])); unset($myobj); }
* example usage (web) * * @package Text_LanguageDetect * @version CVS: $Id: example_web.php 205493 2006-01-18 00:26:57Z taak $ */ // browsers will encode multi-byte characters wrong unless they think the page is utf8-encoded header('Content-type: text/html; charset=utf-8', true); spl_autoload_register(function ($class) { $file = __DIR__ . '/../lib/' . strtr($class, '\\', '/') . '.php'; if (file_exists($file)) { require $file; return true; } }); use TextLanguageDetect; $l = new TextLanguageDetect\TextLanguageDetect(); if (isset($_REQUEST['q'])) { $q = stripslashes($_REQUEST['q']); } ?> <html> <head> <title>Text_LanguageDetect demonstration</title> </head> <body> <h2>Text_LanguageDetect</h2> <?php echo "<small>Supported languages:\n"; $langs = $l->getLanguages(); sort($langs); foreach ($langs as $lang) {