Phrase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Elastica\Suggest;
  3. use Elastica\Suggest\CandidateGenerator\AbstractCandidateGenerator;
  4. /**
  5. * Class Phrase.
  6. *
  7. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html
  8. */
  9. class Phrase extends AbstractSuggest
  10. {
  11. /**
  12. * @param string $analyzer
  13. *
  14. * @return $this
  15. */
  16. public function setAnalyzer($analyzer)
  17. {
  18. return $this->setParam('analyzer', $analyzer);
  19. }
  20. /**
  21. * Set the max size of the n-grams (shingles) in the field.
  22. *
  23. * @param int $size
  24. *
  25. * @return $this
  26. */
  27. public function setGramSize($size)
  28. {
  29. return $this->setParam('gram_size', $size);
  30. }
  31. /**
  32. * Set the likelihood of a term being misspelled even if the term exists in the dictionary.
  33. *
  34. * @param float $likelihood Defaults to 0.95, meaning 5% of the words are misspelled.
  35. *
  36. * @return $this
  37. */
  38. public function setRealWordErrorLikelihood($likelihood)
  39. {
  40. return $this->setParam('real_word_error_likelihood', $likelihood);
  41. }
  42. /**
  43. * Set the factor applied to the input phrases score to be used as a threshold for other suggestion candidates.
  44. * Only candidates which score higher than this threshold will be included in the result.
  45. *
  46. * @param float $confidence Defaults to 1.0.
  47. *
  48. * @return $this
  49. */
  50. public function setConfidence($confidence)
  51. {
  52. return $this->setParam('confidence', $confidence);
  53. }
  54. /**
  55. * Set the maximum percentage of the terms considered to be misspellings in order to form a correction.
  56. *
  57. * @param float $max
  58. *
  59. * @return $this
  60. */
  61. public function setMaxErrors($max)
  62. {
  63. return $this->setParam('max_errors', $max);
  64. }
  65. /**
  66. * @param string $separator
  67. *
  68. * @return $this
  69. */
  70. public function setSeparator($separator)
  71. {
  72. return $this->setParam('separator', $separator);
  73. }
  74. /**
  75. * Set suggestion highlighting.
  76. *
  77. * @param string $preTag
  78. * @param string $postTag
  79. *
  80. * @return $this
  81. */
  82. public function setHighlight($preTag, $postTag)
  83. {
  84. return $this->setParam('highlight', [
  85. 'pre_tag' => $preTag,
  86. 'post_tag' => $postTag,
  87. ]);
  88. }
  89. /**
  90. * @param float $discount
  91. *
  92. * @return $this
  93. */
  94. public function setStupidBackoffSmoothing($discount = 0.4)
  95. {
  96. return $this->setSmoothingModel('stupid_backoff', [
  97. 'discount' => $discount,
  98. ]);
  99. }
  100. /**
  101. * @param float $alpha
  102. *
  103. * @return $this
  104. */
  105. public function setLaplaceSmoothing($alpha = 0.5)
  106. {
  107. return $this->setSmoothingModel('laplace', [
  108. 'alpha' => $alpha,
  109. ]);
  110. }
  111. /**
  112. * @param float $trigramLambda
  113. * @param float $bigramLambda
  114. * @param float $unigramLambda
  115. *
  116. * @return $this
  117. */
  118. public function setLinearInterpolationSmoothing($trigramLambda, $bigramLambda, $unigramLambda)
  119. {
  120. return $this->setSmoothingModel('linear_interpolation', [
  121. 'trigram_lambda' => $trigramLambda,
  122. 'bigram_lambda' => $bigramLambda,
  123. 'unigram_lambda' => $unigramLambda,
  124. ]);
  125. }
  126. /**
  127. * @param string $model the name of the smoothing model
  128. * @param array $params
  129. *
  130. * @return $this
  131. */
  132. public function setSmoothingModel($model, array $params)
  133. {
  134. return $this->setParam('smoothing', [
  135. $model => $params,
  136. ]);
  137. }
  138. /**
  139. * @param AbstractCandidateGenerator $generator
  140. *
  141. * @return $this
  142. */
  143. public function addCandidateGenerator(AbstractCandidateGenerator $generator)
  144. {
  145. return $this->setParam('candidate_generator', $generator);
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function toArray()
  151. {
  152. $array = parent::toArray();
  153. $baseName = $this->_getBaseName();
  154. if (isset($array[$baseName]['candidate_generator'])) {
  155. $generator = $array[$baseName]['candidate_generator'];
  156. unset($array[$baseName]['candidate_generator']);
  157. $keys = array_keys($generator);
  158. $values = array_values($generator);
  159. $array[$baseName][$keys[0]][] = $values[0];
  160. }
  161. return $array;
  162. }
  163. }