3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace App\SemanticAnalysis; /** * Class NoMatchesFoundException * @package App\SemanticAnalysis */ class NoMatchesFoundException extends \Exception {} /** * Class SemanticAnalysis * @package App\SemanticAnalysis */ class SemanticAnalysis { /** * @var */ private $criteria; /** * @var string */ private $text; /** * @var array */ private $matches; /** * @param $criteria * @param $text */ public function __construct($criteria, $text) { $this->criteria = $criteria; $this->text = strtolower($text); $this->matches = []; } /** * @throws NoMatchesFoundException */ public function analyse() { if (preg_match_all($this->buildRegex(), $this->text, $matches) == false) { throw new NoMatchesFoundException(); } $this->matches = $matches['token']; } /** * @return int */ public function getScore() { return $this->scoreMatches(); } /** * @param string $regex * @return string */ private function buildRegex($regex = "") { foreach ($this->criteria as $value) { $regex .= $value[0] . "|"; } return "/(?<token>".rtrim($regex, "|").")/iu"; } /** * @param int $score * @return int */ private function scoreMatches($score = 0) { foreach ($this->criteria as $row) { if (in_array($row[0], $this->matches)) { $score+=$row[1]; } } return $score; } } // TestCase $criteria = [ ['beautiful place', 1], ['amazing location', 1], ['superb service', 1], ['international hotel', 1], ['perfect location', 1], ['terrible', -1] ]; $text = "I have stayed here 4 or 5 times while visiting LA. Despite travelling all over the world and staying in some of the best international hotels ( for work), Hotel Caliornia is one of my absolute favourites. Perfect location, right on the beach. I love the way you can just open your door and be outside, no elevators, corridors big glass windows. The ambience is so nice, retro perfect. As for the staff, I have had consistently superb service, with much more personal service and attention to detail than is usual in bigger hotels. Aaron and Katy were just two who have been exemplary this time but really everyone is terrific. Can't recommend it highly enough."; $semanticAnalysis = new \App\SemanticAnalysis\SemanticAnalysis($criteria, $text); try { $semanticAnalysis->analyse(); var_dump($semanticAnalysis->getScore()); var_dump($semanticAnalysis->getMatches()); } catch (NoMatchesFoundException $e) { print $e->getMessage(); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 20
Branch analysis from position: 20
2 jumps found. (Code = 107) Position 1 = 21, Position 2 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jj3r7
function name:  (null)
number of ops:  25
compiled vars:  !0 = $criteria, !1 = $text, !2 = $semanticAnalysis, !3 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   ASSIGN                                                   !0, <array>
  108     1        ASSIGN                                                   !1, 'I+have+stayed+here+4+or+5+times+while+visiting+LA.+Despite+travelling+all+over+the+world+and+staying+in+some+of+the+best+international+hotels+%28+for+work%29%2C+Hotel+Caliornia+is+one+of+my+absolute+favourites.+Perfect+location%2C+right+on+the+beach.+I+love+the+way+you+can+just+open+your+door+and+be+outside%2C+no+elevators%2C+corridors+big+glass+windows.+The+ambience+is+so+nice%2C+retro+perfect.+As+for+the+staff%2C+I+have+had+consistently+superb+service%2C+with+much+more+personal+service+and+attention+to+detail+than+is+usual+in+bigger+hotels.+Aaron+and+Katy+were+just+two+who+have+been+exemplary+this+time+but+really+everyone+is+terrific.+Can%27t+recommend+it+highly+enough.'
  111     2        NEW                                              $6      'App%5CSemanticAnalysis%5CSemanticAnalysis'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $6
  114     7        INIT_METHOD_CALL                                         !2, 'analyse'
          8        DO_FCALL                                      0          
  116     9        INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Cvar_dump'
         10        INIT_METHOD_CALL                                         !2, 'getScore'
         11        DO_FCALL                                      0  $10     
         12        SEND_VAR_NO_REF_EX                                       $10
         13        DO_FCALL                                      0          
  117    14        INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Cvar_dump'
         15        INIT_METHOD_CALL                                         !2, 'getMatches'
         16        DO_FCALL                                      0  $12     
         17        SEND_VAR_NO_REF_EX                                       $12
         18        DO_FCALL                                      0          
         19      > JMP                                                      ->24
  119    20  E > > CATCH                                       last         'App%5CSemanticAnalysis%5CNoMatchesFoundException'
  120    21    >   INIT_METHOD_CALL                                         !3, 'getMessage'
         22        DO_FCALL                                      0  $14     
         23        ECHO                                                     $14
  121    24    > > RETURN                                                   1

Class App\SemanticAnalysis\NoMatchesFoundException: [no user functions]
Class App\SemanticAnalysis\SemanticAnalysis:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jj3r7
function name:  __construct
number of ops:  12
compiled vars:  !0 = $criteria, !1 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   38     2        ASSIGN_OBJ                                               'criteria'
          3        OP_DATA                                                  !0
   39     4        INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Cstrtolower'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0  $4      
          7        ASSIGN_OBJ                                               'text'
          8        OP_DATA                                                  $4
   40     9        ASSIGN_OBJ                                               'matches'
         10        OP_DATA                                                  <array>
   41    11      > RETURN                                                   null

End of function __construct

Function analyse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jj3r7
function name:  analyse
number of ops:  18
compiled vars:  !0 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Cpreg_match_all'
          1        INIT_METHOD_CALL                                         'buildRegex'
          2        DO_FCALL                                      0  $1      
          3        SEND_VAR_NO_REF_EX                                       $1
          4        CHECK_FUNC_ARG                                           
          5        FETCH_OBJ_FUNC_ARG                               $2      'text'
          6        SEND_FUNC_ARG                                            $2
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $3      
          9        BOOL_NOT                                         ~4      $3
         10      > JMPZ                                                     ~4, ->14
   49    11    >   NEW                                              $5      'App%5CSemanticAnalysis%5CNoMatchesFoundException'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $5
   52    14    >   FETCH_DIM_R                                      ~8      !0, 'token'
         15        ASSIGN_OBJ                                               'matches'
         16        OP_DATA                                                  ~8
   53    17      > RETURN                                                   null

End of function analyse

Function getscore:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jj3r7
function name:  getScore
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   INIT_METHOD_CALL                                         'scoreMatches'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
   61     3*     > RETURN                                                   null

End of function getscore

Function buildregex:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/jj3r7
function name:  buildRegex
number of ops:  17
compiled vars:  !0 = $regex, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV_INIT                                        !0      ''
   69     1        FETCH_OBJ_R                                      ~2      'criteria'
          2      > FE_RESET_R                                       $3      ~2, ->8
          3    > > FE_FETCH_R                                               $3, !1, ->8
   70     4    >   FETCH_DIM_R                                      ~4      !1, 0
          5        CONCAT                                           ~5      ~4, '%7C'
          6        ASSIGN_OP                                     8          !0, ~5
   69     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $3
   73     9        INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Crtrim'
         10        SEND_VAR_EX                                              !0
         11        SEND_VAL_EX                                              '%7C'
         12        DO_FCALL                                      0  $7      
         13        CONCAT                                           ~8      '%2F%28%3F%3Ctoken%3E', $7
         14        CONCAT                                           ~9      ~8, '%29%2Fiu'
         15      > RETURN                                                   ~9
   74    16*     > RETURN                                                   null

End of function buildregex

Function scorematches:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/jj3r7
function name:  scoreMatches
number of ops:  19
compiled vars:  !0 = $score, !1 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV_INIT                                        !0      0
   82     1        FETCH_OBJ_R                                      ~2      'criteria'
          2      > FE_RESET_R                                       $3      ~2, ->16
          3    > > FE_FETCH_R                                               $3, !1, ->16
   83     4    >   INIT_NS_FCALL_BY_NAME                                    'App%5CSemanticAnalysis%5Cin_array'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_DIM_FUNC_ARG                               $4      !1, 0
          7        SEND_FUNC_ARG                                            $4
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $5      'matches'
         10        SEND_FUNC_ARG                                            $5
         11        DO_FCALL                                      0  $6      
         12      > JMPZ                                                     $6, ->15
   84    13    >   FETCH_DIM_R                                      ~7      !1, 1
         14        ASSIGN_OP                                     1          !0, ~7
   82    15    > > JMP                                                      ->3
         16    >   FE_FREE                                                  $3
   88    17      > RETURN                                                   !0
   89    18*     > RETURN                                                   null

End of function scorematches

End of class App\SemanticAnalysis\SemanticAnalysis.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.71 ms | 1408 KiB | 23 Q