PHP Pspell Extension Sentence Spell Checker Tutorial

PHP Pspell Extension Sentence Spell Checker Tutorial

PHP Pspell Extension Sentence Spell Checker Tutorial

Thinking on how to progress from yesterday’s PHP Pspell Extension Word Spell Checker Primer Tutorial start …

  • An English sentence is just word(s).
  • An English sentence involves punctuation.
  • An English sentence might include “numerical words”.

… are all valid considerations, the first the clue to how we progress from “a single word” scenario to a “looping through the sentence word(s)” scenario that is the major step forward, the other two being “the constraints” (within the remit of your project plan) if you will, or the niceties (if attended to), regarding improving our changed second draft small PHP Pspell Sentence Checker web application you can also try below


<?php
// pspell_test.php
// RJM Programming
// September, 2024
// Thanks to https://www.php.net/manual/en/function.pspell-check.php
$results='';
$prefix='';
$midbit='';
$lb='';
$suffix='';
$cstring=" ... This is a valid spelling";
$wstring=" ... Sorry, wrong spelling";
if (isset($_GET['testwords']) || isset($_POST['testwords'])) {
$pspell = pspell_new("en");
$sentence=str_replace("’","'",('' . trim("" . (isset($_GET['testwords']) ? str_replace('+',' ',urldecode($_GET['testwords'])) : '') . (isset($_POST['testwords']) ? str_replace('+',' ',urldecode($_POST['testwords'])) : ''))) . '');
$origwords=explode(' ',trim($sentence));
$sentence=str_replace("’","'",str_replace('~','',str_replace('?~','',str_replace('!~','',str_replace('.~','',('' . trim("" . (isset($_GET['testwords']) ? str_replace('+',' ',urldecode($_GET['testwords'])) : '') . (isset($_POST['testwords']) ? str_replace('+',' ',urldecode($_POST['testwords'])) : ''))) . '~')))));
$words=explode(' ',trim($sentence));
$finalwords=explode(' ',trim($sentence));
$fwords=explode(' ',trim($sentence));
if (sizeof($words) > 1) {
$results='<table style="border:1px dotted pink;"><tr><td>';
$suffix='</td></tr></table>';
$lb='<br>';
$midbit='</td><td>';
$cstring='&#10004;';
$wstring='&#10060;';
}

for ($jj=0; $jj<sizeof($words); $jj++) {
$finalwords[$jj]='';
$fwords[$jj]='';
for ($kk=0; $kk<strlen($words[$jj]); $kk++) {
if (substr(substr($words[$jj],$kk),0,1) >= '0' && substr(substr($words[$jj],$kk),0,1) <= '9') {
$finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
} else if (strtolower(substr(substr($words[$jj],$kk),0,1)) >= 'a' && strtolower(substr(substr($words[$jj],$kk),0,1)) <= 'z') {
$finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
$fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
} else if (substr(substr($words[$jj],$kk),0,1) == '-') {
$finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
$fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
} else if (substr(substr($words[$jj],$kk),0,1) == "'" && $kk != 0 && $kk != (-1 + strlen($words[$jj]))) {
$finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
$fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
}
}
if (pspell_check($pspell, $finalwords[$jj] ) || $fwords[$jj] == '' || is_numeric('' . $fwords[$jj])) {
$results.="" . $origwords[$jj] . $lb . $cstring;
} else {
$results.="" . $origwords[$jj] . $lb . $wstring;
$suggestions = pspell_suggest($pspell, "" . $words[$jj]);
$results.="<br><br>";
$ii=0;
foreach ($suggestions as $suggestion) {
if (strpos($results, '<select') === false) {
$ii=2;
$results.='<select size=2><option value="">Possible spelling ...</option></select>';
} else {
$ii++;
}
$results=str_replace('</select>', '<option value="' . $suggestion . '">' . $suggestion . '</option></select>', $results);
}
if ($ii > 0) {
$results=str_replace('<select', '<select onclick="if (this.size != ' . $ii . ') { this.size=' . $ii . '; }" ', $results);
}
}
$results=str_replace('</select>', '</SELECT>', str_replace('<select', '<SELECT', $results));
$results.=$midbit;
}
$results.=$suffix;
}
echo "<html>
<head>
<style>
td {
vertical-align: top;
}
</style>
<title>Try out Pspell - RJM Programming - September, 2024</title>
</head>
<body>
<h1>Try out Pspell for English</h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank href='//www.php.net/manual/en/function.pspell-check.php'>php.net</a></h4>
<div id=results>" . $results . "</div><br>
<br>
<form action=./pspell_test.php method=POST>
<textarea style=width:80%; data-type=text name=testwords id=words placeholder='Type in your word(s) to check the spelling, regarding ...' value=''></textarea>
<br><br><input type=submit value=Check style=background-color:yellow;></input>
</form>
</body>
</html>";
?>

Stop Press

We realized, after the work above, multiple correcting dropdown suggestion lists were not happening, and we remedied this with our changed third draft small PHP Pspell Sentence Checker web application, here on October 1, after saying “rabbits”.


Previous relevant PHP Pspell Extension Word Spell Checker Primer Tutorial is shown below.

PHP Pspell Extension Word Spell Checker Primer Tutorial

PHP Pspell Extension Word Spell Checker Primer Tutorial

We’re interested in Spell Checking, and saw an extension called Pspell … installed here (for PHP 8.1 purposes) on AlmaLinux via …


dnf install ea-php81-php-pspell.x86_64

… which helps out in this regard, us just starting with …

  • English
  • single word

… analysis in this first draft Single English word spell checker you can also try below …

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, ESL, Tutorials and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *