目前共有7篇帖子。
【PHP扩展】Enchant扩展——PHP单词拼写检查和拼写建议
1樓 巨大八爪鱼 2015-2-21 16:46
官方文档:http://es.php.net/manual/en/book.enchant.php
示例程序:
<?php
$broker = enchant_broker_init();
$dictionaries = enchant_broker_list_dicts($broker);
echo "This server provides the following dictionaries:<br>";
$ar = array();
foreach ($dictionaries as $dictionary) {
    array_push($ar, $dictionary["lang_tag"]);
}
echo join(",", $ar);
echo "<hr>";
$words = array("calender", "roll", "specially", "hasband", "stuff", "staff", "hablar", "carbeza", "dolor", "tengo", "tener", "ser", "estoy", "de", "muchacha", "mucho", "llamo", "kilo", "infinitivo", "cultura", "nada", "cama", "hasta", "papa", "mama", "lentes", "leche", "idioma", "finito", "usted", "ustedes", "yo", "ella", "nosotros", "vosotros", "ellos", "ellas", "contamos", "connex", "connection", "disambigration", "somos", "days", "domingo", "lunes", "vertes", "enero", "septiembre", "octubre", "noviember", "paitent", "september", "November", "January", "April", "abril", "timid");

$languages = array("en_US", "es");
foreach ($languages as $language) {
    echo "<h3>For $language:</h3>";
    if (enchant_broker_dict_exists($broker, $language)) {
        $dictionary = enchant_broker_request_dict($broker, $language);
        foreach ($words as $word) {
            if (enchant_dict_check($dictionary, $word)) {
                echo "Word '$word' is spelled correctly.<br>";
            } else {
                echo "<b>Word '$word' is spelled incorrectly.</b><br>";
                $suggestions = enchant_dict_suggest($dictionary, $word);
                echo "<span style=\"color:green\">" . join(", ", $suggestions) . "</span><br>";
            }
        }

        enchant_broker_free_dict($dictionary);
    } else {
        echo "Dictionary '$language' doesn't exist.<br>";
    }
}
enchant_broker_free($broker);
?>

运行结果:
This server provides the following dictionaries:
en_USFor en_US:Word 'calender' is spelled incorrectly.
calendar, ca lender, ca-lender, lender, candler, Calder, caldera
Word 'roll' is spelled correctly.
Word 'specially' is spelled correctly.
Word 'hasband' is spelled incorrectly.
hatband, husband, has band, has-band, hairband
Word 'stuff' is spelled correctly.
Word 'staff' is spelled correctly.
Word 'hablar' is spelled incorrectly.
Harlan
Word 'carbeza' is spelled incorrectly.
carbide
Word 'dolor' is spelled correctly.
Word 'tengo' is spelled incorrectly.
tango, ten go, ten-go, gotten
Word 'tener' is spelled incorrectly.
tenet, tenser, tenner, tender, enter, tenor, toner, tuner, ten er, ten-er, terrine, tern
Word 'ser' is spelled incorrectly.
tier, res, see, set, er, sere, seer, sear, user, serf, sir, sea, sen, sec, per
Word 'estoy' is spelled incorrectly.
es toy, es-toy, Tolstoy
Word 'de' is spelled incorrectly.
DE, ed, d, e, die, den, doe, ode, due, deg, deb, dye, def, dew, re
Word 'muchacha' is spelled incorrectly.
Duchamp
Word 'mucho' is spelled incorrectly.
much, macho, much o, mucous
Word 'llamo' is spelled incorrectly.
llano, llama, Alamo
Word 'kilo' is spelled correctly.
Word 'infinitivo' is spelled incorrectly.
infinitive, infinitival
Word 'cultura' is spelled incorrectly.
cultural, culture
Word 'nada' is spelled incorrectly.
Dada, Canada, Adana, Adan
Word 'cama' is spelled incorrectly.
cams, cam, came, coma, lama, mama, camp, Rama, Gama, Kama, ca ma, ca-ma, cam a, CAM
Word 'hasta' is spelled incorrectly.
hast, Shasta, haste, pasta, hasty, has ta, has-ta, hast a, Tasha, stash
Word 'papa' is spelled correctly.
Word 'mama' is spelled correctly.
Word 'lentes' is spelled incorrectly.
lenses, lent es, lent-es, Lents, gentles, tentacles, lent
Word 'leche' is spelled incorrectly.
lech, leches, lecher, leched, leech, lech e, heckle
Word 'idioma' is spelled incorrectly.
idioms, idiom, idiom a
Word 'finito' is spelled incorrectly.
finite
Word 'usted' is spelled incorrectly.
used, rusted, ousted, lusted, dusted, gusted, busted, us ted, us-ted, tested, stetted, steed
Word 'ustedes' is spelled incorrectly.
tested
Word 'yo' is spelled correctly.
Word 'ella' is spelled incorrectly.
Ella, ells, ell, fella, Della, Bella, ell a
Word 'nosotros' is spelled incorrectly.
nostrils
Word 'vosotros' is spelled incorrectly.
provosts
Word 'ellos' is spelled incorrectly.
ells, cellos, hellos, ell's, jellos
Word 'ellas' is spelled incorrectly.
ells, fellas, ell's, ell as, ell-as, paellas, Ascella, Ella, Aspell
Word 'contamos' is spelled incorrectly.
contaminants
Word 'connex' is spelled incorrectly.
conned, convex, cone
Word 'connection' is spelled correctly.
Word 'disambigration' is spelled incorrectly.
disambiguation, disintegration
Word 'somos' is spelled incorrectly.
solos, homos, so mos, so-mos, bosom
Word 'days' is spelled correctly.
Word 'domingo' is spelled incorrectly.
Domingo, doming, domino, dooming, doming o
Word 'lunes' is spelled incorrectly.
lines, lubes, lunges, lanes, lures, runes, lutes, tunes, dunes, lungs, luges, lunches
Word 'vertes' is spelled incorrectly.
verses, verges, vertexes, vertices
Word 'enero' is spelled incorrectly.
Nero
Word 'septiembre' is spelled incorrectly.
September
Word 'octubre' is spelled incorrectly.
October
Word 'noviember' is spelled incorrectly.
November, nonmember
Word 'paitent' is spelled incorrectly.
patient, patent, penitent, painted, painter, paint
Word 'september' is spelled incorrectly.
September
Word 'November' is spelled correctly.
Word 'January' is spelled correctly.
Word 'April' is spelled correctly.
Word 'abril' is spelled incorrectly.
brill
Word 'timid' is spelled correctly.
For es:Dictionary 'es' doesn't exist.

2樓 巨大八爪鱼 2015-2-21 16:48
Enchant 函数
enchant_broker_describe — Enumerates the Enchant providers
enchant_broker_dict_exists — Whether a dictionary exists or not. Using non-empty tag
检查一个字典是否存在
enchant_broker_free_dict — Free a dictionary resource
释放字典资源
enchant_broker_free — Free the broker resource and its dictionnaries
释放Broker资源
enchant_broker_get_error — Returns the last error of the broker
enchant_broker_init — create a new broker object capable of requesting
初始化Broker
enchant_broker_list_dicts — Returns a list of available dictionaries
enchant_broker_request_dict — create a new dictionary using a tag
enchant_broker_request_pwl_dict — creates a dictionary using a PWL file
enchant_broker_set_ordering — Declares a preference of dictionaries to use for the language
enchant_dict_add_to_personal — add a word to personal word list
enchant_dict_add_to_session — add 'word' to this spell-checking session
enchant_dict_check — Check whether a word is correctly spelled or not
检查一个单词是否拼写正确
enchant_dict_describe — Describes an individual dictionary
enchant_dict_get_error — Returns the last error of the current spelling-session
enchant_dict_is_in_session — whether or not 'word' exists in this spelling-session
enchant_dict_quick_check — Check the word is correctly spelled and provide suggestions
检查单词是否拼写正确,如果拼写不正确则给出单词拼写建议
enchant_dict_store_replacement — Add a correction for a word
enchant_dict_suggest — Will return a list of values if any of those pre-conditions are not met
给出单词拼写建议
3樓 巨大八爪鱼 2015-2-21 16:50
到目前为止我还没找到添加字典的方法。比如添加西班牙语字典。
我在上面的单词列表$words中放置了很多西班牙语单词,但透过英语字典的检查,结果为拼写错误。
php官方文档只有英文文档,雅虎英文搜索引擎也没搜出什么名堂,全是官方文档。。。。。
4樓 巨大八爪鱼 2015-2-21 16:57
enchant /ɪnˈtʃɑ:nt/ v. 使心醉,使迷惑;用魔法迷惑
5樓 巨大八爪鱼 2015-2-22 10:33

在我的Fedora Linux系统上,php有这个库
6樓 巨大八爪鱼 2015-2-22 10:36

在php5.6.6的官方压缩包中,也有enchant的案例。
7樓 巨大八爪鱼 2015-2-22 10:39
不过还有一个叫pspell的库(extension=php_pspell.dll),也能用于拼写检查:
http://php.net/manual/zh/intro.pspell.php

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
 
 
©2010-2024 Arslanbar [手機版] [桌面版]
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。