Skip to content

Commit

Permalink
#2: Word suggestions with space splits up
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Aug 1, 2016
1 parent dee33c4 commit a1c17df
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
40 changes: 21 additions & 19 deletions src/Hunspell/Hunspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/Hunspell/HunspellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -45,18 +45,20 @@ 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('<will be ignored and loaded from fixtures/check.txt>');
$issues = $hunspell->checkText($source, ['en']);
static::assertCount(6, $issues);
static::assertEquals('Tigr', $issues[0]->word);
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
);

Expand Down
14 changes: 7 additions & 7 deletions tests/Hunspell/fixtures/check.txt
Original file line number Diff line number Diff line change
@@ -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
*
*
*
*
13 changes: 0 additions & 13 deletions tests/Hunspell/fixtures/hunspell.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Hunspell/fixtures/hunspell.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/Hunspell/fixtures/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Tigr, tiger, burning страх
In theforests of the night,
What imortal hand or eey
CCould frame thy fearful symmetry?

0 comments on commit a1c17df

Please sign in to comment.