Skip to content

Commit

Permalink
Example and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Mar 12, 2017
1 parent a418e92 commit db69ef2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Changed

- StringSource, FileSource, HtmlSource and XliffSource now implements EncodingAwareSource.
- HtmlSource and XliffSource now derived from MetaSource.

## 1.5.1 - 2017-03-11

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ You can list languages supported by backend:
print_r($speller->getSupportedLanguages());
```

See [examples](examples/) for more info.

### Source encoding

For aspell, hunspell and ispell source text encoding should be equal to dictionary encoding. You can
Expand Down
101 changes: 101 additions & 0 deletions examples/spellcheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Mekras\Speller\Examples;

use Mekras\Speller\Aspell\Aspell;
use Mekras\Speller\Exception\PhpSpellerException;
use Mekras\Speller\Hunspell\Hunspell;
use Mekras\Speller\Ispell\Ispell;
use Mekras\Speller\Source\FileSource;
use Mekras\Speller\Source\HtmlSource;
use Mekras\Speller\Source\IconvSource;
use Mekras\Speller\Source\XliffSource;

require __DIR__ . '/../vendor/autoload.php';

function showHelp()
{
echo <<<EOT
php-speller example.
Usage: php spellcheck.php [options]
Options:
-b <backend> Backend to use: aspell, hunspell (default) or ispell.
-E <encoding> Dictionary internal encoding.
-e <encoding> Source text encoding (default UTF-8).
-f <format> Treat source as: html, xliff.
-i <filename> Read text from a given file (default is STDIN).
-l <languages> Comma separated list of source text languages (default is system locale).
EOT;
}

$options = getopt('b:E:e:f:i:l:');

/* Choose backend. */
if (array_key_exists('b', $options)) {
switch ($options['b']) {
case 'aspell':
$speller = new Aspell();
break;
case 'ispell':
$speller = new Ispell();
break;
case 'hunspell':
$speller = new Hunspell();
break;
default:
fprintf(STDERR, "Invalid backend: %s\n", $options['b']);
exit(-1);
}
} else {
$speller = new Hunspell();
}

/* Source text encoding */
$encoding = 'UTF-8';
if (array_key_exists('e', $options)) {
$encoding = $options['e'];
}

/* Text source. */
$filename = 'php://stdin';
if (array_key_exists('i', $options)) {
$filename = $options['i'];
}
$source = new FileSource($filename, $encoding);

/* Text source format. */
if (array_key_exists('f', $options)) {
switch ($options['f']) {
case 'html':
$source = new HtmlSource($source);
break;
case 'xliff':
$source = new XliffSource($source);
break;
default:
fprintf(STDERR, "Invalid format: %s\n", $options['f']);
exit(-1);
}
}

/* Source language. */
$languages = [locale_get_default()];
if (array_key_exists('l', $options)) {
$languages = explode(',', $options['l']);
}

/* Dictionary encoding */
if (array_key_exists('E', $options)) {
$source = new IconvSource($source, $options['E']);
}

try {
$issues = $speller->checkText($source, $languages);
} catch (PhpSpellerException $e) {
fprintf(STDERR, $e->getMessage() . PHP_EOL);
exit(-1);
}
foreach ($issues as $issue) {
print_r($issue);
}

0 comments on commit db69ef2

Please sign in to comment.