ホームページ  >  記事  >  バックエンド開発  >  PHP の類似テキスト関数を分析する_PHP チュートリアル

PHP の類似テキスト関数を分析する_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 17:36:13886ブラウズ

//文字列を比較し、2 つの文字列の最初の文字が同じである領域を返します
static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)
{
char *p, *q;
char *end1 = (char *) txt1 + len1;
char *end2 = (char *) txt2 + len2;
int l;

*max = 0 ;
//文字列
for (p = (char *) txt1; p < end1; p++) {
for (q = (char *) txt2; q < end2; q++) {
for (l = 0 ; (p + l if (l > *max) {
//保存同じ地域情報
*max = l;
*pos1 = p -txt1;
*pos2 = q -txt2;
//再帰関数、txt1 と txt2 内の同一文字の数を比較します
static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2)
{
int sum;

int pos1, pos2, max ;


php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
if ((sum = max)) {//このように書く意味は何ですか????!!!!!!

//最後の異なる部分の前部分を再帰します

if (pos1 && pos2) {

sum += php_similar_char(txt1, pos1, txt2, pos2);
}

//最後の異なる部分の後部を再帰します
if ((pos1 + max < len1) && (pos2 + max < len2)) {
sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max ,
txt2 + pos2 + max, len2 - pos2 - max);

}

}

//この比較後の同一文字の数を返します
return sum;
}

//PHP 関数自体は一連のマクロであり、文字列変換と戻り値変換を実行します。メイン関数は上記の 2 つの関数によって実行されます。

PHP_FUNCTION(similar_text)
{
zval **t1, **t2, **percent;

int ac = ZEND_NUM_ARGS();

int sim;

if (ac < 2 || ac > 3 || zend_get_parameters_ex (ac, &t1, &t2, &percent) == 失敗) {
WRONG_PARAM_COUNT;
}

convert_to_string_ex(t1);
convert_to_string_ex(t2);

if (ac > 2) {

Convert_to_double_ex(percent);

}

if (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2) == 0) {

if (ac > 2) {
Z_DVAL_PP(percent) = 0;
}

RETURN_LONG(0);
}

sim = php_similar_char(Z_STRVAL_PP(t1), Z_STRLEN_PP(t1), Z_STRVAL_PP(t2), Z_STRLEN_PP(t2));

if (ac > 2) {

Z_DVAL_PP(パーセント) = sim * 200.0 / (Z_STRLEN_PP(t1) + Z_STRLEN_PP(t2));
}

RETURN_LONG(シム);

}



similar_text は ASCII に基づいて比較されており、フレーズの問題は考慮されていないことがわかります。

テストした結果は次のとおりです:

int main(int argc, char *argv[])

{
char *str1="weican wuxin";

char *str2="wuxin weican";

int pos1,pos2,max;

php_similar_str(str1,strlen (str1),str2,strlen(str2),&pos1,&pos2,&max);
printf("php_similar_str:%d,%d=%d ",pos1,pos2,max);

max=php_similar_char(str1,strlen(str1),str2,strlen(str2));
printf("php_similar_char:%d ",max);

system("PAUSE");
return 0;
}

php_similar_str:0,6=6
php_similar_char:6

char *str1="TCP プロトコル通信の動作原理";
char *str2="TCP プロトコル通信の動作原理";

php_similar_str:3,4=14
php_similar_char:19



http://www.bkjia.com/PHPjc/508234.html

www.bkjia.com

tru​​e

技術記事 //文字列を比較し、2 つの文字列の最初の文字が同じである領域を返します。 static void php_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max ) ...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。