pspell and aspell with php for german language

2k views Asked by At

I am trying to use the pspell in combination with aspell for PHP.

I have installed it on this way:

sudo apt-get install libpspell-dev 
sudo apt-get install php5-pspell
sudo apt-get install aspell-de

After that, here is an example code:

$pspell_link = pspell_new("de");
var_dump(pspell_check($pspell_link, "Verkäuferin"));
if (!pspell_check($pspell_link, "Verkäuferin")) {
    $vorschlaege = pspell_suggest($pspell_link, "verkäuferin");
    foreach ($vorschlaege as $vorschlag) {
    echo "Mögliche Schreibweise:" . $vorschlag . "<br>";
    }
}

There are two problems:

  1. This example don't understand german umlauts
  2. For the substantives they need to be Uppercase so that the pspell_check returns true. When I have the words all in Lowercase, how to become true as well? For example "Ball" would return true, but "ball" will return false. How to solve, that for "ball" the check will return true as well?
2

There are 2 answers

2
Lambart On

Try specifying a character encoding. I was able to get your code to work by simply changing the first line to:

$pspell_link = pspell_new("de", "", "", "utf-8");

Also: Even though this allowed "Verkäuferin" to pass the spell check, my server was unable to output letters with umlauts correctly until I put this line in my php.ini file.

default_charset = "utf-8"

For more information on character encodings with PHP, this page looks useful:

http://kore-nordmann.de/blog/php_charset_encoding_FAQ.html#which-charset-encoding-do-strings-have-in-php

As for your second question, although the aspell package that PHP uses supports case-insensitive matching, unfortunately PHP doesn't allow you to select that option. Here is the best solution I could come up with:

$pspell_link = pspell_new("de", "", "", "utf-8");
$wort = "verkäuferin";
$richtig = pspell_check($pspell_link, $wort);
if (!$richtig) {
    $vorschlaege = pspell_suggest($pspell_link, $wort);
    // make a copy of the array with all words in lowercase, so we can still
    // display the original suggestions if necessary
    $kleinschrift_vorschlaege = array_map('strtolower', $vorschlaege);
    // convert the original word to lowercase before comparing it
    $richtig = in_array(strtolower($wort), $kleinschrift_vorschlaege);
}
echo "'$wort' ist " . ($richtig ? "" : "nicht") . " richtig buchstabiert.<br/>";
if (!$richtig) {
  echo "Mögliche Schreibweisen:<br/>";
  foreach ($vorschlaege as $vorschlag) {
    echo "&nbsp; &nbsp; " .  $vorschlag . "<br/>";
  }
}
0
Arvind Bhardwaj On

There is an easy solution. Just do this:

$word = ucfirst($word); //Always capitalize to avoid case sensitive error
if (!pspell_check($dict, $word)) {
   $suggestions = pspell_suggest($dictionary, $word);
}