1<?php
2/**
3 * Copyright 2005-2016 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category  Horde
9 * @copyright 2005-2016 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   SpellChecker
12 */
13
14/**
15 * Provides a unified spellchecker API.
16 *
17 * @author    Chuck Hagenbuch <chuck@horde.org>
18 * @author    Michael Slusarz <slusarz@horde.org>
19 * @category  Horde
20 * @copyright 2005-2016 Horde LLC
21 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
22 * @package   SpellChecker
23 */
24abstract class Horde_SpellChecker
25{
26    const SUGGEST_FAST = 1;
27    const SUGGEST_NORMAL = 2;
28    const SUGGEST_SLOW = 3;
29
30    /**
31     * Configuration parameters.
32     *
33     * @var array
34     */
35    protected $_params = array(
36        'html' => false,
37        'locale' => 'en',
38        'localDict' => array(),
39        'maxSuggestions' => 10,
40        'minLength' => 3,
41        'suggestMode' => self::SUGGEST_FAST
42    );
43
44    /**
45     * Attempts to return a concrete Horde_SpellChecker instance based on
46     * $driver.
47     *
48     * @deprecated
49     *
50     * @param string $driver  The type of concrete subclass to return.
51     * @param array $params   A hash containing any additional configuration
52     *                        or connection parameters a subclass might need.
53     *
54     * @return Horde_SpellChecker  The newly created instance.
55     * @throws Horde_Exception
56     */
57    public static function factory($driver, $params = array())
58    {
59        $class = 'Horde_SpellChecker_' . Horde_String::ucfirst(basename($driver));
60        if (class_exists($class)) {
61            return new $class($params);
62        }
63
64        throw new Horde_Exception('Driver ' . $driver . ' not found');
65    }
66
67    /**
68     * Constructor.
69     *
70     * @param array $params  TODO
71     */
72    public function __construct(array $params = array())
73    {
74        $this->setParams($params);
75    }
76
77    /**
78     * Set configuration parmeters.
79     *
80     * @param array $params  Parameters to set.
81     */
82    public function setParams($params)
83    {
84        $this->_params = array_merge($this->_params, $params);
85    }
86
87    /**
88     * Perform spellcheck.
89     *
90     * @param string $text  Text to spellcheck.
91     *
92     * @return array  TODO
93     * @throws Horde_SpellChecker_Exception
94     */
95    abstract public function spellCheck($text);
96
97    /**
98     * TODO
99     *
100     * @param string $text  TODO
101     *
102     * @return array  TODO
103     */
104    protected function _getWords($text)
105    {
106        return array_keys(array_flip(preg_split('/[\s\[\]]+/s', $text, -1, PREG_SPLIT_NO_EMPTY)));
107    }
108
109    /**
110     * Determine if a word exists in the local dictionary.
111     *
112     * @param string $word  The word to check.
113     *
114     * @return boolean  True if the word appears in the local dictionary.
115     */
116    protected function _inLocalDictionary($word)
117    {
118        return empty($this->_params['localDict'])
119            ? false
120            : in_array(Horde_String::lower($word, true, 'UTF-8'), $this->_params['localDict']);
121    }
122
123}
124