1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/5/8
- * Time: 下午1:50
- */
- namespace sensitive_word;
- class DFAItem
- {
- private $word = null;
- private $sub_items = array();
- private $is_end = 0;
- public function __set($name, $value)
- {
- $this->$name = $value;
- }
- public function __get($name)
- {
- return $this->$name;
- }
- // 初始化
- public function init($word)
- {
- $cnt = count($word);
- if ($cnt <= 0) {
- return null;
- } else if ($cnt === 1) {
- $this->is_end = 1;
- $this->word = $word[0];
- $this->sub_items = null;
- } else {
- $this->is_end = 0;
- $this->word = $word[0];
- $this->add_item(array_slice($word, 1));
- }
- return $this;
- }
- // 添加子节点内容
- public function add_item($word)
- {
- $subitem = new DFAItem();
- $ret = $subitem->init($word);
- if (!is_null($ret)) {
- array_push($this->sub_items, $subitem);
- }
- }
- // 向链表中添加内容
- public function addwords($word)
- {
- $found = false;
- foreach ($this->sub_items as $item) {
- if (0 == strcmp($word[0], $item->word)) {
- $item->addwords(array_slice($word, 1));
- $found = true;
- }
- }
- if (!$found) {
- $subitem = new DFAItem();
- $subitem->init($word);
- array_push($this->sub_items, $subitem);
- }
- }
- // 判断关键字是否在属于此item
- public function checkword($txt)
- {
- if (is_null($txt)) {
- return false;
- }
- $head = mb_substr($txt, 0, 1);
- $found = false;
- foreach ($this->sub_items as $item) {
- if (0 == strcmp($item->word, $head)) {
- if ($item->is_end == 1) {
- return true;
- } else {
- return $item->checkword(mb_substr($txt, 1));
- }
- }
- }
- if (!$found) {
- return false;
- }
- }
- }
|