相似度匹配

<?php
header("Content-type: text/html; charset=utf-8");

set_time_limit(0);//设置不超时
@ini_set('memory_limit', '512M');//设置PHP能使用的内存大小


    similar_text("中国人民","中国共产",$percent);

    //读取上传文件内容
    $file_path = './str1.txt';
    $handle = fopen($file_path, 'r') or sys_msg('文件读取失败!');
    $keyarr = array();
    if ($handle) 
    {
        while (!feof($handle)) 
        {
            $buffer = fgets($handle, 4096);
            if(!empty($buffer))
            {
                $keyarr[] = explode(',', $buffer);
            }
        }

        fclose($handle);
    }
    
//     var_dump($keyarr);
    $file_path2 = './str2.txt';
    $handle2 = fopen($file_path2, 'r') or sys_msg('文件读取失败!');
    $keyarr2 = array();
    if ($handle2) 
    {
        while (!feof($handle2)) 
        {
            $buffer2 = fgets($handle2, 4096);
            if(!empty($buffer2))
            {
                $keyarr2[] = explode('|', $buffer2);
            }
        }

        fclose($handle2);
    }

   $newArr = array();
   foreach($keyarr as $k => $v)
   {
       foreach($keyarr2 as $k2 => $v2)
       {
           similar_text($v[1], $v2[3], $percent);
           $percent = round($percent);
           if($percent >= 95)
           {
               $newArr[] = array(
                   '0' => $v2[2],
                   '1' => $v[0],
                   '2' => $v[1],
                   '3' => $v[2],
               );
           }
       }
   }
   $newstr = '';
   foreach($newArr as $k3 => $v3)
   {
       $newstr .= implode('|', $v3);
   }
   
  
   $stream = fopen("newfile.txt", "w+");
   fwrite($stream, $newstr);
   echo "成功!";
   
   
   
   
   
   
   
   
   

 

posted @ 2016-06-03 15:20  Abner3721  阅读(275)  评论(0编辑  收藏  举报