|
Lines 1-53
Link Here
|
| 1 |
<?php |
|
|
| 2 |
/*- |
| 3 |
* Copyright (c) 2006 Alex Dupre. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
$lang = $_REQUEST["lang"]; |
| 26 |
$xml = new SimpleXMLElement(file_get_contents("php://input")); |
| 27 |
$spell = pspell_new($lang, "", "", "utf-8", PSPELL_NORMAL); |
| 28 |
$suggestions = array(); |
| 29 |
$offset = 0; |
| 30 |
mb_regex_encoding("UTF-8"); |
| 31 |
foreach (mb_split("\n", $xml->text) as $line) { |
| 32 |
$len = mb_strlen($line, "UTF-8"); |
| 33 |
mb_ereg_search_init($line, "\w+"); |
| 34 |
while (($wpos = mb_ereg_search_pos()) != FALSE) { |
| 35 |
$word = mb_substr($line, $wpos[0], $wpos[1]); |
| 36 |
if (!pspell_check($spell, $word)) { |
| 37 |
$woffset = mb_strlen(mb_substr($line, 0, $wpos[0]), "UTF-8"); |
| 38 |
$wlen = mb_strlen($word, "UTF-8"); |
| 39 |
array_push($suggestions, array($offset + $woffset, $wlen, pspell_suggest($spell, $word))); |
| 40 |
} |
| 41 |
} |
| 42 |
$offset += $len + 1; |
| 43 |
} |
| 44 |
$xml = new SimpleXMLElement("<spellresponse/>"); |
| 45 |
$xml->addAttribute("error", count($suggestions) ? "1" : "0"); |
| 46 |
foreach ($suggestions as $s) { |
| 47 |
$c = $xml->addChild("c", join("\t", $s[2])); |
| 48 |
$c->addAttribute("o", $s[0]); |
| 49 |
$c->addAttribute("l", $s[1]); |
| 50 |
$c->addAttribute("s", "1"); |
| 51 |
} |
| 52 |
header('Content-Type: text/xml'); |
| 53 |
echo $xml->asXML(); |