【问题标题】:find matching link in text file with php用php在文本文件中找到匹配的链接
【发布时间】:2012-07-29 02:58:44
【问题描述】:

我有一个函数可以读取文本文件并与目录搜索交叉匹配,以计算描述(文本文件)和文件的目录索引。我使用 leveltensin 函数来提供一些模糊逻辑,因此名称不需要 100% 相同,但我遇到了一个障碍,因为我现在设置了它,我正在修复记忆墙,因为当我取消注释行时在它下面搜索整个 txt 文件并将每个 ling 与目录文件名进行比较。超过 700 个文件每个都被检查了 700 次,我很快就耗尽了内存。我需要一些方法来跳出 while (!feof($file_handle) ) 当它找到一个匹配然后找到一些方法来设置下一次传递的起点到我们停止它的行位置所以它是循环 0-700每一次

function GenerateList($titleB, $descB, $thumbB, $dirB, $patternB){
$outputB = "<CATEGORY name=\"$titleB\" desc=\"$descB\" thumb=\"$thumbB\">";
$open_error = 0;

if (is_dir($dirB)){
$myDirectory = opendir($dirB);
// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);

// sort em
sort($dirArray);
// loop through the array of files and print them all
if (!($text = file_get_contents("Scripts/descriptions.txt"))){$open_error = 1;}
$results = array();
for($index=0; $index < $indexCount; $index++) {
    $ext = explode(".", $dirArray[$index]);
    $parsed_title = preg_replace ($patternB, "", $ext[0]);
    if ((substr("$dirArray[$index]", 0, 1) != ".")&&($ext[1] == "flv")){ // don't list hidden files

//if ($open_error == 0){
//  $file_handle = fopen("Scripts/descriptions.txt", "rb");

//while (!feof($file_handle) ) {
//$line_of_text = fgets($file_handle);
//$parts = explode('|', $line_of_text);
/*
echo "<PRE>";
echo strtolower($parts[0]);
echo "</br>";
echo strtolower($parsed_title);
echo "</br>";
echo "</PRE>";
*/
//if ((wordMatch(strtolower($parts[0]), strtolower($parsed_title), 2)) > 0){
        $outputB .= "<ITEM>";
        $outputB .= "<file_path>/Sources/Power Rangers/$dirB".$dirArray[$index]."</file_path>";
        $outputB .= "<file_width>500</file_width>";
        $outputB .= "<file_height>375</file_height>";
        $outputB .= "<file_title>".$parsed_title."</file_title>";
//      $outputB .= "<file_desc>".$parts[1]."</file_desc>";
        $outputB .= "<file_desc>test</file_desc>";
//      $outputB .= "<file_image>".$match_result[2]."</file_image>";
        $outputB .= "<file_image>$thumbB</file_image>";
//      $outputB .= "<featured_image>".$match_result[3]."</featured_image>";
        $outputB .= "<featured_image>$thumbB</featured_image>";
//      $outputB .= "<featured_or_not>".$parts[4]."</featured_or_not>";
        $outputB .= "<featured_or_not>true</featured_or_not>";
        $outputB .= "</ITEM>";
//};//if ((wordMatch($parts[0], strtolower($word), 2) > 0)
//};//while
//fclose($file_handle);

//};//if ($open_error == 0)
    };//if ((substr("$dirArray[$index]", 0, 1) != ".")&&($ext[1] == "flv"))
};//for($index=0; $index < $indexCount; $index++) 
};//if (file_exists($dirB))
$outputB .= "</CATEGORY>";
return $outputB;
};//function

    function wordMatch($words, $input, $sensitivity){ 
        $shortest = -1; 
        foreach ($words as $word) { 
            $lev = levenshtein($input, $word); 
            if ($lev == 0) { 
                $closest = $word; 
                $shortest = 0; 
                break; 
            } //if
            if ($lev <= $shortest || $shortest < 0) { 
                $closest  = $word; 
                $shortest = $lev; 
            } //if
        } //foreach
        if($shortest <= $sensitivity){ 
            return $closest; 
        } else { 
            return 0; 
        } //if/else
    } // function, http://php.net/manual/en/function.levenshtein.php

【问题讨论】:

  • 你如何定义“80%”?一个正则表达式要么匹配要么不匹配。
  • 如果 $parsed 是 "Peace Love and Woe" 并且匹配的是 "Peace Love & Woe" 或 "Peace Love and Woe" 或 "Peace Love and Woe.avi" 这应该是棘手的部分有效
  • 所以......您的“80%”规则与其说是已定义的规则,不如说是您希望帮助定义的东西?你考虑过fuzy logic吗?您将无法在正则表达式中实现它,但它可能会让您更接近目标。此外,包括一些示例数据(以及您对它所代表的匹配程度的想法)将使编写实际符合您要求的内容变得更加容易。
  • 示例很容易解析的数据是这里的左侧菜单maskedriders.info/Sources/Power%20Rangers 而文件在这里maskedriders.info/Sources/Power%20Rangers/Scripts/… 我的想法是我想在 txt 中匹配解析的标题,这样我就可以加载到我在 | 之后的其他字段中的 xml 数据在 txt 上
  • 至于模糊逻辑,在解释后我在想“如果只是去掉 ehite 空格,(),并将任何 & 转换为 and 并将其全部转换为小写”,它应该满足 80% 的规则问题是我必须对文本文件做同样的事情,但只能在 | 之前

标签: php regex file-get-contents


【解决方案1】:

您可以计算两个项目之间的edit distance,而不是正则表达式。然后,您的 80% 启发式相当于说 (length-edit_distance)/length &gt;= .8 其中 length 是您尝试匹配的字符串的长度。

因此,如果字符串长度为 20 个字符且与目标的编辑距离为 2,则您将计算出 (20-2) / 20 == .9 换句话说,该项目与您的目标的匹配率为 90%。这高于 0.8,因此您接受它作为匹配项。

请注意,“编辑距离”也称为Levenshtein distance,因此您只需执行以下操作:

$len = (float) strlen($target);  // Avoids integer division.
$match = ($len-levenshtein($input, $target))/$len;

if ($match >= 0.8) {
  // The $input matches our $target
}

【讨论】:

  • 好主意。如果您可以包含(或指向)一些计算 PHP 中字符串之间的编辑距离的示例代码,那肯定会让您获得 StackOverflow 积分。 :)
  • 还有在 txt 文件中搜索与 huristics 匹配的链接的问题,我真的很想避免将 extire TXT 加载到变量或数组中,只是为了丢弃其中的大部分,因为我必须循环大约 700 次以填充每个条目的 xml 文件
  • 我认为编辑距离已经是 PHP 函数了? php.net/manual/en/function.levenshtein.php 至于其他的,你的链接不都是http://或者https://开头的吗?你可以把所有看起来像链接的东西都拉出来,然后做你模糊匹配的东西。
  • 好的,我想我到了某个地方,但我遇到了障碍,请检查修改后的 OP 以查看问题
  • 有什么障碍?这个问题在我看来是一样的吗?
猜你喜欢
  • 2015-06-24
  • 1970-01-01
  • 2023-01-12
  • 2020-03-03
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多