【问题标题】:Simple string matching in PHPPHP中的简单字符串匹配
【发布时间】:2014-10-15 22:29:24
【问题描述】:

我有一个文件名列表和一个我想相互匹配的标题列表。 (对于我正在编写的电视节目跟踪应用程序)

例子:

[Commie] Psycho-Pass 2 - 01 [495A3950].mkv //filename
Psycho-Pass 2 // title it should be matched to

[UTW]_Fate_Kaleid_Liner_Prisma_Ilya_2wei_-_01_[h264-720p][34F564F6].mkv
Fate Kaleid Liner Prisma Ilya 2wei

The.Big.Bang.Theory.S08E05.720p.HDTV.X264-DIMENSION[rartv]
The Big Bang Theory

Modern.Family.S06E03.720p.HDTV.x264-KILLERS[rartv]
Modern Family

我发现正则表达式是一个有点乏味的解决方案,因为文件名格式并不总是相同的。我正在考虑一个比较,其中系统将根据置信度测量(百分比阈值)来决定。实际标题是在数据库中预定义的(没有剧集编号)。我基本上需要将文件名与标题匹配。

如果没有必要,我不想走机器学习的道路;)

有什么想法吗?

【问题讨论】:

  • 你能提供一些真实的例子吗?如果您发布要匹配的 5-6 个实际字符串,我认为您不会放弃太多有关您的应用程序的信息。而且,正则表达式可能是您最有可能的情况。只是让你热身。
  • PHP,作为解释器,根据定义并不快。你最好的选择是依靠正则表达式的东西,我认为它是用 C 实现的;它本身可能实际上很快。
  • 运行速度不是问题。快速我实际上是指简单快速地编写。每天只需要匹配几个标题

标签: php pattern-matching


【解决方案1】:

下面的简单方法不行吗?

for each $title
    $count = 0
    for each $word in $title
        if $word in $filename:
            $count++

    /* additive error */
    if count >= (number of words in title) - $some_alpha:
        /* found matching title */

    /* multiplicative error */
    if count / (number of words in title) >= $some_percentage:
        /* found matching title */

或者您正在寻找更复杂的东西?

【讨论】:

    【解决方案2】:

    经过一番研究,我偶然发现了levenshtein php 方法: http://php.net/manual/en/function.levenshtein.php

    由于我已经在数据库中填充了节目名称,并且我只想匹配文件名,因此我可以使用该方法遍历每个节目名称并选择最合适的名称!

    【讨论】:

      【解决方案3】:

      根据您的文本,您有一个数据库,其中包含已存储的标题列表。现在您想将它们与 file_names 匹配。下面,我有可以做到这一点的代码。我已经使用了匹配,并且在匹配的情况下您将要做的事情放在不匹配的地方。

      您要做的第一件事是清理文件名,然后将标题与文件名匹配。在这种情况下,我只会说您正在尝试将标题与您列表中名为 [Commie] Psycho-Pass 2 - 01 [495A3950].mkv 的文件名匹配。代码如下所示。您可以复制和粘贴,它会起作用。

      /** list of titles from the database**/ 
      $title_array = ["Psycho-Pass 2", "Fate Kaleid Liner Prisma Ilya 2wei", "The Big Bang Theory", "Modern Family"];
      
      /** filename you want to match with the titles **/
      $filename_raw = "[Commie] Psycho-Pass 2 - 01 [495A3950].mkv";
      
      /**
       * Clean the $filename
       * Replace the dot and underscore with space, and remember to escape the characters, because they are special
       * Here we just have a variable holding the pattern we need to replace and the replacement
       **/ 
      $patterns = array ('/\./','/\_/');
      $replace = array (' ', ' ');
      /** 
       * this is were replacement occurs
       **/
      $filename_clean = preg_replace($patterns, $replace, $filename_raw);
      
      foreach($title_array as $title){
          if (strpos($filename_clean,$title) !== false) {
              echo "Match <br />";
              /**
               * you might want to put a break here since your have already found the match but I will leave that up to you
               */
          }else{
              echo "Match Not found<br />";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 2011-09-04
        • 2023-03-11
        • 2012-10-17
        • 1970-01-01
        相关资源
        最近更新 更多