ソースを参照

spell lib added

euromark 14 年 前
コミット
947f168439
2 ファイル変更190 行追加0 行削除
  1. 91 0
      Lib/SpellLib.php
  2. 99 0
      Test/Case/Lib/SpellLibTest.php

+ 91 - 0
Lib/SpellLib.php

@@ -0,0 +1,91 @@
+<?php
+if (!defined('ENCHANT_MYSPELL')) {
+	define('ENCHANT_MYSPELL', 1);
+}
+if (!defined('ENCHANT_ISPELL')) {
+	define('ENCHANT_ISPELL', 2);
+}
+
+/**
+ * Wrapper for the PHP Extension Enchant which provides basic spellchecking
+ * @author Mark Scherer
+ * @licence MIT
+ * 
+ * 2012-02-17 ms
+ */
+class SpellLib {
+	
+	const ENGINE_MYSPELL = ENCHANT_MYSPELL; # default engine
+	
+	const ENGINE_ISPELL = ENCHANT_ISPELL;
+	
+	/**
+	 * available engines
+	 */
+	protected $_engines = array(self::ENGINE_MYSPELL => 'myspell', self::ENGINE_ISPELL => 'ispell');
+		
+	/**
+	 * available languages
+	 */
+	protected $l_langs = array('en_GB', 'de_DE');
+	
+	protected $_Broker;
+	
+	protected $_Dict;
+	
+	//public $settings = array();
+	
+	public function __construct($options = array()) {
+		if (!function_exists('enchant_broker_init')) {
+			throw new InternalErrorException(__('Module %s not installed', 'Enchant'));
+		}
+		$this->_Broker = enchant_broker_init();
+		
+		$defaults = array(
+			'path' => VENDORS.'dictionaries'.DS,
+			'lang' => 'en_GB',
+			'engine' => self::ENGINE_MYSPELL
+		);
+		$defaults = am($defaults, (array) Configure::read('Spell'));
+		$options = array_merge($defaults, $options);
+		
+		if (!isset($this->_engines[$options['engine']])) {
+			throw new InternalErrorException(__('Engine %s not found', (String) $options['engine']));
+		}
+		$engineFolder = $this->_engines[$options['engine']];
+		enchant_broker_set_dict_path($this->_Broker, $options['engine'], $options['path'] . $engineFolder . DS);
+		
+		if (!enchant_broker_dict_exists($this->_Broker, $options['lang'])) {
+			throw new InternalErrorException(__('Dictionary %s not found', $options['lang']));
+		}
+		
+		$this->_Dict = enchant_broker_request_dict($this->_Broker, $options['lang']);
+	}
+	
+	public function listDictionaries() {
+		return enchant_broker_list_dicts($this->_Broker);
+	}
+	
+	public function listBrokers() {
+		 return enchant_broker_describe($this->_Broker);
+	}
+	
+	/**
+	 * @return bool $success
+	 */
+	public function check($word) {
+		return enchant_dict_check($this->_Dict, $word);
+	}
+	
+	/**
+	 * @return array $listOfSuggestions
+	 */
+	public function suggestions($word) {
+		return enchant_dict_suggest($this->_Dict, $word);
+	}
+	
+	public function __destruct() {
+		enchant_broker_free($this->_Broker);
+	}
+	
+}

+ 99 - 0
Test/Case/Lib/SpellLibTest.php

@@ -0,0 +1,99 @@
+<?php
+
+App::uses('SpellLib', 'Tools.Lib');
+App::uses('MyCakeTestCase', 'Tools.Lib');
+
+class SpellLibTest extends MyCakeTestCase {
+	
+	public $SpellLib;
+	
+	public function setUp() {
+		parent::setUp();
+		
+		$this->SpellLib = new SpellLib();
+	}
+	
+	public function tearDown() {
+		parent::tearDown();
+		
+		unset($this->SpellLib);
+	}
+	
+	public function testObject() {
+		$this->assertTrue(is_a($this->SpellLib, 'SpellLib'));
+	}
+	
+	public function testList() {
+		$res = $this->SpellLib->listBrokers();
+		debug($res);
+		$this->assertTrue(is_array($res) && count($res) > 1);
+		$this->assertTrue(in_array($res[0]['name'], array('ispell', 'myspell')));
+		
+		$res = $this->SpellLib->listDictionaries();
+		debug($res);
+		$this->assertTrue(is_array($res) && count($res) > 1);
+		$this->assertTrue(in_array($res[0]['lang_tag'], array('de_DE', 'en_GB')));
+	}
+		
+	public function testDefaults() {
+		$word = 'house';
+		$res = $this->SpellLib->check($word);
+		$this->assertTrue($res);
+		
+		$word = 'soong';
+		$res = $this->SpellLib->check($word);
+		$this->assertFalse($res);
+		$suggestions = $this->SpellLib->suggestions($word);
+		debug($suggestions);
+		$this->assertTrue(is_array($suggestions) && count($suggestions) > 1);
+		$this->assertTrue(in_array('song', $suggestions));
+		
+		$word = 'bird';
+		$res = $this->SpellLib->check($word);
+		$this->assertTrue($res);
+		
+		ob_flush();
+	}
+	
+	public function testGerman() {
+		$this->SpellLib = new SpellLib(array('lang'=>'de_DE'));
+		
+		$word = 'Wand';
+		$res = $this->SpellLib->check($word);
+		$this->assertTrue($res);
+		
+		$word = 'Hauz';
+		$res = $this->SpellLib->check($word);
+		$this->assertFalse($res);
+		$suggestions = $this->SpellLib->suggestions($word);
+		debug($suggestions);
+		$this->assertTrue(is_array($suggestions) && count($suggestions) > 1);
+		$this->assertTrue(in_array('Haus', $suggestions));
+		
+		$word = 'Mäuse';
+		$res = $this->SpellLib->check($word);
+		$this->assertTrue($res);
+		
+		ob_flush();
+	}
+	
+	public function testConfigureConfiguration() {
+		Configure::write('Spell.lang', 'de_DE');
+		$this->SpellLib = new SpellLib();
+		
+		$word = 'Mäuse';
+		$res = $this->SpellLib->check($word);
+		$this->assertTrue($res);
+		
+		Configure::write('Spell.lang', 'en_GB');
+		$this->SpellLib = new SpellLib();
+		
+		$word = 'Mäuse';
+		$res = $this->SpellLib->check($word);
+		$this->assertFalse($res);
+		$suggestions = $this->SpellLib->suggestions($word);
+		$this->assertTrue(is_array($suggestions) && count($suggestions) > 0);
+		$this->assertTrue(in_array('Mouse', $suggestions));
+	}
+	
+}