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