Skip to content
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

[BUG] Mecab not set #182

Closed
wants to merge 16 commits into from
Closed
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
6 changes: 4 additions & 2 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ function media_files($get_req)
/**
* Get the phonetic reading of a word based on it's language.
*
* @param array $get_req Array with the fields "text" and "lang" (short language name)
* @param array $get_req Array with the fields "text" and "lang_id" (language_id)
*
* @return string[] JSON-encoded result
*
* @psalm-return array{phonetic_reading: string}
*/
function get_phonetic_reading($get_req): array
{
$data = phonetic_reading($get_req['text'], $get_req['lang']);
$data = phonetic_reading($get_req['text'], $get_req['lang_id']);
return array("phonetic_reading" => $data);
}

Expand All @@ -188,6 +188,7 @@ function get_word_test_ajax($testsql, $word_mode, $lgid, $wordregex, $testtype):
if (empty($word_record)) {
$output = array(
"word_id" => 0,
"word_lg_id" => 0,
"word_text" => '',
"group" => ''
);
Expand Down Expand Up @@ -217,6 +218,7 @@ function get_word_test_ajax($testsql, $word_mode, $lgid, $wordregex, $testtype):

return array(
"word_id" => $word_record['WoID'],
"word_lg_id" => $word_record['WoLgID'],
"solution" => $solution,
"word_text" => $save,
"group" => $html_sentence
Expand Down
68 changes: 42 additions & 26 deletions do_test_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,20 @@ function do_test_prepare_ajax_test_area($selector, $selection, $count, $testtype
);

$sql = "SELECT LgName, LgDict1URI, LgDict2URI, LgGoogleTranslateURI, LgTextSize,
LgRemoveSpaces, LgRegexpWordCharacters, LgRightToLeft
LgRemoveSpaces, LgRegexpWordCharacters, LgRightToLeft, LgTTSVoiceAPI
FROM {$tbpref}languages WHERE LgID = $lgid";
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
$lang = array(
'wb1' => isset($record['LgDict1URI']) ? $record['LgDict1URI'] : "",
'wb2' => isset($record['LgDict2URI']) ? $record['LgDict2URI'] : "",
'wb3' => isset($record['LgGoogleTranslateURI']) ? $record['LgGoogleTranslateURI'] : "",
'textsize' => $record['LgTextSize'],
'removeSpaces' => $record['LgRemoveSpaces'],
'regexword' => $record['LgRegexpWordCharacters'],
'rtlScript' => $record['LgRightToLeft']
'wb3' => isset($record['LgGoogleTranslateURI'])?$record['LgGoogleTranslateURI']:"",
'word_parsing' => isset($record['LgRegexpWordCharacters'])?$record['LgRegexpWordCharacters']:"",
'ttsVoiceApi' => isset($record['LgTTSVoiceAPI'])?$record['LgTTSVoiceAPI']:"",
'textsize' =>isset($record['textsize'])?$record['textsize']:"",
'removeSpaces' =>isset($record['removeSpaces'])?$record['removeSpaces']:"" ,
'regexword' => isset($record['regexword'])?$record['regexword']:"",
'rtlScript' =>isset($record['rtlScript'])?$record['rtlScript']:""
);
mysqli_free_result($res);

Expand Down Expand Up @@ -518,7 +520,7 @@ function get_new_word()
</div>
<?php

do_test_test_interaction_globals($lang['wb1'], $lang['wb2'], $lang['wb3']);
do_test_test_interaction_globals($lang['wb1'], $lang['wb2'], $lang['wb3'],$lang['word_parsing'],$lang['ttsVoiceApi']);

return $count;
}
Expand All @@ -536,7 +538,7 @@ function get_new_word()
*
* @global string $tbpref Table prefix
* @global int $debug Show the SQL query used if 1.
*
* @deprecated use do_test_prepare_ajax_test_area
* @psalm-return int<0, max>
*/
function prepare_test_area($testsql, $totaltests, $count, $testtype): int
Expand Down Expand Up @@ -678,6 +680,8 @@ function prepare_test_area($testsql, $totaltests, $count, $testtype): int
* @param string $wb1 URL of the first dictionary.
* @param string $wb2 URL of the secondary dictionary.
* @param string $wb3 URL of the google translate dictionary.
* @param string $word_parsing Word parsing strategy, usually regular expression or 'mecab'
* @param string $ttsWordApi Third-party voice API
* @param int $testtype Type of test
* @param int $nosent 1 to use single word instead of sentence.
* @param string $save Word or sentence to use for the test
Expand All @@ -687,13 +691,15 @@ function prepare_test_area($testsql, $totaltests, $count, $testtype): int
* @global string $tbpref Database table prefix
* @global string $angDefs Languages definition array
*/
function do_test_test_interaction_globals($wb1, $wb2, $wb3)
function do_test_test_interaction_globals($wb1, $wb2, $wb3,$word_parsing,$ttsWordApi)
{
?>
<script type="text/javascript">
LWT_DATA.language.dict_link1 = <?php echo json_encode($wb1); ?>;
LWT_DATA.language.dict_link2 = <?php echo json_encode($wb2); ?>;
LWT_DATA.language.translator_link = <?php echo json_encode($wb3); ?>;
LWT_DATA.language.word_parsing = <?php echo json_encode($word_parsing); ?>;
LWT_DATA.language.ttsVoiceApi = <?php echo json_encode($ttsWordApi); ?>;
LANG = getLangFromDict(LWT_DATA.language.translator_link);
if (LANG && LANG != LWT_DATA.language.translator_link) {
$("html").attr('lang', LANG);
Expand All @@ -715,32 +721,21 @@ function do_test_test_interaction_globals($wb1, $wb2, $wb3)
* @param int $testtype Type of test
* @param int $nosent 1 to use single word instead of sentence.
* @param string $save Word or sentence to use for the test
*
* @deprecated
* @return void
*/
function do_test_test_javascript_clickable($wo_record, $solution)
{
global $tbpref;
$wid = $wo_record['WoID'];
$abbr = getLanguageCode($wo_record['WoLgID'], LWT_LANGUAGES_ARRAY);
$phoneticText = phonetic_reading($wo_record['WoText'], $abbr);
$text = $wo_record['WoText'];
$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = " . $wo_record['WoLgID']
);
?>
<script type="text/javascript">
/**
* Read the word aloud
*/
function read_word() {
if (('speechSynthesis' in window) &&
document.getElementById('utterance-allowed').checked) {
const text = <?php echo json_encode($phoneticText); ?>;
const lang = <?php echo json_encode($abbr); ?>;
readRawTextAloud(text, lang);
}
}
<script type="text/javascript">

LWT_DATA.test.solution = <?php echo prepare_textdata_js($solution); ?>;
LWT_DATA.word.id = <?php echo $wid; ?>;
Expand All @@ -765,7 +760,8 @@ function read_word() {
* @param int $testtype Type of test
* @param int $nosent 1 to use single word instead of sentence.
* @param string $save Word or sentence to use for the test
*
*
* @deprecated use do_test_prepare_ajax_test_area
* @return void
*
* @global string $tbpref Database table prefix
Expand Down Expand Up @@ -903,9 +899,27 @@ function prepare_test_frames()
*
* @param {number} word_id Word ID
* @param {string} solution Test answer
* @param {string} word_text word text
* @param {string} word_lg_id word language id
* @param {string} group
*/
function insert_new_word(word_id, solution, group) {
function insert_new_word(word_id, word_text,word_lg_id,solution, group) {



/**
* Read the word aloud
*/
function read_word() {
if (('speechSynthesis' in window) &&
document.getElementById('utterance-allowed').checked) {
const text = word_text;
const lang_id = word_lg_id;
speechDispatcher(text, lang_id);
}
}



LWT_DATA.test.solution = solution;
LWT_DATA.word.id = word_id;
Expand All @@ -915,6 +929,8 @@ function insert_new_word(word_id, solution, group) {
$(document).on('keydown', keydown_event_do_test_test);
$('.word')
.on('click', word_click_event_do_test_test)
$('.word').on('click', read_word);

}

/**
Expand Down Expand Up @@ -947,7 +963,7 @@ function (tomorrow_test) {
);
} else {
insert_new_word(
current_test.word_id, current_test.solution, current_test.group
current_test.word_id,current_test.word_text,current_test.word_lg_abbr,current_test.solution, current_test.group
);
}
}
Expand Down
9 changes: 3 additions & 6 deletions do_text_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ function browser_tts($text, $languageName): void
WHERE LgName = " . convert_string_to_sqlsyntax($languageName)
);
$languageCode = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY);
// Phonetic reading for this text
$phoneticText = phonetic_reading($text, $languageCode);

$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = $lg_id"
Expand All @@ -199,7 +198,7 @@ function browser_tts($text, $languageName): void
/// Main object for text-to-speech interaction with SpeechSynthesisUtterance
const text_reader = {
/// The text to read
text: <?php echo json_encode($phoneticText); ?>,
text: <?php echo json_encode($text) ?>,

/// {string} ISO code for the language
lang: getLangFromDict(LWT_DATA.language.translator_link) || <?php echo json_encode($languageCode); ?>,
Expand All @@ -218,9 +217,7 @@ function init_reading() {
alert('Your browser does not support speechSynthesis!');
return;
}
readRawTextAloud(
text_reader.text, getLangFromDict(LWT_DATA.language.translator_link) || text_reader.lang
);
speechDispatcher(text_reader.text, getLangFromDict(LWT_DATA.language.translator_link) || text_reader.lang );
}

/** Start and stop the reading feature. */
Expand Down
3 changes: 2 additions & 1 deletion edit_word.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class="textarea-noreturn checklength checkoutsidebmp"
const TRANS_URI = <?php echo json_encode($trans_uri); ?>
const LANG_SHORT = <?php echo json_encode($lang_short); ?> ||
getLangFromDict(TRANS_URI);
const LANG_ID = <?php echo json_encode($lang); ?>

/**
* Sets the translation of a term.
Expand All @@ -420,7 +421,7 @@ class="textarea-noreturn checklength checkoutsidebmp"
*/
const autoRomanization = function () {
const term = $('#wordfield').val();
getPhoneticTextAsync(term, LANG_SHORT)
getPhoneticTextAsync(term, LANG_ID)
.then(function (phonetic) {
newword.WoRomanization.value = phonetic["phonetic_reading"];
});
Expand Down
4 changes: 2 additions & 2 deletions inc/ajax_get_phonetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* \file
* \brief Make the phonetic translation of a word.
*
* Call: inc/ajax_get_phonetic.php?text=[text_string]&lang=[language_string]
* Call: inc/ajax_get_phonetic.php?text=[text_string]&lang_id=[language_id]
*
* @package Lwt
* @author HugoFara <[email protected]>
Expand All @@ -16,7 +16,7 @@
require_once 'session_utility.php';

if (isset($_GET['text']) && isset($_GET['lang'])) {
echo phonetic_reading(getreq('text'), getreq('lang'));
echo phonetic_reading(getreq('text'), getreq('lang_id'));
}

?>
10 changes: 7 additions & 3 deletions inc/session_utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -3636,16 +3636,20 @@ function trim_value(&$value): void
* is supported, using MeCab.
*
* @param string $text Text to be converted
* @param string $lang Language code (usually BCP 47 or ISO 639-1)
* @param string $lang_id the language id
* @return string Parsed text in a phonetic format.
*
* @since 2.9.0 Any language starting by "ja" or "jp" is considered phonetic.
*/
function phonetic_reading($text, $lang)
function phonetic_reading($text, $lang_id)
{
global $tbpref;
// Many languages are already phonetic
if (!str_starts_with($lang, "ja") && !str_starts_with($lang, "jp")) {
$languageCode = getLanguageCode($lang_id, LWT_LANGUAGES_ARRAY);

$mecab = get_first_value('select LgRegexpWordCharacters as value from ' . $tbpref . 'languages where LgID = ' . $lang_id);

if (!str_starts_with($languageCode, "ja") && !str_starts_with($languageCode, "jp") && $mecab != "mecab") {
return $text;
}

Expand Down
6 changes: 3 additions & 3 deletions js/pgm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 24 additions & 24 deletions src/js/user_interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,24 @@ function saveAudioPosition (text_id, pos) {
* Get the phonetic version of a text.
*
* @param {string} text Text to convert to phonetics.
* @param {string} lang Language, either two letters code or four letters (BCP 47).
*
* @param {string} lang_id language id
*
* @deprecated Since 2.10.0 use getPhoneticTextAsync
*/
function getPhoneticText (text, lang) {
let phoneticText;
$.ajax(
'api.php/v1/phonetic-reading',
{
async: false,
data: {
text: text,
lang: lang
},
dataType: 'json',
type: 'GET'
}
)
function getPhoneticText(text, lang_id) {
let phoneticText;
$.ajax(
'api.php/v1/phonetic-reading',
{
async: false,
data: {
text: text,
lang_id: lang_id
},
dataType: 'json',
type: 'GET'
}
)
.done(
function (data) {
phoneticText = data.phonetic_reading;
Expand All @@ -172,16 +172,16 @@ function getPhoneticText (text, lang) {
* Get the phonetic version of a text, asynchronous.
*
* @param {string} text Text to convert to phonetics.
* @param {string} lang Language, either two letters code or four letters (BCP 47)
* @param {string} lang_id Language id
*/
async function getPhoneticTextAsync (text, lang) {
return $.getJSON(
'api.php/v1/phonetic-reading',
{
text: text,
lang: lang
}
);
return $.getJSON(
'api.php/v1/phonetic-reading',
{
text: text,
lang_id: lang_id
}
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Calls on GET', function() {
it('GET /phonetic-reading', function(done) {
supertest(host)
.get(api_path + '/phonetic-reading')
.query({text: 'test', lang: 'en'})
.query({text: 'test', lang_id: '1'})
.expect('Content-Type', 'application/json')
.expect(function(res) {
expect(res.body.phonetic_reading).equal('test');
Expand Down