From a1c17df84283ecfaf392a35e4cd3cd06875b346c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Mon, 1 Aug 2016 17:02:10 +0300 Subject: [PATCH] #2: Word suggestions with space splits up --- src/Hunspell/Hunspell.php | 40 +++++++++++++++------------- tests/Hunspell/HunspellTest.php | 8 +++--- tests/Hunspell/fixtures/check.txt | 14 +++++----- tests/Hunspell/fixtures/hunspell.php | 13 --------- tests/Hunspell/fixtures/hunspell.sh | 16 +++++++++++ tests/Hunspell/fixtures/input.txt | 4 +++ 6 files changed, 53 insertions(+), 42 deletions(-) delete mode 100644 tests/Hunspell/fixtures/hunspell.php create mode 100755 tests/Hunspell/fixtures/hunspell.sh create mode 100644 tests/Hunspell/fixtures/input.txt diff --git a/src/Hunspell/Hunspell.php b/src/Hunspell/Hunspell.php index ae8292e..bad676a 100644 --- a/src/Hunspell/Hunspell.php +++ b/src/Hunspell/Hunspell.php @@ -114,8 +114,7 @@ public function checkText(Source $source, array $languages) if (!$process->isSuccessful()) { throw new \RuntimeException(sprintf('hunspell: %s', $process->getErrorOutput())); } - $result = $process->getOutput(); - $result = explode(PHP_EOL, $result); + $result = explode(PHP_EOL, $process->getOutput()); $issues = []; $lineNo = 1; foreach ($result as $line) { @@ -125,23 +124,26 @@ public function checkText(Source $source, array $languages) $lineNo++; continue; } - $parts = explode(' ', $line); - $code = array_shift($parts); - if ('#' === $code || '&' === $code) { - $word = array_shift($parts); - $issue = new Issue($word); - $issue->line = $lineNo; - $issue->offset = trim(array_shift($parts)); - $issues [] = $issue; - if ('&' === $code) { - $issue->offset = trim(array_shift($parts), ':'); - $issue->suggestions = array_map( - function ($word) { - return trim($word, ', '); - }, - $parts - ); - } + switch ($line[0]) { + case '#': + $parts = explode(' ', $line); + $word = $parts[1]; + $issue = new Issue($word); + $issue->line = $lineNo; + $issue->offset = trim($parts[2]); + $issues [] = $issue; + break; + case '&': + $parts = explode(':', $line); + $parts[0] = explode(' ', $parts[0]); + $parts[1] = explode(', ', trim($parts[1])); + $word = $parts[0][1]; + $issue = new Issue($word); + $issue->line = $lineNo; + $issue->offset = trim($parts[0][3]); + $issue->suggestions = $parts[1]; + $issues [] = $issue; + break; } } diff --git a/tests/Hunspell/HunspellTest.php b/tests/Hunspell/HunspellTest.php index d6bb8cb..7f9a135 100644 --- a/tests/Hunspell/HunspellTest.php +++ b/tests/Hunspell/HunspellTest.php @@ -36,7 +36,7 @@ public function testArgumentEscaping() */ public function testGetSupportedLanguages() { - $hunspell = new Hunspell('php ' . __DIR__ . '/fixtures/hunspell.php'); + $hunspell = new Hunspell(__DIR__ . '/fixtures/hunspell.sh'); static::assertEquals( ['de_BE', 'de_DE', 'de_LU', 'en-GB', 'en_AU', 'en_GB', 'en_US', 'en_ZA', 'ru_RU'], $hunspell->getSupportedLanguages() @@ -45,10 +45,12 @@ public function testGetSupportedLanguages() /** * Test spell checking + * + * See fixtures/input.txt for the source text. */ public function testCheckText() { - $hunspell = new Hunspell('php ' . __DIR__ . '/fixtures/hunspell.php'); + $hunspell = new Hunspell(__DIR__ . '/fixtures/hunspell.sh'); $source = new StringSource(''); $issues = $hunspell->checkText($source, ['en']); static::assertCount(6, $issues); @@ -56,7 +58,7 @@ public function testCheckText() static::assertEquals(1, $issues[0]->line); static::assertEquals(0, $issues[0]->offset); static::assertEquals( - ['Tiger', 'Trig', 'Tier', 'Tigris', 'Tigress'], + ['Ti gr', 'Ti-gr', 'Tiger', 'Trig', 'Tier', 'Tigris', 'Grit', 'Tigress', 'Tagore'], $issues[0]->suggestions ); diff --git a/tests/Hunspell/fixtures/check.txt b/tests/Hunspell/fixtures/check.txt index 1799d79..371ae1a 100644 --- a/tests/Hunspell/fixtures/check.txt +++ b/tests/Hunspell/fixtures/check.txt @@ -1,23 +1,23 @@ -@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.3) -& Tigr 5 0: Tiger, Trig, Tier, Tigris, Tigress +@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.2) +& Tigr 9 0: Ti gr, Ti-gr, Tiger, Trig, Tier, Tigris, Grit, Tigress, Tagore * * # страх 21 * -& theforests 8 3: the forests, the-forests, reforests, deforests, reforest, therefore, reafforest, deforester +& theforests 8 3: the forests, the-forests, reforests, deforests, reforest, therefore, disafforest, forestland * * * * -& imortal 8 5: mortal, immortal, immoral, important, immemorial, mortar, imitable, immutable +& imortal 9 5: mortal, immortal, i mortal, immoral, important, immemorial, mortar, imitable, immutable * * -& eey 8 21: eye, eel, fey, hey, key, Eyre, Ely, emery +& eey 10 21: ea, eye, eery, eel, hey, bey, fey, eek, key, Key -& CCould 5 0: C Could, Could, Cold, Collude, Cuckoldry -* +& CCould 5 0: C Could, Could, Cold, Cuckold, Cloudy * * * +* \ No newline at end of file diff --git a/tests/Hunspell/fixtures/hunspell.php b/tests/Hunspell/fixtures/hunspell.php deleted file mode 100644 index e0fdb2e..0000000 --- a/tests/Hunspell/fixtures/hunspell.php +++ /dev/null @@ -1,13 +0,0 @@ - 1 && '-D' === $argv[1]) { - fwrite(STDERR, file_get_contents(__DIR__ . '/dicts.txt')); - exit(0); -} - -if (in_array('-a', $argv, true)) { - fwrite(STDOUT, file_get_contents(__DIR__ . '/check.txt')); - exit(0); -} diff --git a/tests/Hunspell/fixtures/hunspell.sh b/tests/Hunspell/fixtures/hunspell.sh new file mode 100755 index 0000000..b626ad4 --- /dev/null +++ b/tests/Hunspell/fixtures/hunspell.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# +# hunspell binary stub +# + +folder=$(dirname $0) + +case "$*" in + '-D') + cat "$folder/dicts.txt" >&2 + ;; + *'-a'*) + cat "$folder/check.txt" + ;; +esac +exit 0 diff --git a/tests/Hunspell/fixtures/input.txt b/tests/Hunspell/fixtures/input.txt new file mode 100644 index 0000000..019bdab --- /dev/null +++ b/tests/Hunspell/fixtures/input.txt @@ -0,0 +1,4 @@ +Tigr, tiger, burning страх +In theforests of the night, +What imortal hand or eey +CCould frame thy fearful symmetry? \ No newline at end of file