php-speller icon indicating copy to clipboard operation
php-speller copied to clipboard

Throws runtime exception when loading languages

Open v1ta opened this issue 8 years ago • 7 comments

    public function getSupportedLanguages()
    {
        if (null === $this->supportedLanguages) {
            $process = $this->createProcess('-D');
            $process->run();
            if (!$process->isSuccessful()) {
                throw new \RuntimeException(sprintf('hunspell: %s', $process->getErrorOutput()));
            }

Is throwing an exception when the command is seemingly producing the correct output:

Command output:

.::/usr/share/hunspell:/usr/share/myspell:/usr/share/myspell/dicts:/Library/Spelling:/home/vagrant/.openoffice.org/3/user/wordbook:.openoffice.org2/user/wordbook:.openoffice.org2.0/user/wordbook:Library/Spelling:/opt/openoffice.org/basis3.0/share/dict/ooo:/usr/lib/openoffice.org/basis3.0/share/dict/ooo:/opt/openoffice.org2.4/share/dict/ooo:/usr/lib/openoffice.org2.4/share/dict/ooo:/opt/openoffice.org2.3/share/dict/ooo:/usr/lib/openoffice.org2.3/share/dict/ooo:/opt/openoffice.org2.2/share/dict/ooo:/usr/lib/openoffice.org2.2/share/dict/ooo:/opt/openoffice.org2.1/share/dict/ooo:/usr/lib/openoffice.org2.1/share/dict/ooo:/opt/openoffice.org2.0/share/dict/ooo:/usr/lib/openoffice.org2.0/share/dict/ooo
AVAILABLE DICTIONARIES (path is not mandatory for -d option):
/usr/share/hunspell/en_US
LOADED DICTIONARY:
/usr/share/hunspell/en_US.aff
/usr/share/hunspell/en_US.dic
Hunspell 1.3.2
>

Error output:

-- Error message --

    hunspell: SEARCH PATH:
    .::/usr/share/hunspell:/usr/share/myspell:/usr/share/myspell/dicts:/Library/Spelling:/var/empty/.openoffice.org/3/user/wordbook:.openoffice.org2/user/wordbook:.openoffice.org2.0/user/wordbook:Library/Spelling:/opt/openoffice.org/basis3.0/share/dict/ooo:/usr/lib/openoffice.org/basis3.0/share/dict/ooo:/opt/openoffice.org2.4/share/dict/ooo:/usr/lib/openoffice.org2.4/share/dict/ooo:/opt/openoffice.org2.3/share/dict/ooo:/usr/lib/openoffice.org2.3/share/dict/ooo:/opt/openoffice.org2.2/share/dict/ooo:/usr/lib/openoffice.org2.2/share/dict/ooo:/opt/openoffice.org2.1/share/dict/ooo:/usr/lib/openoffice.org2.1/share/dict/ooo:/opt/openoffice.org2.0/share/dict/ooo:/usr/lib/openoffice.org2.0/share/dict/ooo
    AVAILABLE DICTIONARIES (path is not mandatory for -d option):
    /usr/share/hunspell/en_US
    Can't open affix or dictionary files for dictionary named "default"

v1ta avatar Feb 23 '17 16:02 v1ta

Hunspell can't find some dictionary files:

Can't open affix or dictionary files for dictionary named "default"

Do you use custom dictionaries? Can you show your code?

mekras avatar Feb 25 '17 09:02 mekras

Sadly it's proprietary :( I'm pointing it at the standard US dict (hunspell-en-us) that ships with Ubuntu LTS 14.04 (which is named as shown above).

v1ta avatar Feb 28 '17 03:02 v1ta

Can you show just a part of code where you creating and configuring Hunspell instance?

mekras avatar Feb 28 '17 08:02 mekras

public function spellcheck() {

      $tokens = $this->bodyParameters();
      $corrections = [];
      $dict = [ 'en_US' ];

      $speller = new Hunspell( '/usr/bin/hunspell' );
      //$speller->setLanguages( $dict );

      $source = new StringSource( implode( ',', $tokens[ 'words' ] ) );

      foreach ( $speller->checkText( $source, $dict ) as $suggestion ) {
          $word = $suggestion->word;
          $corrections[ $word ] = $suggestion->suggestions;
      }

      return $this->respond( $corrections );

  } 

That function I've commented out is how I've gotten around it for now (I forked the repo and added it). I also experimented w/changing the command passed (the block where the default languages are parsed from the output of hunspell -D) to the CLI, specifying the dict allowed it to work.

v1ta avatar Mar 02 '17 18:03 v1ta

Hmm… Still can't reproduce. I've installed Hunspell 1.3.3:

mekras@mekras ~/t/h/usr> env LD_LIBRARY_PATH=lib64 bin/hunspell -v
@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.3)

And use this test code:

<?php
use Mekras\Speller\Hunspell\Hunspell;
use Mekras\Speller\Source\StringSource;

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

$dict = [ 'en_US' ];
$speller = new Hunspell( 'LD_LIBRARY_PATH=/home/mekras/tmp/hunspell/usr/lib64 /home/mekras/tmp/hunspell/usr/bin/hunspell' );
$source = new StringSource( 'tigr,cta,brid' );
foreach ( $speller->checkText( $source, $dict ) as $suggestion ) {
   print_r($suggestion->suggestions);
}

Works fine. I have no ideas… :-(

mekras avatar Mar 03 '17 09:03 mekras

On my server the dictionaries are stored under /usr/share/myspell, and I know this is not the best way to solve it, but for example, if your default language is en_US, then inside that folder, make a copy of the en_US.aff file and the en_US.dic file and then rename the copies to default.aff and default.dic respectively. This solved the issue for me. Hope this helps.

JPPreusDev avatar May 15 '17 07:05 JPPreusDev

just set LANG

if (getenv('LANG') == false) {
    putenv('LANG=en_US.UTF-8');
}

sergsan avatar Sep 28 '17 09:09 sergsan