summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Suggest/Phrase.php
blob: 14630dc148857e1dbe8f78f441b1eb8e5c4e82b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace Elastica\Suggest;

use Elastica\Suggest\CandidateGenerator\AbstractCandidateGenerator;


/**
 * Class Phrase
 * @package Elastica\Suggest
 * @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html
 */
class Phrase extends AbstractSuggest
{
    /**
     * @param string $analyzer
     * @return \Elastica\Suggest\Phrase
     */
    public function setAnalyzer($analyzer)
    {
        return $this->setParam("analyzer", $analyzer);
    }

    /**
     * Set the max size of the n-grams (shingles) in the field
     * @param int $size
     * @return \Elastica\Suggest\Phrase
     */
    public function setGramSize($size)
    {
        return $this->setParam("gram_size", $size);
    }

    /**
     * Set the likelihood of a term being misspelled even if the term exists in the dictionary
     * @param float $likelihood Defaults to 0.95, meaning 5% of the words are misspelled.
     * @return \Elastica\Suggest\Phrase
     */
    public function setRealWorldErrorLikelihood($likelihood)
    {
        return $this->setParam("real_world_error_likelihood", $likelihood);
    }

    /**
     * Set the factor applied to the input phrases score to be used as a threshold for other suggestion candidates.
     * Only candidates which score higher than this threshold will be included in the result.
     * @param float $confidence Defaults to 1.0.
     * @return \Elastica\Suggest\Phrase
     */
    public function setConfidence($confidence)
    {
        return $this->setParam("confidence", $confidence);
    }

    /**
     * Set the maximum percentage of the terms considered to be misspellings in order to form a correction
     * @param float $max
     * @return \Elastica\Suggest\Phrase
     */
    public function setMaxErrors($max)
    {
        return $this->setParam("max_errors", $max);
    }

    /**
     * @param string $separator
     * @return \Elastica\Param
     */
    public function setSeparator($separator)
    {
        return $this->setParam("separator", $separator);
    }

    /**
     * Set suggestion highlighting
     * @param string $preTag
     * @param string $postTag
     * @return \Elastica\Suggest\Phrase
     */
    public function setHighlight($preTag, $postTag)
    {
        return $this->setParam("highlight", array(
            'pre_tag' => $preTag,
            'post_tag' => $postTag
        ));
    }

    /**
     * @param float $discount
     * @return \Elastica\Suggest\Phrase
     */
    public function setStupidBackoffSmoothing($discount = 0.4)
    {
        return $this->setSmoothingModel("stupid_backoff", array(
            "discount" => $discount
        ));
    }

    /**
     * @param float $alpha
     * @return \Elastica\Suggest\Phrase
     */
    public function setLaplaceSmoothing($alpha = 0.5)
    {
        return $this->setSmoothingModel("laplace", array(
            "alpha" => $alpha
        ));
    }

    /**
     * @param float $trigramLambda
     * @param float $bigramLambda
     * @param float $unigramLambda
     * @return \Elastica\Suggest\Phrase
     */
    public function setLinearInterpolationSmoothing($trigramLambda, $bigramLambda, $unigramLambda)
    {
        return $this->setSmoothingModel("linear_interpolation", array(
            "trigram_lambda" => $trigramLambda,
            "bigram_lambda" => $bigramLambda,
            "unigram_lambda" => $unigramLambda
        ));
    }

    /**
     * @param string $model the name of the smoothing model
     * @param array $params
     * @return \Elastica\Suggest\Phrase
     */
    public function setSmoothingModel($model, array $params)
    {
        return $this->setParam("smoothing", array(
            $model => $params
        ));
    }

    /**
     * @param AbstractCandidateGenerator $generator
     * @return \Elastica\Suggest\Phrase
     */
    public function addCandidateGenerator(AbstractCandidateGenerator $generator)
    {
        $generator = $generator->toArray();
        $keys = array_keys($generator);
        $values = array_values($generator);
        return $this->addParam($keys[0], $values[0]);
    }
}