3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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(); }
based on 8cMQu
Output for 7.0.0 - 7.2.0
int(3) Fatal error: Uncaught Error: Call to undefined method App\SemanticAnalysis\SemanticAnalysis::getMatches() in /in/jj3r7:117 Stack trace: #0 {main} thrown in /in/jj3r7 on line 117
Process exited with code 255.
Output for 5.4.12 - 5.6.28
int(3) Fatal error: Call to undefined method App\SemanticAnalysis\SemanticAnalysis::getMatches() in /in/jj3r7 on line 117
Process exited with code 255.
Output for 5.3.22 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/jj3r7 on line 40
Process exited with code 255.