-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Janusz Żukowicz
committed
Aug 18, 2016
0 parents
commit 0ed1db0
Showing
10 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
##Hunspell PHP wrapper | ||
This is a hunspell php wrapper. | ||
|
||
Example | ||
=================== | ||
```php | ||
$hunspell = new \HunspellPHP\Hunspell(); | ||
|
||
var_dump($hunspell->find('otwórz')); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "hunspell-php/hunspell-php", | ||
"description": "Hunspell PHP wrapper", | ||
"minimum-stability": "dev", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Janusz Żukowicz", | ||
"email": "john_zuk@wp.pl" | ||
} | ||
], | ||
"require": { | ||
"php" : ">=5.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"HunspellPHP\\": "src/HunspellPHP" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
include '../vendor/autoload.php'; | ||
|
||
$hunspell = new \HunspellPHP\Hunspell(); | ||
|
||
var_dump($hunspell->find('otwórz')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace HunspellPHP\Exception; | ||
|
||
class InvalidMatchTypeException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace HunspellPHP\Exception; | ||
|
||
|
||
class InvalidResultException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
namespace HunspellPHP\Exception; | ||
|
||
|
||
class WordNotFoundException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
namespace HunspellPHP; | ||
|
||
use HunspellPHP\Exception\InvalidMatchTypeException; | ||
use HunspellPHP\Exception\InvalidResultException; | ||
use HunspellPHP\Exception\WordNotFoundException; | ||
|
||
class Hunspell | ||
{ | ||
const OK = '*'; | ||
|
||
const ROOT = '+'; | ||
|
||
const MISS = '&'; | ||
|
||
const NONE = '#'; | ||
|
||
const STATUSES_NAME = [ | ||
Hunspell::OK => 'OK', | ||
Hunspell::ROOT => 'ROOT', | ||
Hunspell::MISS => 'MISS', | ||
Hunspell::NONE => 'NONE', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $language = "pl_PL"; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $encoding = "pl_PL.utf-8"; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $matcher = | ||
"/(?P<type>\*|\+|&|#)\s?(?P<original>\w+)?(\s(?P<count>\d+)\s(?P<offset>\d+):\s(?P<misses>.*+))?/u"; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getLanguage() | ||
{ | ||
return $this->language; | ||
} | ||
|
||
/** | ||
* @param string $language | ||
*/ | ||
public function setLanguage($language) | ||
{ | ||
$this->language = $this->clear($language); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getEncoding() | ||
{ | ||
return $this->encoding; | ||
} | ||
|
||
/** | ||
* @param string $encoding | ||
*/ | ||
public function setEncoding($encoding) | ||
{ | ||
$this->encoding = $this->clear($encoding); | ||
} | ||
|
||
/** | ||
* @param string $word word to find | ||
* @return HunspellResponse | ||
* @throws InvalidMatchTypeException | ||
* @throws InvalidResultException | ||
* @throws WordNotFoundException | ||
*/ | ||
public function find($word) | ||
{ | ||
$matches = []; | ||
$result = $this->preParse($this->command($word)); | ||
|
||
$match = preg_match($this->matcher, $result, $matches); | ||
|
||
if (!$match) { | ||
throw new InvalidResultException(sprintf("Invalid hunspell result.")); | ||
} | ||
$matches['input'] = $word; | ||
|
||
return $this->parse($matches); | ||
} | ||
|
||
/** | ||
* @param string $input | ||
* @return mixed | ||
*/ | ||
protected function clear($input) | ||
{ | ||
return preg_replace('[^a-zA-Z0-9_\-.]', '', $input); | ||
} | ||
|
||
/** | ||
* @return string | ||
* @param string $input | ||
*/ | ||
protected function command($input) | ||
{ | ||
return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s", $this->encoding, $input, $this->language)); | ||
} | ||
|
||
/** | ||
* @param string $input | ||
* @return string | ||
*/ | ||
protected function preParse($input) | ||
{ | ||
$result = explode(PHP_EOL, $input); | ||
|
||
return isset($result[1]) ? $result[1] : $result[0]; | ||
} | ||
|
||
/** | ||
* @param array $matches | ||
* @return HunspellResponse | ||
* @throws InvalidMatchTypeException | ||
* @throws WordNotFoundException | ||
*/ | ||
protected function parse(array $matches) | ||
{ | ||
if ($matches['type'] == Hunspell::OK) { | ||
return new HunspellResponse( | ||
$matches['input'], | ||
$matches['input'], | ||
Hunspell::STATUSES_NAME[$matches['type']] | ||
); | ||
} else if ($matches['type'] == Hunspell::ROOT) { | ||
return new HunspellResponse( | ||
$matches['original'], | ||
$matches['input'], | ||
Hunspell::STATUSES_NAME[$matches['type']] | ||
); | ||
} else if ($matches['type'] == Hunspell::MISS) { | ||
return new HunspellResponse( | ||
'', | ||
$matches['original'], | ||
Hunspell::STATUSES_NAME[$matches['type']], | ||
$matches['offset'], | ||
explode(", ", $matches['misses']) | ||
); | ||
} else if ($matches['type'] == Hunspell::NONE) { | ||
throw new WordNotFoundException(sprintf("Word %s not found", $matches['input'])); | ||
} | ||
|
||
throw new InvalidMatchTypeException(sprintf("Match type %s is invalid", $matches['type'])); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace HunspellPHP; | ||
|
||
class HunspellResponse | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $root; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $original; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $offset; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $misses = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $code; | ||
|
||
/** | ||
* HunspellResponse constructor. | ||
* @param string $root | ||
* @param string $original | ||
* @param int $offset | ||
* @param array $misses | ||
* @param string $code | ||
*/ | ||
public function __construct($root, $original, $code = '', $offset = null, array $misses = []) | ||
{ | ||
$this->root = $root; | ||
$this->original = $original; | ||
$this->offset = $offset; | ||
$this->misses = $misses; | ||
$this->code = $code; | ||
} | ||
} |