Skip to content

Commit

Permalink
web service changes
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhunt committed Aug 4, 2020
1 parent 6cebddd commit 8fd6bf7
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
111 changes: 111 additions & 0 deletions classes/external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* External.
*
* @package mod_poodlltime
* @author Justin Hunt - poodll.com
*/


use filter_poodll\poodlltools;
use filter_poodll\constants;
use filter_poodll\diff;

/**
* External class.
*
* @package mod_poodlltime
* @author Justin Hunt - poodll.com
*/
class filter_poodll_external extends external_api {

public static function check_by_phonetic_parameters(){
return new external_function_parameters(
array('spoken' => 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);
}
}
14 changes: 14 additions & 0 deletions classes/poodlltools.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
11 changes: 10 additions & 1 deletion db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
);

29 changes: 29 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Services definition.
*
* @package mod_poodlltime
* @author Justin Hunt - poodll.com
*/

$functions = array(


'filter_poodll_check_by_phonetic' => 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,
)
);
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)';

0 comments on commit 8fd6bf7

Please sign in to comment.