Skip to content

Commit

Permalink
Feature: Aspell custom dictionary (#13)
Browse files Browse the repository at this point in the history
* Add PersonalDictionary for Aspell
* Update README and CHANGELOG
* Add gitattributes for clean install package
* Remove old phpunit config
* Fix codesniffer errors
  • Loading branch information
icanhazstring committed Jan 17, 2019
1 parent 5d11385 commit 8007814
Show file tree
Hide file tree
Showing 48 changed files with 546 additions and 238 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
6 changes: 6 additions & 0 deletions .gitignore
@@ -1,4 +1,10 @@
# Project specifics
/build

# Composer
/composer.lock
/composer.phar
/vendor/

# Configs.
/phpunit.xml
16 changes: 8 additions & 8 deletions .travis.yml
Expand Up @@ -3,27 +3,27 @@ language: php
sudo: false

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

matrix:
fast_finish: true
include:
- php: 5.6
env:
- COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- PHPUNIT_FLAGS="--coverage-clover build/logs/clover.xml"
# include:
# - php: 5.6
# env:
# - COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
# - PHPUNIT_FLAGS="--coverage-clover build/logs/clover.xml"

before_install:
- travis_retry composer self-update
- sudo apt-get install aspell

install:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction

script:
- mkdir -p build/logs
- vendor/bin/phpcs --standard=PSR12 src/ tests/
- vendor/bin/phpunit ${PHPUNIT_FLAGS}

after_script:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -11,12 +11,17 @@

- PHP 7 support.

## 2.0

- Add custom dictionary support for aspell
- Raise PHP requirement to 7.2
- Dropped `@deprecated` interfaces

## 1.7.2 - 2017-04-30

### Fixed

- HtmlFilter: <script> content should be filtered out.
- HtmlFilter: `<script>` content should be filtered out.
- HtmlFilter: only for "keywords" and "description" meta tags "content" attr should be treated as
string.

Expand Down
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -73,6 +73,23 @@ use Mekras\Speller\Aspell\Aspell;
$speller = new Aspell('/usr/local/bin/aspell');
```

### Custom Dictionary

You can use a custom dictionary for aspell. The dictionary needs to be in the following format:

```
personal_ws-1.1 [lang] [words]
```

Where `[lang]` shout be the shorthand for the language you are using (e.g. `en`) and `[words]` is the count
of words inside the dictionary. **Beware** that there should no spaces at the end of words. Each word should be listed
in a new line.

```php
$aspell = new Aspell();
$aspell->setPersonalDictionary(new Dictionary('/path/to/custom.pws'));
```

### Important

- aspell allow to specify only one language at once, so only first item taken from
Expand Down
17 changes: 13 additions & 4 deletions composer.json
Expand Up @@ -13,15 +13,21 @@
{
"name": "Михаил Красильников",
"email": "m.krasilnikov@yandex.ru"
},
{
"name": "Andreas Frömer",
"email": "blubb0r05+github@gmail.com"
}
],
"require": {
"php": ">=5.6",
"symfony/process": "^3.0|^4.0"
"php": "^7.2",
"symfony/process": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
"php-coveralls/php-coveralls": "^2.0"
"phpunit/phpunit": "^7.5",
"php-coveralls/php-coveralls": "^2.0",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
"psr-4": {
Expand All @@ -32,5 +38,8 @@
"psr-4": {
"Mekras\\Speller\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
}
}
1 change: 0 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -5,7 +5,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
Expand Down
30 changes: 21 additions & 9 deletions src/Aspell/Aspell.php
Expand Up @@ -9,10 +9,10 @@

namespace Mekras\Speller\Aspell;

use Mekras\Speller\Dictionary;
use Mekras\Speller\Exception\ExternalProgramFailedException;
use Mekras\Speller\Ispell\Ispell;
use Mekras\Speller\Source\EncodingAwareSource;
use Mekras\Speller\Source\Source;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\RuntimeException;
Expand All @@ -31,6 +31,11 @@ class Aspell extends Ispell
*/
private $supportedLanguages = null;

/**
* @var Dictionary
*/
private $personalDictionary;

/**
* Create new aspell adapter.
*
Expand Down Expand Up @@ -88,22 +93,25 @@ public function getSupportedLanguages()
return $this->supportedLanguages;
}

/**
* @param Dictionary $dictionary
*/
public function setPersonalDictionary(Dictionary $dictionary)
{
$this->personalDictionary = $dictionary;
}

/**
* Create arguments for external speller.
*
* @param Source $source Text source to check.
* @param array $languages List of languages used in text (IETF language tag).
* @param EncodingAwareSource $source Text source to check.
* @param array $languages List of languages used in text (IETF language tag).
*
* @return string[]
*
* @throws ExternalProgramFailedException
* @throws InvalidArgumentException
* @throws LogicException
* @throws RuntimeException
*
* @since 1.6
*/
protected function createArguments(Source $source, array $languages)
protected function createArguments(EncodingAwareSource $source, array $languages)
{
$args = [
// Input encoding
Expand All @@ -116,6 +124,10 @@ protected function createArguments(Source $source, array $languages)
$args[] = '--lang=' . $languages[0];
}

if ($this->personalDictionary !== null) {
$args[] = '--personal=' . $this->personalDictionary->getPath();
}

return $args;
}
}
35 changes: 35 additions & 0 deletions src/Dictionary.php
@@ -0,0 +1,35 @@
<?php
/**
* PHP Speller.
*
* @copyright 2017, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/

namespace Mekras\Speller;

/**
* Representing any dictionary for a certain speller
*/
class Dictionary
{
/** @var string */
private $dictionaryPath;

/**
* @param string $path
*/
public function __construct(string $path)
{
$this->dictionaryPath = $path;
}

/**
* @return string
*/
public function getPath()
{
return $this->dictionaryPath;
}
}

0 comments on commit 8007814

Please sign in to comment.