Skip to content

Commit

Permalink
Move basic LimelightResults methods to collection method implementati…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
zachleigh committed Apr 24, 2016
1 parent 25b8e23 commit dbda222
Show file tree
Hide file tree
Showing 32 changed files with 377 additions and 642 deletions.
4 changes: 2 additions & 2 deletions limelight_console
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ $limelight = new Limelight();
$results = $limelight->parse($input);

if ($method && $flag) {
echo $results->$flag()->$method() . "\n";
var_dump($results->$flag()->$method()) . "\n";
} else if ($method) {
echo $results->$method() . "\n";
var_dump($results->$method()) . "\n";
} else {
var_dump($results);
}
14 changes: 13 additions & 1 deletion src/Classes/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Limelight\Classes;

use ArrayAccess;
use ArrayIterator;
use JsonSerializable;
use IteratorAggregate;
use Limelight\Helpers\Arr;
use Limelight\Classes\LimelightResults;
use Limelight\Helpers\Contracts\Arrayable;

abstract class Collection implements ArrayAccess, JsonSerializable
abstract class Collection implements ArrayAccess, JsonSerializable, IteratorAggregate
{
use Arr;

Expand Down Expand Up @@ -616,6 +618,16 @@ public function zip($items)
return new static($this->text, call_user_func_array('array_map', $params), $this->pluginData);
}

/**
* Get an iterator for the items.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->words);
}

/**
* Convert the object into something JSON serializable.
*
Expand Down
180 changes: 32 additions & 148 deletions src/Classes/LimelightResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Limelight\Helpers\ResultsHelpers;
use Limelight\Helpers\Contracts\Jsonable;
use Limelight\Helpers\Contracts\Arrayable;
use Limelight\Exceptions\InvalidInputException;

class LimelightResults extends Collection implements Arrayable, Jsonable
{
Expand All @@ -32,13 +31,6 @@ class LimelightResults extends Collection implements Arrayable, Jsonable
*/
protected $pluginData = [];

/**
* Flag for calling Converter on LimelightWord.
*
* @var null/string
*/
protected $conversionFlag = null;

/**
* Construct.
*
Expand All @@ -53,16 +45,6 @@ public function __construct($text, array $words, $pluginData)
$this->pluginData = $pluginData;
}

/**
* Call generator if invoked as function.
*
* @return function
*/
public function __invoke()
{
return $this->next();
}

/**
* Print result info.
*
Expand All @@ -80,26 +62,34 @@ public function __toString()
}

/**
* Get the original, user inputed text.
* Get values as string.
*
* @param string $value [value]
* @param string $glue [word divider]
*
* @return string
*/
public function original()
public function string($value, $glue = null)
{
return $this->text;
$string = $this->map(function ($item, $key) use ($value, $glue) {
if ($item->partOfSpeech === 'symbol' && preg_match('/\\s/', $glue)) {
return $item->$value;
}

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

return $this->cutFirst(implode('', $string->all()), $glue);
}

/**
* Get all words combined as a string.
*
* @param bool $spaces [put divider between words]
* @param string $divider
* Get the original, user inputed text.
*
* @return string
*/
public function get($spaces = false, $divider = ' ')
public function original()
{
return $this->words($spaces, $divider);
return $this->text;
}

/**
Expand All @@ -112,7 +102,7 @@ public function get($spaces = false, $divider = ' ')
*/
public function words($spaces = false, $divider = ' ')
{
return $this->makePropertyString('word', $spaces, $divider);
return $this->pluck('word');
}

/**
Expand All @@ -125,7 +115,7 @@ public function words($spaces = false, $divider = ' ')
*/
public function lemmas($spaces = false, $divider = ' ')
{
return $this->makePropertyString('lemma', $spaces, $divider);
return $this->pluck('lemma');
}

/**
Expand All @@ -138,7 +128,7 @@ public function lemmas($spaces = false, $divider = ' ')
*/
public function readings($spaces = false, $divider = ' ')
{
return $this->makePropertyString('reading', $spaces, $divider);
return $this->pluck('reading');
}

/**
Expand All @@ -151,7 +141,7 @@ public function readings($spaces = false, $divider = ' ')
*/
public function pronunciations($spaces = false, $divider = ' ')
{
return $this->makePropertyString('pronunciation', $spaces, $divider);
return $this->pluck('pronunciation');
}

/**
Expand All @@ -164,7 +154,7 @@ public function pronunciations($spaces = false, $divider = ' ')
*/
public function partsOfSpeech($spaces = true, $divider = ' ')
{
return $this->makePropertyString('partOfSpeech', $spaces, $divider);
return $this->pluck('partOfSpeech');
}

/**
Expand All @@ -174,7 +164,13 @@ public function partsOfSpeech($spaces = true, $divider = ' ')
*/
public function toHiragana()
{
$this->conversionFlag = 'hiragana';
// $this->conversionFlag = 'hiragana';

$first = $this->first();

if ($first instanceof LimelightWord) {
var_dump(555);
}

return $this;
}
Expand Down Expand Up @@ -219,118 +215,6 @@ public function toFurigana()
return $this;
}

/**
* Get next word.
*
* @return function
*/
public function next()
{
$count = count($this->words);

for ($i = 0; $i < $count; ++$i) {
yield $this->words[$i];
}
}

/**
* Get single word, by word.
*
* @param string $string
*
* @return Limelight\Classes\LimelightWord/InvalidInputException
*/
public function findWord($string)
{
foreach ($this->words as $word) {
if ($word->word() === $string) {
return $word;
}
}

throw new InvalidInputException("Word {$string} does not exist.");
}

/**
* Get single word by index.
*
* @param int $index
*
* @return Limelight\Classes\LimelightWord/InvalidInputException
*/
public function findIndex($index)
{
$count = count($this->words);

if ($count <= $index) {
throw new InvalidInputException("Index {$index} does not exist. Results contain exactly {$count} item(s).");
}

return $this->words[$index];
}

/**
* Loop through words to make string of properties.
*
* @param string $property
* @param bool $space [should results be sparated by spaces?]
*
* @return string
*/
private function makePropertyString($property, $space = false, $divider = ' ')
{
if ($this->isNonLemmaPlugin($property)) {
$string = $this->plugin(ucfirst($this->conversionFlag));

if ($divider !== ' ') {
$string = mb_ereg_replace(' ', $divider, $string);
}

return $string;
}

$string = '';

foreach ($this->words as $word) {
$word->setConversionFlag($this->conversionFlag);

if ($this->shouldTrim($word, $string, $property)) {
$string = substr($string, 0, -1);
}

$string .= $word->$property().($space === true || $this->conversionFlag === 'romaji' ? $divider : '');
}

$this->conversionFlag = null;

return $this->cutLast($string, $divider);
}

/**
* Property is not lemma and conversionFlag is a plugin.
*
* @param string $property
*
* @return bool
*/
private function isNonLemmaPlugin($property)
{
return ($this->conversionFlag === 'furigana' || $this->conversionFlag === 'romaji') && $property !== 'lemma';
}

/**
* Results string should have last space trimmed.
*
* @param LimelightWord $word
* @param string $string
*
* @return bool
*/
private function shouldTrim($word, $string, $property)
{
return $word->partOfSpeech === 'symbol' && substr($string, -1) === ' ' && $property !== 'partOfSpeech';
}

/**
* Cut last char if its is divider.
*
Expand All @@ -339,10 +223,10 @@ private function shouldTrim($word, $string, $property)
*
* @return string
*/
private function cutLast($string, $divider)
private function cutFirst($string, $divider)
{
if (mb_substr($string, -1) === $divider) {
return mb_substr($string, 0, -1);
if (mb_substr($string, 0, 1) === $divider) {
return mb_substr($string, 1);
}

return $string;
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function arrGet($array, $key, $default = null)
if ($this->arrAccessible($array) && $this->arrExists($array, $segment)) {
$array = $array[$segment];
} else {
return value($default);
return $this->value($default);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ private function parseLemma($lemma)
{
$result = $this->limelight->parse($lemma);

return $result->findIndex(0);
return $result->pull(0);
}
}
2 changes: 1 addition & 1 deletion tests/Acceptance/NoParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function it_parses_kana_text()
{
$results = self::$limelight->noParse('できるかな。。。');

$this->assertEquals('できるかな。。。', $results->words());
$this->assertEquals('できるかな。。。', $results->string('word'));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Acceptance/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function it_parses_a_simple_sentence()
{
$results = self::$limelight->parse('音楽を聴きます。');

$this->assertEquals('音楽を聴きます。', $results->words());
$this->assertEquals('音楽を聴きます。', $results->string('word'));

$words = $results->all();

Expand Down Expand Up @@ -64,7 +64,7 @@ public function it_parses_a_slightly_more_complicated_sentence()
{
$results = self::$limelight->parse('東京に行って、パスタを食べてしまった。');

$this->assertEquals('東京に行って、パスタを食べてしまった。', $results->words());
$this->assertEquals('東京に行って、パスタを食べてしまった。', $results->string('word'));

$words = $results->all();

Expand Down Expand Up @@ -110,7 +110,7 @@ public function it_parses_multiple_sentences()
{
$results = self::$limelight->parse('私はすき焼きが大好きです。だから、いつも食べています。');

$this->assertEquals('私はすき焼きが大好きです。だから、いつも食べています。', $results->words());
$this->assertEquals('私はすき焼きが大好きです。だから、いつも食べています。', $results->string('word'));

$words = $results->all();

Expand All @@ -124,7 +124,7 @@ public function it_handles_random_characters()
{
$results = self::$limelight->parse('フキイldksf塩jkdfllsdf帰依kdサブ');

$this->assertEquals('フキイldksf塩jkdfllsdf帰依kdサブ', $results->words());
$this->assertEquals('フキイldksf塩jkdfllsdf帰依kdサブ', $results->string('word'));

$words = $results->all();
}
Expand All @@ -136,7 +136,7 @@ public function it_parses_text()
{
$results = self::$limelight->parse(self::$lib['textOne']);

$this->assertEquals(preg_replace('/\s+/', '', self::$lib['textOne']), $results->words());
$this->assertEquals(preg_replace('/\s+/', '', self::$lib['textOne']), $results->string('word'));

$words = $results->all();

Expand Down
Loading

0 comments on commit dbda222

Please sign in to comment.