Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Żukowicz authored Dec 19, 2016
2 parents ad343a8 + d967ec2 commit 76d33cc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/HunspellPHP/Hunspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function setEncoding($encoding)
public function find($words)
{
$matches = [];
$results = $this->preParse($this->command($words), $words);
$results = $this->preParse($this->findCommand($words), $words);

$response = [];
foreach ($results as $word => $result) {
Expand All @@ -95,6 +95,21 @@ public function find($words)
return $response;
}

/**
* @param string $word word to find
* @return HunspellStemResponse
* @throws InvalidMatchTypeException
* @throws InvalidResultException
* @throws WordNotFoundException
*/
public function stem($word)
{
$result = explode(PHP_EOL, $this->stemCommand($word));
$result['input'] = $word;
$result = $this->stemParse($result);
return $result;
}

/**
* @param string $input
* @return mixed
Expand All @@ -108,11 +123,20 @@ protected function clear($input)
* @return string
* @param string $input
*/
protected function command($input)
protected function findCommand($input)
{
return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s", $this->encoding, $input, $this->language));
}

/**
* @return string
* @param string $input
*/
protected function stemCommand($input)
{
return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s -s", $this->encoding, $input, $this->language));
}

/**
* @param string $input
* @param string $words
Expand Down Expand Up @@ -166,4 +190,30 @@ protected function parse(array $matches)
throw new InvalidMatchTypeException(sprintf("Match type %s is invalid", $matches['type']));
}

/**
* @param array $matches
* @return HunspellStemResponse
* @throws InvalidMatchTypeException
* @throws WordNotFoundException
*/
protected function stemParse(array $matches)
{
$input = $matches['input'];
unset($matches['input']);
$stems = [];
foreach ($matches as $match) {
$stem = explode(' ', $match);
if (isset($stem[1]) && !empty($stem[1])) {
if (!in_array($stem[1], $stems)) {
$stems[] = $stem[1];
}
} elseif (isset($stem[0]) && !empty($stem[0])) {
if (!in_array($stem[0], $stems)) {
$stems[] = $stem[0];
}
}
}
return new HunspellStemResponse($input, $stems);
}

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

class HunspellStemResponse
{
/**
* @var string
*/
public $original;

/**
* @var string[]
*/
public $stems;

/**
* HunspellStemResponse constructor.
* @param string $original
* @param string[] $stems
*/
public function __construct($original, $stems = [])
{
$this->original = $original;
$this->stems = $stems;
}
}

0 comments on commit 76d33cc

Please sign in to comment.