PHP One-yuan word segmentation algorithm _php skills

Source: Internet
Author: User

Copy Code code as follows:


/**
* One-yuan word segmentation algorithm
* UTF8 encoded next character if the first character ASCII code is not greater than 192, it only occupies 1 bytes.
* If the first character ASCII code is greater than 192 or less than 224, it occupies 2 bytes, otherwise it occupies 3 bytes
* One-yuan participle needs to be added to the MySQL My.ini file ft_min_word_len=1
* Can use MySQL query statement show variables like '%ft% ' view MySQL full-text search Related Settings
*
* @access Global
* @param string $str
* @param boolean $unique whether to remove duplicate values
* @param Boolean $merge whether to combine added value
* @return Array
*/
function Seg_word ($str, $unique =false, $merge =true)
{
$str = Trim (Strip_tags ($STR));
$strlen = strlen ($STR);
if ($strlen = = 0) return array ();
$SPC = ';
Increase the number of characters you need to filter on demand
$search = Array (', ', '/', ' \ \ ', '. ', '; ', ': ', ', ', ', ', ', ', '! '), ' ~ ', ' ', ' ', ', ' ^ ', ' (', ') ', '? ', '-', ' \ t ', ' \ n ', ' \ ', ' < ', ' > ', ' \ R ', ' \ r \ n ', ' \$ ', ' & ', '% ', ' # ', ' @ ', ' + ', ' = ', ' {', '} ', ' [', '] ', ', ', '. ', '。 ', ',', '! ', ';', '“', '”', '‘', ''', '[', ']', '、', '—', ' ', '《', '》', '-', '...', '【', '】',':');
$numpairs = Array (' 1 ' => ' one ', ' 2 ' => ' II ', ' 3 ' => ' III ', ' 4 ' => ' IV ', ' 5 ' => ' V ', ' 6 ' => ', ' 7 ', ' => ', ' 8 ', ' => ' Viii. ', ' 9 ' => ' IX ', ' 0 ' => ' 0 ');
$str = Alab_num ($STR);
$str = Str_replace ($search, ', $str);
$ord = $i = $k = 0;
$prechar = 0;//0-blank 1-English and Chinese symbols
$result = Array ();
$annex = Array ();

while ($ord = Ord ($str [$i]))
{
1-byte characters
if ($ord <= 0xc0)
{
To remove an empty string
if ($ord < 33) {
$prechar = 0;
$i + +;
$k + +;
Continue
}
Append Chinese capital Number conversion
if (Isset ($numpairs [$STR [$i]]) {
$annex []= $numpairs [$STR [$i]];
}
If the front is Chinese
if ($prechar = = 2) {
$result [+ + $k] = $str [$i];
}
else {
$result [$k]. = $str [$i];
}
$prechar = 1;
$i + +;
}
else//2-3 Byte character (Chinese)
{
if ($ord < 0XE0)
$step = 2;
Else
$step = 3;
$c = substr ($str, $i, $step);
if (false!== $key = Array_search ($c, $numpairs)) {
$annex [] = $key;
}
if ($prechar!= 0) {
$result [+ + $k] = $c;
}
else {
$result [$k]. = $c;
}

$prechar = 2;
$i + + $step;
}
}
$result = $merge? Array_merge ($result, $annex): $result;
Return $unique? Array_unique ($result): $result;
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.