コード例 #1
0
ファイル: iso.php プロジェクト: webmil/text-language-detect
<?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";
コード例 #2
0
 function test_omit_error()
 {
     $str = 'On January 29, 1737, Thomas Paine was born in Thetford, England. His father, a corseter, had grand visions for his son, but by the age of 12, Thomas had failed out of school. The young Paine began apprenticing for his father, but again, he failed.';
     $myobj = new TextLanguageDetect();
     $result = $myobj->detectSimple($str);
     $this->assertEquals('english', $result);
     // omit all languages and you should get an error
     $myobj->omitLanguages($myobj->getLanguages());
     $result = $myobj->detectSimple($str);
     $this->assertNull($result, gettype($result));
 }
コード例 #3
0
 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);
 }