/** * Executes phrase suggester and checks values. */ public function testPhraseSuggester() { $phrase = new Phrase('description', 'Lorm adip'); $phrase->setAnalyzer('simple'); $phrase->setSize(1); $phrase->setRealWordErrorLikelihood(0.95); $phrase->setMaxErrors(0.5); $phrase->setGramSize(2); $phrase->setHighlight(['pre_tag' => '<span class="highlight">', 'post_tag' => '</span>']); $repository = $this->getManager()->getRepository('AcmeTestBundle:Product'); $search = $repository->createSearch()->addSuggester($phrase); $results = $repository->execute($search, Repository::RESULTS_RAW); $score = array_pop($results['suggest']['description-phrase'][0]['options'][0]); $this->assertEquals(['description-phrase' => [['text' => 'Lorm adip', 'offset' => 0, 'length' => 9, 'options' => [['text' => 'lorem adip', 'highlighted' => '<span class="highlight">lorem</span> adip']]]]], $results['suggest']); $this->assertTrue($score > 0 && $score <= 1, 'Score is out of bounds'); }
/** * Data provider for testSuggestionIterator(). * * @return array */ public function getSuggestIterationData() { $out = []; // Case #0, Phrase type with all parameters set. $phrase = new Phrase('description', 'Lorm adip'); $phrase->setAnalyzer('simple'); $phrase->setSize(1); $phrase->setRealWordErrorLikelihood(0.95); $phrase->setMaxErrors(0.5); $phrase->setGramSize(2); $phrase->setHighlight(['pre_tag' => '<span class="highlight">', 'post_tag' => '</span>']); $expectedOption = new PhraseOption('lorem adip', 0.0, '<span class="highlight">lorem</span> adip'); $out[] = ['suggesters' => [$phrase], 'expectedOptions' => [$expectedOption]]; // Case #1, Phrase type with almost nothing set. $phrase = new Phrase('description', 'Lorm adip'); $expectedOption = new SimpleOption('lorem adip', 0.0); $out[] = ['suggesters' => [$phrase], 'expectedOptions' => [$expectedOption]]; // Case #2, Term type with almost nothing set. $term = new Term('description', 'ipsu'); $expectedOption = new TermOption('ipsum', 0.0, 3); $out[] = ['suggesters' => [$term], 'expectedOptions' => [$expectedOption]]; // Case #3, Multiple suggesters. $term = new Term('description', 'ipsu'); $phrase = new Phrase('description', 'Lorm adip'); $expectedOptions = [new SimpleOption('lorem adip', 0.0), new TermOption('ipsum', 0.0, 3)]; $out[] = ['suggesters' => [$term, $phrase], 'expectedOptions' => $expectedOptions]; // Case #4, Multiple options within multiple suggesters. $term = new Term('description', 'distibutd'); $phrase = new Phrase('description', 'Lorm adip'); $expectedOptions = [new SimpleOption('lorem adip', 0.0), new TermOption('disributed', 0.0, 1), new TermOption('distributed', 0.0, 1)]; $out[] = ['suggesters' => [$term, $phrase], 'expectedOptions' => $expectedOptions]; // Case #5, completion option using context suggester, with payload. $geoContext = new Context\GeoContext('location', ['lat' => 0, 'lon' => 0]); $categoryContext = new Context\CategoryContext('price', '500'); $context = new Context('suggestions', 'cons'); $context->addContext($geoContext); $context->addContext($categoryContext); $expectedOption = new CompletionOption('Lorem ipsum', 0.0, ['test' => true]); $out[] = ['suggesters' => [$context], 'expectedOptions' => [$expectedOption]]; // Case #6, completion option using completion suggester, no payload. $completion = new Completion('completion_suggesting', 'ipsum'); $expectedOption = new SimpleOption('Lorem ipsum', 0.0, null); $out[] = ['suggesters' => [$completion], 'expectedOptions' => [$expectedOption]]; return $out; }
/** * @return array */ public function getTestToArrayData() { $out = []; // Case #0: simple. $phrase0 = new Phrase('body', 'lorem ipsum'); $expected0 = ['body-phrase' => ['text' => 'lorem ipsum', 'phrase' => ['field' => 'body']]]; $out[] = [$expected0, $phrase0]; // Case #1: using all fields. $phrase1 = new Phrase('description', 'awesome cat'); $phrase1->setMaxErrors(2); $phrase1->setGramSize(1); $phrase1->setRealWordErrorLikelihood(0.95); $phrase1->setHighlight(['pre_tag' => '<span class="info">', 'post_tag' => '</span>']); $phrase1->setAnalyzer('simple'); $phrase1->setConfidence(1); $phrase1->setSize(6); $highlightObject = new \stdClass(); $highlightObject->post_tag = '</span>'; $highlightObject->pre_tag = '<span class="info">'; $expected1 = ['description-phrase' => ['text' => 'awesome cat', 'phrase' => ['analyzer' => 'simple', 'field' => 'description', 'size' => 6, 'real_word_error_likelihood' => 0.95, 'max_errors' => 2.0, 'gram_size' => 1, 'highlight' => $highlightObject]]]; $out[] = [$expected1, $phrase1]; return $out; }