diff --git a/src/Helpers/PluginHelper.php b/src/Helpers/PluginHelper.php new file mode 100644 index 0000000..ceea6e1 --- /dev/null +++ b/src/Helpers/PluginHelper.php @@ -0,0 +1,62 @@ +getPlugins(); + + foreach ($plugins as $plugin => $namespace) { + $this->validatePlugin($namespace); + + $pluginClass = new $namespace($text, $node, $tokens, $words); + + $pluginResults[$plugin] = $this->firePlugin($pluginClass); + } + + return $pluginResults; + } + + /** + * Validate plugin class exists. + * + * @param string $namespace + */ + private function validatePlugin($namespace) + { + if (!class_exists($namespace)) { + throw new PluginNotFoundException("Plugin {$namespace} not found."); + } + } + + /** + * Fire the plugin. + * + * @param Plugin $plugin + * + * @return mixed + */ + private function firePlugin(Plugin $plugin) + { + return $plugin->handle(); + } +} diff --git a/src/Limelight.php b/src/Limelight.php index bb2af88..9e69629 100644 --- a/src/Limelight.php +++ b/src/Limelight.php @@ -4,6 +4,7 @@ use Limelight\Parse\Parser; use Limelight\Config\Config; +use Limelight\Parse\NoParser; use Limelight\Parse\Tokenizer; use Limelight\Parse\TokenParser; @@ -45,6 +46,20 @@ public function parse($text, $runPlugins = true) return $parser->handle($text, $runPlugins); } + /** + * Run given text through plugins without mecab parsing. Kanji input will fail. + * + * @param string $text + * + * @return Limelight\Classes\LimelightResults/ InvalidInputException + */ + public function noParse($text) + { + $noParser = new NoParser(); + + return $noParser->handle($text); + } + /** * MeCab parseToNode method. Returns native Limelight node object. * diff --git a/src/Parse/NoParser.php b/src/Parse/NoParser.php new file mode 100644 index 0000000..3317658 --- /dev/null +++ b/src/Parse/NoParser.php @@ -0,0 +1,75 @@ +hasKanji($text)) { + throw new InvalidInputException('Text must not contain kanji.'); + } + + $limelight = new Limelight(); + + $converter = new Converter($limelight); + + $token = $this->buildToken($text); + + $properties = $this->buildProperties(); + + $words = [new LimelightWord($token, $properties, $converter)]; + + $pluginResults = $this->runPlugins($text, null, $token, $words); + + return new LimelightResults($text, $words, $pluginResults); + } + + /** + * Build token using raw text for all properties. + * + * @param string $text + * + * @return array + */ + private function buildToken($text) + { + return [ + 'literal' => $text, + 'lemma' => $text, + 'reading' => $text, + 'pronunciation' => $text, + ]; + } + + /** + * Build array on full properties. + * + * @return array + */ + private function buildProperties() + { + return [ + 'partOfSpeech' => null, + 'grammar' => null, + ]; + } +} diff --git a/src/Parse/Parser.php b/src/Parse/Parser.php index a189fc1..e5d09a7 100644 --- a/src/Parse/Parser.php +++ b/src/Parse/Parser.php @@ -3,13 +3,13 @@ namespace Limelight\Parse; use Limelight\Mecab\Mecab; -use Limelight\Config\Config; -use Limelight\Plugins\Plugin; +use Limelight\Helpers\PluginHelper; use Limelight\Classes\LimelightResults; -use Limelight\Exceptions\PluginNotFoundException; class Parser { + use PluginHelper; + /** * @var implements Limelight\Mecab\Mecab */ @@ -59,57 +59,4 @@ public function handle($text, $runPlugins) return new LimelightResults($text, $words, $pluginResults); } - - /** - * Run all registered plugins. - * - * @param string $text - * @param Node $node - * @param array $tokens - * @param array $words - * - * @return array - */ - private function runPlugins($text, $node, $tokens, $words) - { - $pluginResults = []; - - $config = Config::getInstance(); - - $plugins = $config->getPlugins(); - - foreach ($plugins as $plugin => $namespace) { - $this->validatePlugin($namespace); - - $pluginClass = new $namespace($text, $node, $tokens, $words); - - $pluginResults[$plugin] = $this->firePlugin($pluginClass); - } - - return $pluginResults; - } - - /** - * Validate plugin class exists. - * - * @param string $namespace - */ - private function validatePlugin($namespace) - { - if (!class_exists($namespace)) { - throw new PluginNotFoundException("Plugin {$namespace} not found."); - } - } - - /** - * Fire the plugin. - * - * @param Plugin $plugin - * - * @return mixed - */ - private function firePlugin(Plugin $plugin) - { - return $plugin->handle(); - } } diff --git a/src/Parse/TokenParser.php b/src/Parse/TokenParser.php index 9de2985..951558a 100644 --- a/src/Parse/TokenParser.php +++ b/src/Parse/TokenParser.php @@ -32,7 +32,7 @@ class TokenParser /** * Parse the text by filtering through the tokens. * - * @return [type] [description] + * @return array */ public function parseTokens($tokens) { @@ -97,6 +97,26 @@ private function getProperties($registry, $previousWord, $previous, $current, $n return $properties; } + /** + * Update current if reading does not exist. + * + * @param array $current + * + * @return array + */ + private function updateCurrent($current) + { + $current['lemma'] = $current['literal']; + + $katakana = mb_convert_kana($current['literal'], 'C'); + + $current['reading'] = $katakana; + + $current['pronunciation'] = $katakana; + + return $current; + } + /** * Append current word to last word in words array. * @@ -126,9 +146,9 @@ private function appendWordToLast($current, $properties, $previousWord) /** * Make new word and append it to words array. * - * @param array $current - * @param array $properties - * @param Converter $converter + * @param array $current + * @param array $properties + * @param Converter $converter */ private function makeNewWord($current, $properties, Converter $converter) { diff --git a/src/Plugins/Library/Romanji/Romanji.php b/src/Plugins/Library/Romanji/Romanji.php index 3878515..207b58c 100644 --- a/src/Plugins/Library/Romanji/Romanji.php +++ b/src/Plugins/Library/Romanji/Romanji.php @@ -20,6 +20,8 @@ public function handle() $romanjiString = ''; foreach ($this->words as $word) { + $spaces = true; + $hiraganaWord = mb_convert_kana($word->reading, 'c'); $romanjiWord = $style->handle($hiraganaWord, $word); diff --git a/tests/Parse/NoParseTest.php b/tests/Parse/NoParseTest.php new file mode 100644 index 0000000..aa711c6 --- /dev/null +++ b/tests/Parse/NoParseTest.php @@ -0,0 +1,58 @@ +noParse('できるかな。。。'); + + $this->assertEquals('できるかな。。。', $results->words()); + } + + /** + * It gets romanji for kana text. + * + * @test + */ + public function it_gets_romanji_for_kana_text() + { + $results = self::$limelight->noParse('ねんがっぴ'); + + $this->assertEquals('Nengappi', $results->plugin('romanji')); + } + + /** + * It throws exception for kanji input. + * + * @test + * @expectedException Limelight\Exceptions\InvalidInputException + * @expectedExceptionMessage Text must not contain kanji. + */ + public function it_throws_exception_for_kanji_text() + { + $results = self::$limelight->noParse('今日'); + } +}