dongzuan4917 2017-08-03 08:55
浏览 31
已采纳

PHP - Group Strings By Similarity / Substring [关闭]

PHP

Hi, I have been struggling with this problem for awhile and can not find a solution to it and was wondering if anyone could help.

I need to group similar strings for example:

Input

Slim Aluminium HDMI Lead, 1m Blue
Slim Aluminium HDMI Lead, 2m Blue
Slim Aluminium HDMI Lead, 3m Blue
Frozen Kids Headphones with Volume Limiter
XLR Plug to Socket Lead, 3m
XLR Plug to Socket Lead, 6m
Monster High Kids Headphones with Volume Limiter
TMNT Kids Headphones with Volume Limiter
Batman Kids Headphones with Volume Limiter
1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm
Slim Aluminium HDMI Lead, 5m Blue
Slim Aluminium HDMI Lead, 7.5m Blue
6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange
XLR Plug to Socket Lead, 0.5m
XLR Plug to Socket Lead, 1m
XLR Plug to Socket Lead, 2m

Output (Grouped In Array)

Slim Aluminium HDMI Lead, 1m Blue
Slim Aluminium HDMI Lead, 2m Blue
Slim Aluminium HDMI Lead, 3m Blue
Slim Aluminium HDMI Lead, 5m Blue
Slim Aluminium HDMI Lead, 7.5m Blue

XLR Plug to Socket Lead, 0.5m
XLR Plug to Socket Lead, 1m
XLR Plug to Socket Lead, 2m
XLR Plug to Socket Lead, 3m
XLR Plug to Socket Lead, 6m

1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm

Frozen Kids Headphones with Volume Limiter
Monster High Kids Headphones with Volume Limiter
TMNT Kids Headphones with Volume Limiter
Batman Kids Headphones with Volume Limiter

6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange

  • 写回答

1条回答 默认 最新

  • douhe4608 2017-08-03 09:10
    关注

    Update: I've found a better solution

    I've never played with php's similar_text() function but I thought I'd give it a shot...

    $array = explode(PHP_EOL,'Slim Aluminium HDMI Lead, 1m Blue
    Slim Aluminium HDMI Lead, 2m Blue
    Slim Aluminium HDMI Lead, 3m Blue
    Frozen Kids Headphones with Volume Limiter
    XLR Plug to Socket Lead, 3m
    XLR Plug to Socket Lead, 6m
    Monster High Kids Headphones with Volume Limiter
    TMNT Kids Headphones with Volume Limiter
    Batman Kids Headphones with Volume Limiter
    1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
    2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
    1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
    2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm
    Slim Aluminium HDMI Lead, 5m Blue
    Slim Aluminium HDMI Lead, 7.5m Blue
    6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange
    XLR Plug to Socket Lead, 0.5m
    XLR Plug to Socket Lead, 1m
    XLR Plug to Socket Lead, 2m');
    
    $output = array();
    while( empty( $array ) === false )
    {
      $currentWord = array_shift( $array );
      $currentGroup = array( $currentWord );
      foreach( $array as $index => $word )
      {
        if( similar_text( $word, $currentWord, $percentage ) and $percentage > 80 )
        {
          $currentGroup[] = $word;
          unset( $array[ $index ] );
        }
      }
      $output[] = $currentGroup;
    }
    
    print_r($output);
    

    // Array
    // (
    //     [0] => Array
    //         (
    //             [0] => Slim Aluminium HDMI Lead, 1m Blue
    //             [1] => Slim Aluminium HDMI Lead, 2m Blue
    //             [2] => Slim Aluminium HDMI Lead, 3m Blue
    //             [3] => Slim Aluminium HDMI Lead, 5m Blue
    //             [4] => Slim Aluminium HDMI Lead, 7.5m Blue
    //         )
    // 
    //     [1] => Array
    //         (
    //             [0] => Frozen Kids Headphones with Volume Limiter
    //             [1] => Monster High Kids Headphones with Volume Limiter
    //             [2] => TMNT Kids Headphones with Volume Limiter
    //             [3] => Batman Kids Headphones with Volume Limiter
    //         )
    // 
    //     [2] => Array
    //         (
    //             [0] => XLR Plug to Socket Lead, 3m
    //             [1] => XLR Plug to Socket Lead, 6m
    //             [2] => XLR Plug to Socket Lead, 0.5m
    //             [3] => XLR Plug to Socket Lead, 1m
    //             [4] => XLR Plug to Socket Lead, 2m
    //         )
    // 
    //     [3] => Array
    //         (
    //             [0] => 1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
    //             [1] => 2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
    //             [2] => 1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
    //             [4] => 2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm
    //         )
    // 
    //     [4] => Array
    //         (
    //             [0] => 6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange
    //         )
    // 
    // )
    

    Assoc array edit

    $products = array(
      array('id'=>'A','name'=>'Slim Aluminium HDMI Lead, 1m Blue'),
      array('id'=>'B','name'=>'Slim Aluminium HDMI Lead, 2m Blue'),
      array('id'=>'C','name'=>'Slim Aluminium HDMI Lead, 3m Blue'),
      array('id'=>'D','name'=>'1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm'),
      array('id'=>'E','name'=>'2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm'),
      array('id'=>'F','name'=>'1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm'),
      array('id'=>'G','name'=>'2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm'),
      array('id'=>'H','name'=>'Slim Aluminium HDMI Lead, 5m Blue'),
      array('id'=>'I','name'=>'Slim Aluminium HDMI Lead, 7.5m Blue')
    );
    $output = array();
    while( empty( $products) === false )
    {
      $currentProduct = array_shift( $products );
      $currentGroup = array( $currentProduct );
      foreach( $products as $index => $product )
      {
        if( similar_text( $product['name'], $currentProduct['name'], $percentage ) and $percentage > 80 )
        {
          $currentGroup[] = $product;
          unset( $products[ $index ] );
        }
      }
      $output[] = $currentGroup;
    }
    print_r($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行