3v4l.org

run code in 300+ PHP versions simultaneously
<?php //http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings class StringMatch{ public static function compare($a, $b){ $pairsA = self::wordLetterPairs($a); $pairsB = self::wordLetterPairs($b); $union = count($pairsA) + count($pairsB); $intersect = count(array_intersect($pairsA, $pairsB)); return 2*$intersect/$union; } public static function letterPairs($str){ $pairCount = strlen($str)-1; //don't compute every time in if $pairs = array(); for($i = 0; $i < $pairCount; $i++){ $pairs[] = substr($str, $i, 2); } return $pairs; } public static function wordLetterPairs($str){ $allPairs = array(); $str = explode(" ", $str); //get pairs in each word foreach($str as $w){ $pairs = self::letterPairs($w); //make sure, no doubled pairs are included foreach($pairs as $p){ $allPairs[$p] = $p; } } return array_values($allPairs); } } var_dump(StringMatch::compare("GutenTag Frau Fickerin", "Ficker Herr GutenTag"));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YSOR
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_STATIC_METHOD_CALL                                  'StringMatch', 'compare'
          2        SEND_VAL                                                 'GutenTag+Frau+Fickerin'
          3        SEND_VAL                                                 'Ficker+Herr+GutenTag'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Class StringMatch:
Function compare:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YSOR
function name:  compare
number of ops:  24
compiled vars:  !0 = $a, !1 = $b, !2 = $pairsA, !3 = $pairsB, !4 = $union, !5 = $intersect
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        INIT_STATIC_METHOD_CALL                                  'wordLetterPairs'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
    7     6        INIT_STATIC_METHOD_CALL                                  'wordLetterPairs'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $8      
          9        ASSIGN                                                   !3, $8
    9    10        COUNT                                            ~10     !2
         11        COUNT                                            ~11     !3
         12        ADD                                              ~12     ~10, ~11
         13        ASSIGN                                                   !4, ~12
   10    14        INIT_FCALL                                               'array_intersect'
         15        SEND_VAR                                                 !2
         16        SEND_VAR                                                 !3
         17        DO_ICALL                                         $14     
         18        COUNT                                            ~15     $14
         19        ASSIGN                                                   !5, ~15
   11    20        MUL                                              ~17     !5, 2
         21        DIV                                              ~18     ~17, !4
         22      > RETURN                                                   ~18
   12    23*     > RETURN                                                   null

End of function compare

Function letterpairs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 7
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 7
Branch analysis from position: 17
Branch analysis from position: 7
filename:       /in/9YSOR
function name:  letterPairs
number of ops:  19
compiled vars:  !0 = $str, !1 = $pairCount, !2 = $pairs, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        STRLEN                                           ~4      !0
          2        SUB                                              ~5      ~4, 1
          3        ASSIGN                                                   !1, ~5
   16     4        ASSIGN                                                   !2, <array>
   18     5        ASSIGN                                                   !3, 0
          6      > JMP                                                      ->15
   19     7    >   INIT_FCALL                                               'substr'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !3
         10        SEND_VAL                                                 2
         11        DO_ICALL                                         $10     
         12        ASSIGN_DIM                                               !2
         13        OP_DATA                                                  $10
   18    14        PRE_INC                                                  !3
         15    >   IS_SMALLER                                               !3, !1
         16      > JMPNZ                                                    ~12, ->7
   21    17    > > RETURN                                                   !2
   22    18*     > RETURN                                                   null

End of function letterpairs

Function wordletterpairs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 20
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 20
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 18
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/9YSOR
function name:  wordLetterPairs
number of ops:  26
compiled vars:  !0 = $str, !1 = $allPairs, !2 = $w, !3 = $pairs, !4 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        ASSIGN                                                   !1, <array>
   26     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !0, $6
   29     7      > FE_RESET_R                                       $8      !0, ->20
          8    > > FE_FETCH_R                                               $8, !2, ->20
   30     9    >   INIT_STATIC_METHOD_CALL                                  'letterPairs'
         10        SEND_VAR                                                 !2
         11        DO_FCALL                                      0  $9      
         12        ASSIGN                                                   !3, $9
   32    13      > FE_RESET_R                                       $11     !3, ->18
         14    > > FE_FETCH_R                                               $11, !4, ->18
   33    15    >   ASSIGN_DIM                                               !1, !4
         16        OP_DATA                                                  !4
   32    17      > JMP                                                      ->14
         18    >   FE_FREE                                                  $11
   29    19      > JMP                                                      ->8
         20    >   FE_FREE                                                  $8
   36    21        INIT_FCALL                                               'array_values'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                         $13     
         24      > RETURN                                                   $13
   37    25*     > RETURN                                                   null

End of function wordletterpairs

End of class StringMatch.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.03 ms | 1404 KiB | 23 Q