Home > Backend Development > PHP Tutorial > 求二句文字的同异词组分词(求算法思路)

求二句文字的同异词组分词(求算法思路)

WBOY
Release: 2016-06-13 13:05:30
Original
1057 people have browsed it

求2句文字的同异词组分词(求算法思路)
语句1 “CSDN论坛是一个非常不错的论坛”
语句2 “CSDN是一个不错的坛子”

求同异后为以下结果

CSDN=>2
论坛=>2
是一个=>2
非常=>1
不错的=>2
坛子=>1

------解决方案--------------------

PHP code
$a = 'CSDN论坛是一个非常不错的论坛';
$b = 'CSDN是一个不错的坛子';

print_r(sunday($a, $b));

function sunday($a, $b) {
    //检查传入串的字符集
    mb_internal_encoding( mb_check_encoding($a, 'utf-8') ? 'utf-8' : 'gbk' );
    //将传入串拆分成数组,主要是为了下面打代码书写起来方便点
    $t = array();
    for($i=0; $i<mb_strlen mb_substr array for do while count if>= count($a)) break;
                }elseif($k) break;
            }

            //检查匹配的情况
            if($n - $i > 1) {
                if($buf) {
                    $r[] = $buf;
                    $buf = '';
                }
                $r[] = join('',array_slice($a, $i, $n-$i));
                $i = $n;
            }else $buf .= $a[$i++];
        }
        if($buf) $r[] = $buf;

        list($b, $a) = array($a, $b); //交换两组数据,再来一遍
    }while($loop--);
    return array_count_values($r);
} <div class="clear">
                 
              
              
        
            </div></mb_strlen>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template