|
@@ -8,10 +8,88 @@
|
|
|
|
|
|
namespace sensitive_word;
|
|
|
|
|
|
-
|
|
|
class DFAItem
|
|
|
{
|
|
|
- private $word = array();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|