Skip to content

Develop #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The current version of the plugin is v0.2.4 Beta.
* HTML parsing (for using the spellchecker within WSYIWYG editors)
* Text parsing (for using the spellchecker on form fields)
* Multiple fields
* Multiple PHP back-end drivers (Enchant, PSpell, Google)
* Multiple PHP back-end drivers (Enchant, PSpell, Google, Yandex)
* Friendly API
* Event based
* MIT licensed
Expand Down
55 changes: 55 additions & 0 deletions src/examples/english/text-yandex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Spell Checker example - Text parser using Yandex</title>
<meta name="description" content="">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/jquery.spellchecker.css" />
</head>
<body>
<p>This example demonstrates how the <em>text parser</em> can be used to check the spelling of text in a form field.</p>
<div>
<textarea id="text-content" rows="5" cols="25">Add your own text and check the spellling</textarea>
<div id="incorrect-word-list"></div>
</div>
<button id="check-textarea">
Check Spelling
</button>&nbsp;
</div>
<script type="text/javascript" src="../../js/libs/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../../js/jquery.spellchecker.js"></script>
<script type="text/javascript">

(function() {

var element = $('#text-content');

// Init the text spellchecker
var spellchecker = new $.SpellChecker(element, {
lang: 'en',
parser: 'text',
webservice: {
path: '../../webservices/php/SpellChecker.php',
driver: 'yandex'
},
suggestBox: {
position: 'above'
},
incorrectWords: {
container: '#incorrect-word-list'
}
});

// Bind spellchecker handler functions
spellchecker.on('check.success', function() {
alert('There are no incorrectly spelt words.');
});

// Check the spelling
$("#check-textarea").click(function(e){
spellchecker.check();
});
})();
</script>
</body>
</html>
57 changes: 57 additions & 0 deletions src/examples/russian/text-yandex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery Spell Checker example</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/jquery.spellchecker.css" />
</head>
<body>
<p>This example demonstrates how the <em>text parser</em> can be used to check the spelling of text in a form field.</p>
<div>
<textarea id="text-content" rows="5" cols="25">Добавить свой собственный текст и проверки орфоoграфии</textarea>
<div id="incorrect-word-list"></div>
</div>
<button id="check-textarea">
Check Spelling
</button>&nbsp;
</div>
<script type="text/javascript" src="../../js/libs/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../../js/jquery.spellchecker.js"></script>
<script type="text/javascript">

(function() {

var element = $('#text-content');

// Init the text spellchecker
var spellchecker = new $.SpellChecker(element, {
lang: 'ru',
parser: 'text',
webservice: {
path: '../../webservices/php/SpellChecker.php',
driver: 'yandex'
},
suggestBox: {
position: 'above'
},
incorrectWords: {
container: '#incorrect-word-list'
}
});

// Bind spellchecker handler functions
spellchecker.on('check.success', function() {
alert('There are no incorrectly spelt words.');
});

// Check the spelling
$("#check-textarea").click(function(e){
spellchecker.check();
});
})();
</script>
</body>
</html>
91 changes: 91 additions & 0 deletions src/webservices/php/SpellChecker/Driver/Yandex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Spellchecker Yandex driver class
* !! Curl is required to use Yandex spellchecker API !!
*/

namespace SpellChecker\Driver;

class Yandex extends \SpellChecker\Driver
{
protected $_default_config = array(
'lang' => 'ru,en'
);

const IGNORE_UPPERCASE = 1;
const IGNORE_DIGITS = 2; // ignore words with digits
const IGNORE_URLS = 4;
const FIND_REPEAT_WORDS = 8;
const IGNORE_LATIN = 16;
const NO_SUGGEST = 32;
const FLAG_LATIN = 128;
const IGNORE_CAPITALIZATION = 512;

public function get_incorrect_words()
{
$texts = (array) \SpellChecker\Request::post('text');
$options = self::IGNORE_DIGITS + self::IGNORE_URLS;

$xml = $this->check_texts($texts, $options + self::NO_SUGGEST);

$response = array();
foreach ($xml->SpellResult as $result)
{
$words = array();
foreach ($result->error as $error)
{
$words[] = (string) $error->word[0];
}
$response[] = $words;
}

$this->send_data('success', $response);
}

public function get_word_suggestions($word = NULL)
{
$xml = $this->check_texts(array($word), 0);

$suggestions = array();
$result = $xml->SpellResult[0];
foreach ($result->error as $error)
{
foreach ($error->s as $s)
{
$suggestions[] = (string) $s;
}
break;
}

return $suggestions;
}

public function check_word($word = NULL) {}

private function check_texts($texts, $options)
{
$url = 'http://speller.yandex.net/services/spellservice/checkTexts';

$body = 'lang=' . urlencode($this->_config['lang']);
$body .= '&options=' . $options;
foreach ($texts as $text)
{
$body .= "&text=" . urlencode($text);
}

if (!function_exists('curl_init'))
{
exit('Curl is not available');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$xml_response = curl_exec($ch);
curl_close($ch);

return simplexml_load_string($xml_response);
}
}