similar_text() 한자버전
//문자열 분할
함수 Split_str($str) {
Preg_match_all("/./u", $str, $arr)
$arr[0] 반환
}
//유사성 감지
함수 유사한_텍스트_cn($str1, $str2) {
$arr_1 = array_unique(split_str($str1))
$arr_2 = array_unique(split_str($str2))
$similarity = 개수($arr_2) - 개수(array_diff($arr_2, $arr_1))
$유사성 반환
}
levenshtein() 한자버전
//拆分字符串
function mbStringToArray($string, $encoding = 'UTF-8') {
$arrayResult = 배열();
while ($iLen = mb_strlen($string, $encoding)) {
array_push($arrayResult, mb_substr($string, 0, 1, $encoding));
$string = mb_substr($string, 1, $iLen, $encoding);
}
$arrayResult를 반환합니다.
}
//编辑距离
function levenshtein_cn($str1, $str2, $costReplace = 1, $encoding = 'UTF-8') {
$count_same_letter = 0;
$d = 배열();
$mb_len1 = mb_strlen($str1, $encoding);
$mb_len2 = mb_strlen($str2, $encoding);
$mb_str1 = mbStringToArray($str1, $encoding);
$mb_str2 = mbStringToArray($str2, $encoding);
for ($i1 = 0; $i1 <= $mb_len1; $i1 ) {
$d[$i1] = 배열();
$d[$i1][0] = $i1;
}
for ($i2 = 0; $i2 <= $mb_len2; $i2 ) {
$d[0][$i2] = $i2;
}
for ($i1 = 1; $i1 <= $mb_len1; $i1 ) {
for ($i2 = 1; $i2 <= $mb_len2; $i2 ) {
// $cost = ($str1[$i1 - 1] == $str2[$i2 - 1]) ? 0:1;
if ($mb_str1[$i1 - 1] === $mb_str2[$i2 - 1]) {
$ 비용 = 0;
$count_same_letter ;
} else {
$cost = $costReplace; //替换
}
$d[$i1][$i2] = min($d[$i1 - 1][$i2] 1, //插入
$d[$i1][$i2 - 1] 1, //删除
$d[$i1 - 1][$i2 - 1] $비용);
}
}
$d[$mb_len1][$mb_len2] 반환;
//return array('distance' => $d[$mb_len1][$mb_len2], 'count_same_letter' => $count_same_letter);
}
最长公共子序列LCS()
//最长公共子序列英文版
함수 LCS_en($str_1, $str_2) {
$len_1 = strlen($str_1);
$len_2 = strlen($str_2);
$len = $len_1 > $len_2 ? $len_1 : $len_2;
$dp = 배열();
for ($i = 0; $i <= $len; $i ) {
$dp[$i] = 배열();
$dp[$i][0] = 0;
$dp[0][$i] = 0;
}
for ($i = 1; $i <= $len_1; $i ) {
for ($j = 1; $j <= $len_2; $j ) {
if ($str_1[$i - 1] == $str_2[$j - 1]) {
$dp[$i][$j] = $dp[$i - 1][$j - 1] 1;
} else {
$dp[$i][$j] = $dp[$i - 1][$j] > $dp[$i][$j - 1] ? $dp[$i - 1][$j] : $dp[$i][$j - 1];
}
}
}
$dp[$len_1][$len_2] 반환;
}
//拆分字符串
function mbStringToArray($string, $encoding = 'UTF-8') {
$arrayResult = 배열();
while ($iLen = mb_strlen($string, $encoding)) {
array_push($arrayResult, mb_substr($string, 0, 1, $encoding));
$string = mb_substr($string, 1, $iLen, $encoding);
}
$arrayResult를 반환합니다.
}
//最长公共子序列中文版
함수 LCS_cn($str1, $str2, $encoding = 'UTF-8') {
$mb_len1 = mb_strlen($str1, $encoding);
$mb_len2 = mb_strlen($str2, $encoding);
$mb_str1 = mbStringToArray($str1, $encoding);
$mb_str2 = mbStringToArray($str2, $encoding);
$len = $mb_len1 > $mb_len2 ? $mb_len1 : $mb_len2;
$dp = 배열();
for ($i = 0; $i <= $len; $i ) {
$dp[$i] = 배열();
$dp[$i][0] = 0;
$dp[0][$i] = 0;
}
for ($i = 1; $i <= $mb_len1; $i ) {
for ($j = 1; $j <= $mb_len2; $j ) {
if ($mb_str1[$i - 1] == $mb_str2[$j - 1]) {
$dp[$i][$j] = $dp[$i - 1][$j - 1] 1;
} else {
$dp[$i][$j] = $dp[$i - 1][$j] > $dp[$i][$j - 1] ? $dp[$i - 1][$j] : $dp[$i][$j - 1];
}
}
}
$dp[$mb_len1][$mb_len2] 반환;
}