表示当前的词语

$this->setWord($word);

// 加载词库

$this->loadDict();

}

/**

* 加载词库

* @access public

* @return void

*/

public function loadDict()

{

$dictFile = __DIR__ . '/../dict/dic.dic';

$handle = fopen($dictFile, 'r');

while(!feof($handle))

{

$char = fgets($handle);

$this->dict[] = trim($char);

}

fclose($handle);

}

/**

* 设置要检测的词语(支持中文)

* @access public

* @param string $word 要检测的词语

* @return void

*/

public function setWord($word)

{

// 将中文转成拼音

$pinyins = $this->pinyin($word);

// 根据拼音组成数组

$this->chars = explode('-', $pinyins);

}

/**

* 检测词语

* @access public

* @return boolean

*/

public function detect()

{

// 将词库中的词语进行拆解

foreach($this->dict as $word)

{

$words = explode('-', $word);

// 判断当前拆解的词语是否在要检测的词语中

$contain = true;

foreach($this->chars as $char)

{

if(!in_array($char, $words))

{

$contain = false;

break;

}

}

if($contain) return true;

}

return false;

}

/**

* 将中文转成拼音

* @access public

* @param string $word 待转换的词语

* @return string

*/

public function pinyin($word)

{

$pinyin = new \Overtrue\Pinyin\Pinyin();

return implode('-', $pinyin->convert($word));

}

}

$detector = new WordDetector('中国');

var_dump($detector->detect());

表示当前的词语有哪些

1. Access

2. Address

3. Array

4. Binary

5. Break

6. Boolean

7. Call

8. Char

9. Class

10. Const

11. Continue

12. Debug

13. Define

14. Delete

15. Do

16. Double

17. Else

18. Enum

19. Event

20. Exit

表示当前的词语2个字

你可能想看: