Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Jan 28, 2017
1 parent 3057956 commit 3c2c89e
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 53 deletions.
60 changes: 48 additions & 12 deletions src/Classes/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public function count()
*/
public function diff($items)
{
return new static($this->text, array_diff($this->words, $this->getArrayableItems($items)), $this->pluginData);
return new static(
$this->text,
array_diff($this->words, $this->getArrayableItems($items)), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -130,7 +133,10 @@ public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

return new static($this->text, $this->arrExcept($this->words, $keys), $this->pluginData);
return new static(
$this->text,
$this->arrExcept($this->words, $keys), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -179,7 +185,10 @@ public function first(callable $callback = null, $default = null)
*/
public function flatten($depth = INF)
{
return new static($this->text, $this->arrFlatten($this->words, $depth), $this->pluginData);
return new static(
$this->text,
$this->arrFlatten($this->words, $depth), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -259,7 +268,10 @@ public function implode($value, $glue = null)
*/
public function intersect($items)
{
return new static($this->text, array_intersect($this->words, $this->getArrayableItems($items)), $this->pluginData);
return new static(
$this->text,
array_intersect($this->words, $this->getArrayableItems($items)), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -320,7 +332,10 @@ public function map(callable $callback)
*/
public function merge($items)
{
return new static($this->text, array_merge($this->words, $this->getArrayableItems($items)), $this->pluginData);
return new static(
$this->text,
array_merge($this->words, $this->getArrayableItems($items)), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -383,7 +398,10 @@ public function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

return new static($this->text, $this->arrOnly($this->words, $keys), $this->pluginData);
return new static(
$this->text,
$this->arrOnly($this->words, $keys), $this->pluginData
);
}

/**
Expand All @@ -396,7 +414,10 @@ public function only($keys)
*/
public function pluck($value, $key = null)
{
return new static($this->text, $this->arrPluck($this->words, $value, $key), $this->pluginData);
return new static(
$this->text,
$this->arrPluck($this->words, $value, $key), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -491,7 +512,10 @@ public function shift()
*/
public function slice($offset, $length = null)
{
return new static($this->text, array_slice($this->words, $offset, $length, true), $this->pluginData);
return new static(
$this->text,
array_slice($this->words, $offset, $length, true), $this->pluginData
);
}

/**
Expand All @@ -506,10 +530,16 @@ public function slice($offset, $length = null)
public function splice($offset, $length = null, $replacement = [])
{
if (func_num_args() == 1) {
return new static($this->text, array_splice($this->words, $offset), $this->pluginData);
return new static(
$this->text,
array_splice($this->words, $offset), $this->pluginData
);
}

return new static($this->text, array_splice($this->words, $offset, $length, $replacement), $this->pluginData);
return new static(
$this->text,
array_splice($this->words, $offset, $length, $replacement), $this->pluginData
);
}

/**
Expand Down Expand Up @@ -576,7 +606,10 @@ public function transform(callable $callback)
public function unique($key = null)
{
if (is_null($key)) {
return new static($this->text, array_unique($this->words, SORT_REGULAR), $this->pluginData);
return new static(
$this->text,
array_unique($this->words, SORT_REGULAR), $this->pluginData
);
}

$key = $this->valueRetriever($key);
Expand Down Expand Up @@ -641,7 +674,10 @@ public function zip($items)
return new static($this->text, func_get_args(), $this->pluginData);
}, $this->words], $arrayableItems);

return new static($this->text, call_user_func_array('array_map', $params), $this->pluginData);
return new static(
$this->text,
call_user_func_array('array_map', $params), $this->pluginData
);
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Classes/LimelightResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,29 @@ public function plugin($name)
*/
private function buildString($item, $value, $glue)
{
if ($value !== 'partOfSpeech' && $item->partOfSpeech === 'symbol' && preg_match('/\\s/', $glue)) {
if ($this->shouldNotGlue($item, $value, $glue)) {
return $item->$value;
}

return $glue.$item->$value;
}

/**
* Return true if should not prefix with glue.
*
* @param LimelightWord $item
* @param string $value
* @param string $glue
*
* @return bool
*/
private function shouldNotGlue($item, $value, $glue)
{
return $value !== 'partOfSpeech' &&
$item->partOfSpeech === 'symbol' &&
preg_match('/\\s/', $glue);
}

/**
* Cut first chars if its is glue.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Classes/LimelightWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ public function setPluginData($pluginName, $value)
*/
private function getPublicProperties()
{
return (new \ReflectionObject($this))->getProperties(\ReflectionProperty::IS_PUBLIC);
return (
new \ReflectionObject($this))->getProperties(\ReflectionProperty::IS_PUBLIC
);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public function make($interface)

return $instance;
} catch (\Exception $e) {
throw new InternalErrorException("Class {$fullClassName} could not be instantiated.");
throw new InternalErrorException(
"Class {$fullClassName} could not be instantiated."
);
}
}

Expand Down Expand Up @@ -172,7 +174,9 @@ private function getFullClassName(array $bindings, $interface)
if (isset($bindings[$interface])) {
return $bindings[$interface];
} else {
throw new InternalErrorException("Cannot resolve interface {$interface}. Check config.php file bindings.");
throw new InternalErrorException(
"Cannot resolve interface {$interface}. Check config.php file bindings."
);
}
}

Expand All @@ -187,11 +191,15 @@ private function getFullClassName(array $bindings, $interface)
private function validateClass($fullClassName, $interface)
{
if (!class_exists($fullClassName)) {
throw new InternalErrorException("Class {$fullClassName} defined in config.php does not exist.");
throw new InternalErrorException(
"Class {$fullClassName} defined in config.php does not exist."
);
}

if (!in_array($interface, class_implements($fullClassName))) {
throw new InternalErrorException("Class {$fullClassName} does not implement interface {$interface}.");
throw new InternalErrorException(
"Class {$fullClassName} does not implement interface {$interface}."
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ protected function getPluginData($type, $target = 'child')
if (is_null($this->pluginData)) {
return;
} elseif (!isset($this->pluginData[$type])) {
throw new PluginNotFoundException("Plugin data for {$type} can not be found. Is the {$type} plugin registered in config?");
throw new PluginNotFoundException(
"Plugin data for {$type} can not be found. Is the {$type} plugin registered in config?"
);
}

if ($this instanceof Collection && $target === 'child') {
Expand Down
9 changes: 5 additions & 4 deletions src/Parse/NoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class NoParser
/**
* Construct.
*
* @param Limelight $limelight
* @param Dispatcher $dispatcher
*/
public function __construct(Limelight $limelight, Dispatcher $dispatcher)
Expand Down Expand Up @@ -79,9 +80,9 @@ public function handle($text, array $pluginWhiteList)
private function buildToken($text)
{
return [
'literal' => $text,
'lemma' => $text,
'reading' => $text,
'literal' => $text,
'lemma' => $text,
'reading' => $text,
'pronunciation' => $text,
];
}
Expand All @@ -95,7 +96,7 @@ private function buildProperties()
{
return [
'partOfSpeech' => null,
'grammar' => null,
'grammar' => null,
];
}
}
9 changes: 7 additions & 2 deletions src/Parse/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ class Parser
* @param Mecab $mecab
* @param Tokenizer $tokenizer
* @param TokenParser $tokenParser
* @param Dispatcher $dispatcher
*/
public function __construct(Mecab $mecab, Tokenizer $tokenizer, TokenParser $tokenParser, Dispatcher $dispatcher)
{
public function __construct(
Mecab $mecab,
Tokenizer $tokenizer,
TokenParser $tokenParser,
Dispatcher $dispatcher
) {
$this->mecab = $mecab;
$this->tokenizer = $tokenizer;
$this->tokenParser = $tokenParser;
Expand Down
15 changes: 14 additions & 1 deletion src/Parse/PartOfSpeech/Classes/Doushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ public function handle(array $properties, $previousWord, $previous, array $curre

if ($current['partOfSpeech2'] === 'setsubi') {
$properties['attachToPrevious'] = true;
} elseif ($current['partOfSpeech2'] === 'hijiritsu' && $current['inflectionForm'] !== 'meireiI') {
} elseif ($this->isHijiritsuNotMeireiI($current)) {
$properties['attachToPrevious'] = true;
}

return $properties;
}

/**
* Return true if POS is hijiritsu and inflection is not meireiI.
*
* @param array $current
*
* @return boolean
*/
protected function isHijiritsuNotMeireiI($current)
{
return $current['partOfSpeech2'] === 'hijiritsu' &&
$current['inflectionForm'] !== 'meireiI';
}
}
79 changes: 69 additions & 10 deletions src/Parse/PartOfSpeech/Classes/Jodoushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ public function handle(array $properties, $previousWord, $previous, array $curre
{
$properties['partOfSpeech'] = 'postposition';

if ($this->isInInflections($previous, $current)) {
$properties['attachToPrevious'] = true;
} elseif ($this->isFuhenkagataAndNorU($current)) {
$properties['attachToPrevious'] = true;
} elseif ($this->isNaraAndPreviousIsMeishi($previous, $current)) {
$properties['partOfSpeech'] = 'conjunction';
} elseif ($this->isTokushuAndNa($current)) {
$properties['partOfSpeech'] = 'verb';
}

return $properties;
}

/**
* Return true if previous exists, POS is kakarijoshi and is in inflections
* array.
*
* @param array $previous
* @param array $current
*
* @return bool
*/
protected function isInInflections($previous, $current)
{
$inflections = [
'tokushuTa',
'tokushuNai',
Expand All @@ -29,16 +53,51 @@ public function handle(array $properties, $previousWord, $previous, array $curre
'tokushuNu',
];

if (is_null($previous) || (!is_null($previous) && $previous['partOfSpeech2'] !== 'kakarijoshi') && in_array($current['inflectionType'], $inflections)) {
$properties['attachToPrevious'] = true;
} elseif ($current['inflectionType'] === 'fuhenkagata' && ($current['lemma'] === '' || $current['lemma'] === '')) {
$properties['attachToPrevious'] = true;
} elseif ($current['literal'] === 'なら' && $previous['partOfSpeech1'] === 'meishi') {
$properties['partOfSpeech'] = 'conjunction';
} elseif ($current['inflectionType'] === 'tokushuDa' || $current['inflectionType'] === 'tokushuDesu' && $current['literal'] !== '') {
$properties['partOfSpeech'] = 'verb';
}
return is_null($previous) ||
(!is_null($previous) &&
$previous['partOfSpeech2'] !== 'kakarijoshi') &&
in_array($current['inflectionType'], $inflections
);
}

return $properties;
/**
* Return true if inflection is fuhenkagata and lemma is ん or う.
*
* @param array $current
*
* @return bool
*/
protected function isFuhenkagataAndNorU($current)
{
return $current['inflectionType'] === 'fuhenkagata' &&
($current['lemma'] === '' || $current['lemma'] === '');
}

/**
* Return true if literal is なら and previous POS is meishi.
*
* @param array $previous
* @param array $current
*
* @return boolean
*/
protected function isNaraAndPreviousIsMeishi($previous, $current)
{
return $current['literal'] === 'なら' &&
$previous['partOfSpeech1'] === 'meishi';
}

/**
* Return true inflection is tokushu and literal is な.
*
* @param array $current
*
* @return boolean
*/
protected function isTokushuAndNa($current)
{
return $current['inflectionType'] === 'tokushuDa' ||
$current['inflectionType'] === 'tokushuDesu' &&
$current['literal'] !== '';
}
}
Loading

0 comments on commit 3c2c89e

Please sign in to comment.