99久久国产露脸精品麻豆,欧美日韩精品小说,亚洲免费在线美女视频,国产三级中文字幕,91极品国产情侣高潮对白,国产亚洲一区二区三区不卡片,欧美jizz精品欧美性,久久国产精品久久国产片

php數(shù)組字段相似度排序

管理員 3637次瀏覽

摘要:今天有一個客戶有這么一個需求,要求對某篇文章的相關(guān)內(nèi)容的列表,在跟文章標(biāo)題進(jìn)行相似度排序,于是想了又想,寫了一個小算法,可以根據(jù)關(guān)鍵字相似度對數(shù)組某個字段進(jìn)行排序...

今天有一個客戶有這么一個需求,要求對某篇文章的相關(guān)內(nèi)容的列表,在跟文章標(biāo)題進(jìn)行相似度排序,于是想了又想,寫了一個小算法,可以根據(jù)關(guān)鍵字相似度對數(shù)組某個字段進(jìn)行排序。

廢話少說,直接上代碼:

//原數(shù)據(jù)
$data = [
	[
		'id'=>1,
		'title'=>'YzmCMS內(nèi)容管理系統(tǒng)',
	],
	[
		'id'=>2,
		'title'=>'開源CMS',
	],
	[
		'id'=>3,
		'title'=>'YzmCMS輕量級開源內(nèi)容管理系統(tǒng)',
	],
	[
		'id'=>4,
		'title'=>'內(nèi)容管理系統(tǒng)',
	],
	[
		'id'=>5,
		'title'=>'免費(fèi)內(nèi)容管理系統(tǒng)',
	],
	[
		'id'=>6,
		'title'=>'YzmCMS開源CMS',
	],
	[
		'id'=>7,
		'title'=>'免費(fèi)CMS',
	],
	[
		'id'=>8,
		'title'=>'輕量級開源CMS',
	],
	[
		'id'=>9,
		'title'=>'YzmCMS建站CMS',
	],
	[
		'id'=>10,
		'title'=>'免費(fèi)開源CMS',
	],
];


處理方法:

/**
 * 根據(jù)關(guān)鍵字對數(shù)組字段進(jìn)行相似度排序
 * @param   $array   原數(shù)組
 * @param   $keyword 關(guān)鍵字
 * @param   $arr_key 要匹配的數(shù)組字段名
 * @return  array    排序好的數(shù)組
 */
function similar_arr($array, $keyword, $arr_key = 'title'){
    //數(shù)組key小于3,直接返回,不符合排序要求(特例,可不寫)
	if(count($array)<= 3){
		return $array;
	}
	
    //數(shù)組相似度處理
	foreach ($array as $key => $value) {
		similar_text($value[$arr_key], $keyword, $percent);
		$value['percent'] = $percent;
		$data[] = $value;
		
	}

	//取出數(shù)組中percent的一列,返回一維數(shù)組
	$percent =  array_column($data, 'percent'); 

	//排序,根據(jù) percent 排序
	array_multisort($percent, SORT_DESC, $data);
	return $data;
}


使用方法:

$res = similar_arr($data, 'YzmCMS');
var_dump($res);


運(yùn)行結(jié)果:

array(10) {
  [0]=>
  array(3) {
    ["id"]=>
    int(6)
    ["title"]=>
    string(15) "YzmCMS開源CMS"
    ["percent"]=>
    float(57.142857142857)
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(9)
    ["title"]=>
    string(15) "YzmCMS建站CMS"
    ["percent"]=>
    float(57.142857142857)
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(24) "YzmCMS內(nèi)容管理系統(tǒng)"
    ["percent"]=>
    float(40)
  }
  [3]=>
  array(3) {
    ["id"]=>
    int(2)
    ["title"]=>
    string(9) "開源CMS"
    ["percent"]=>
    float(40)
  }
  [4]=>
  array(3) {
    ["id"]=>
    int(7)
    ["title"]=>
    string(9) "免費(fèi)CMS"
    ["percent"]=>
    float(40)
  }
  [5]=>
  array(3) {
    ["id"]=>
    int(10)
    ["title"]=>
    string(15) "免費(fèi)開源CMS"
    ["percent"]=>
    float(28.571428571429)
  }
  [6]=>
  array(3) {
    ["id"]=>
    int(3)
    ["title"]=>
    string(39) "YzmCMS輕量級開源內(nèi)容管理系統(tǒng)"
    ["percent"]=>
    float(26.666666666667)
  }
  [7]=>
  array(3) {
    ["id"]=>
    int(8)
    ["title"]=>
    string(18) "輕量級開源CMS"
    ["percent"]=>
    float(25)
  }
  [8]=>
  array(3) {
    ["id"]=>
    int(4)
    ["title"]=>
    string(18) "內(nèi)容管理系統(tǒng)"
    ["percent"]=>
    float(0)
  }
  [9]=>
  array(3) {
    ["id"]=>
    int(5)
    ["title"]=>
    string(24) "免費(fèi)內(nèi)容管理系統(tǒng)"
    ["percent"]=>
    float(0)
  }
}


隨機(jī)內(nèi)容

表情

共4條評論