Skip to content

Commit

Permalink
Hunspell PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Żukowicz committed Aug 18, 2016
0 parents commit 0ed1db0
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
10 changes: 10 additions & 0 deletions README.MD
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'));
```
20 changes: 20 additions & 0 deletions composer.json
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"
}
}
}
20 changes: 20 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions example/index.php
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'));
7 changes: 7 additions & 0 deletions src/HunspellPHP/Exception/InvalidMatchTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace HunspellPHP\Exception;

class InvalidMatchTypeException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/HunspellPHP/Exception/InvalidResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace HunspellPHP\Exception;


class InvalidResultException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/HunspellPHP/Exception/WordNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace HunspellPHP\Exception;


class WordNotFoundException extends \Exception
{

}
159 changes: 159 additions & 0 deletions src/HunspellPHP/Hunspell.php
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']));
}

}
47 changes: 47 additions & 0 deletions src/HunspellPHP/HunspellResponse.php
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;
}
}

0 comments on commit 0ed1db0

Please sign in to comment.