diff --git a/classes/external.php b/classes/external.php new file mode 100644 index 00000000..b4e77af6 --- /dev/null +++ b/classes/external.php @@ -0,0 +1,111 @@ + new external_value(PARAM_TEXT, 'The spoken phrase'), + 'correct' => new external_value(PARAM_TEXT, 'The correct phrase'), + 'language' => new external_value(PARAM_TEXT, 'The language eg en-US') + ) + ); + } + + public static function check_by_phonetic($spoken, $correct, $language){ + $language = substr($language,0,2); + $spokenphonetic = poodlltools::convert_to_phonetic($spoken,$language); + $correctphonetic = poodlltools::convert_to_phonetic($correct,$language); + $similar_percent = 0; + $similar_chars = similar_text($correctphonetic,$spokenphonetic,$similar_percent); + return round($similar_percent,0); + } + + public static function check_by_phonetic_returns(){ + return new external_value(PARAM_INT,'how close is spoken to correct, 0 - 100'); + } + + public static function compare_passage_to_transcript_parameters(){ + return new external_function_parameters( + array('transcript' => new external_value(PARAM_TEXT, 'The spoken phrase'), + 'passage' => new external_value(PARAM_TEXT, 'The correct phrase'), + 'language' => new external_value(PARAM_TEXT, 'The language eg en-US'), + 'alternatives' => new external_value(PARAM_TEXT, 'list of alternatives',false,'') + ) + ); + } + + public static function compare_passage_to_transcript($language,$passage,$transcript, $alternatives) { + + //turn the passage and transcript into an array of words + $passagebits = diff::fetchWordArray($passage); + $alternatives = diff::fetchAlternativesArray($alternatives); + $transcriptbits = diff::fetchWordArray($transcript); + $wildcards = diff::fetchWildcardsArray($alternatives); + + //fetch sequences of transcript/passage matched words + // then prepare an array of "differences" + $passagecount = count($passagebits); + $transcriptcount = count($transcriptbits); + $sequences = diff::fetchSequences($passagebits, $transcriptbits, $alternatives, $language); + //fetch diffs + $debug=false; + $diffs = diff::fetchDiffs($sequences, $passagecount, $transcriptcount, $debug); + $diffs = diff::applyWildcards($diffs, $passagebits, $wildcards); + + + //from the array of differences build error data, match data, markers, scores and metrics + $errors = new \stdClass(); + $currentword = 0; + + //loop through diffs + $results=[]; + foreach ($diffs as $diff) { + $currentword++; + $result = new \stdClass(); + $result->word = $passagebits[$currentword - 1]; + $result->wordnumber = $currentword; + switch ($diff[0]) { + case Diff::UNMATCHED: + //we collect error info so we can count and display them on passage + + $result->matched =false; + break; + + case Diff::MATCHED: + $result->matched =true; + break; + + default: + //do nothing + //should never get here + } + $results[]=$result; + } + + //finalise and serialise session errors + $sessionresults = json_encode($results); + + return $sessionresults; + } + + public static function compare_passage_to_transcript_returns() { + return new external_value(PARAM_RAW); + } +} diff --git a/classes/poodlltools.php b/classes/poodlltools.php index 5dfc73d4..12a8ddaa 100644 --- a/classes/poodlltools.php +++ b/classes/poodlltools.php @@ -585,6 +585,20 @@ public static function fetchJSWidgetJSON($widget, $params, $width, $height, $bgc } + //convert a phrase or word to a series of phonetic characters that we can use to compare text/spoken + public static function convert_to_phonetic($phrase,$language){ + + switch($language){ + case 'en': + $phonetic = metaphone($phrase); + break; + case 'ja': + default: + $phonetic = $phrase; + } + return $phonetic; + } + //We check if the OS version is too old here, //Android 4+ iOS6+ diff --git a/db/access.php b/db/access.php index a84c6037..f718c19c 100644 --- a/db/access.php +++ b/db/access.php @@ -42,6 +42,15 @@ 'editingteacher' => CAP_ALLOW, 'manager' => CAP_ALLOW ) - ) + ), + 'filter/poodll:comparetext' => array( + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'archetypes' => array( + 'user' => CAP_ALLOW, + 'guest' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'mod/label:view' + ), ); diff --git a/db/services.php b/db/services.php new file mode 100644 index 00000000..41d963f3 --- /dev/null +++ b/db/services.php @@ -0,0 +1,29 @@ + array( + 'classname' => 'filter_poodll_external', + 'methodname' => 'check_by_phonetic', + 'description' => 'compares a spoken phrase to a correct phrase by phoneme' , + 'capabilities'=> 'filter/poodll:comparetext', + 'type' => 'read', + 'ajax' => true, + ), + + 'filter_poodll_compare_passage_to_transcript' => array( + 'classname' => 'filter_poodll_external', + 'methodname' => 'compare_passage_to_transcript', + 'description' => 'compares a spoken phrase to a correct phrase' , + 'capabilities'=> 'filter/poodll:comparetext', + 'type' => 'read', + 'ajax' => true, + ) +); \ No newline at end of file diff --git a/version.php b/version.php index cf5a63df..a67ead93 100644 --- a/version.php +++ b/version.php @@ -25,9 +25,9 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020062400; +$plugin->version = 2020071000; $plugin->requires = 2016052300;//moodle 3.1.0 $plugin->component = 'filter_poodll'; $plugin->maturity = MATURITY_STABLE; -$plugin->release = '3.1.35 (Build 2020062400)'; +$plugin->release = '3.1.36 (Build 2020071000)';