New package: text.spelling

at 2009-05-17 in Announcements by friebe (0 comments)

The new text.spelling package provides a wrapper around the pspell extension and provides an easy way to spell-check texts. Here's the basic usage:

<?php 
$spell= new SpellChecker('en');
$spell->check('Hello'); // TRUE
$spell->check('Bahnhof'); // FALSE
$spell->suggestionsFor('delibrate'); // [ "deliberate", "deliberator", "deliberated", ... ]
?>

To spell-check an entire text, you can use the text.Tokenizer implementations to split up the text into words:

<?php 
$st= new StreamTokenizer(new FileInputStream(new File('candidate.txt')), " \r\n\t.:,;-"));
while
($st->hasMoreTokens()) {
$word= $st->nextToken();
if
(is_numeric($word)) continue; // Ignore numbers

if
(!$spell->check($word)) {
// Handle misspelled word, e.g. make suggestions, ...
}
}
?>



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES

Related

Find related articles by a search for «New».