php tutorial simple Chinese word segmentation system
Structure: first hash table, trie index tree node Advantages: word segmentation, without the need to predict the length of the query word, word by word matching along the tree. Disadvantages: the construction and maintenance more complicated, the word branches, wasting a certain amount of space * @ version 0.1 * @todo Constructs a general dictionary algorithm and writes a simple word segmentation * @author shjuto@gmail.com * trie dictionary tree * * /
/ ** * Add words to the dictionary * * @param string $ key * / function & setword ($ word = '') { $ trienode = & $ this-> trie; for ($ i = 0; $ i <strlen ($ word); $ i ++) { $ character = $ word [$ i]; if (! isset ($ trienode ['children'] [$ character])) { $ trienode ['children'] [$ character] = array ('isword' => false); } if ($ i == strlen ($ word) -1) { $ trienode ['children'] [$ character] = array ('isword' => true); } $ trienode = & $ trienode ['children'] [$ character]; } }
/ ** * To determine whether the dictionary word * * @param string $ word @return bool true / false * / function & isword ($ word) { $ trienode = & $ this-> trie; for ($ i = 0; $ i <strlen ($ word); $ i ++) { $ character = $ word [$ i]; if (! isset ($ trienode ['children'] [$ character])) { return false; } else { // judge the end of the word if ($ i == (strlen ($ word) -1) && $ trienode ['children'] [$ character] ['isword'] == true) { return true; } elseif ($ i == (strlen ($ word) -1) && $ trienode ['children'] [$ character] ['isword'] == false) { return false; } $ trienode = & $ trienode ['children'] [$ character]; } } }
/ ** * The location where the text $ text appears * * @param string $ text * @return array array ('position' => $ position, 'word' => $ word); * / function search ($ text = "") { $ textlen = strlen ($ text); $ trienode = $ tree = $ this-> trie; $ find = array (); $ wordrootposition = 0; // stem location $ prenode = false; / / backtracking parameters, when the dictionary ab, in the string aab, $ i need to backtrack back once $ word = ''; for ($ i = 0; $ i <$ textlen; $ i ++) {
if (isset ($ trienode ['children'] [$ text [$ i]])) { $ word = $ word. $ text [$ i]; $ trienode = $ trienode ['children'] [$ text [$ i]]; if ($ prenode == false) { $ wordrootposition = $ i; } $ prenode = true; if ($ trienode ['isword']) { $ find [] = array ('position' => $ wordrootposition, 'word' => $ word); } } else { $ trienode = $ tree; $ word = ''; if ($ prenode) { $ i = $ i -1; $ prenode = false; } } } return $ find; } } $ trie = new trie (); $ trie-> setword ('China'); $ trie-> setword ('Chinese'); $ trie-> setword ('great'); $ trie-> setword ('army'); $ trie-> setword ('Chinese people'); $ trie-> setword ('People's Liberation Army'); $ trie-> setword ('People's Liberation Army'); $ trie-> setword ('liberation'); $ words = $ trie-> search ('The great liberation of the Chinese People's Liberation Army of China is a very great army'); foreach ($ words as $ word) { echo 'position:'. $ word ['position'] .'- '(strlen ($ word [' word ']) + $ word [' position ']); echo 'word:'. $ word ['word']. "n"; }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.