作者共發了7篇帖子。 內容轉換:不轉換▼
 
點擊 回復
299 6
【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

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:299 回複數:6
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-2-22 10:39
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。