4

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?
user229044
  • 232,980
  • 40
  • 330
  • 338
Mutatos
  • 1,675
  • 4
  • 25
  • 55
  • German umlauts problem is fixed with this: $pspell_link = pspell_new("de", "", "", "UTF-8"); But the uppercase problem is not fixed yet :-( – Mutatos Apr 30 '12 at 19:34

2 Answers2

2

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/>";
  }
}
Lambart
  • 1,985
  • 2
  • 21
  • 37
  • Ooops! I just realized you already figured this out. If you answer your own question you should probably post an answer and approve it. Comments aren't so obvious. :) – Lambart Jul 27 '12 at 01:37
  • 1
    There, I also tried to answer your second question. Hope this helps someone. – Lambart Jul 27 '12 at 02:09
0

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);
}
Arvind Bhardwaj
  • 5,231
  • 5
  • 35
  • 49