Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 9x 9x 9x 9x 9x 9x 794x 794x 794x 9x 9x 9x 140x 140x 140x 280x 280x 15x 15x 15x 12x 12x 15x 15x 15x 140x 140x 140x 140x 140x 140x 9x 9x 9x 9x 9x 9x 9x 9x 9x 132x 132x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 132x 9x 9x 9x 9x 9x 132x 132x 132x 132x 1x 1x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 9x 9x 1032x 1032x 1032x 70x 1032x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 344x 344x 344x 344x 344x 321x 7253x 7253x 7253x 7253x 7253x 7253x 7253x 7253x 321x 344x 344x 344x 344x 344x 344x 344x 344x 344x 344x 9x 9x 55x 55x 55x 9x 9x 9x 60x 60x 60x 60x 2x 2x 60x 60x 60x 9x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 9x 17x 17x 17x 5x 5x 5x 5x 5x 5x 5x 5x 5x 17x 17x 9x 9x 9x 11x 11x 11x 8x 5x 1x 11x 9x 9x 7x 7x 7x 7x 7x 7x 7x 7x 7x | // Result parsing. Evaluate and compat answer with the bare result JSON (no envelope); TTS and report
// use {code: 0, data: {...}} (DESIGN 2.8).
import { WARNING_TABLE } from './error-table.js';
import { fromCode, ProtocolViolationException } from './errors.js';
import { toInt, toNumberOrNull, headerValue } from './util.js';
function nn(v) {
return v === undefined ? null : v;
}
/** Merges `result.warning` and top level `warnings`. Entries are codes or {code, message} objects. */
export function extractWarnings(resp) {
const out = [];
const seen = {};
const push = (list) => {
if (!Array.isArray(list)) return;
for (let i = 0; i < list.length; i++) {
const w = list[i];
const code = toInt(w && typeof w === 'object' ? w.code : w);
if (!code || seen[code]) continue;
seen[code] = true;
const entry = WARNING_TABLE[code];
const message = w && typeof w === 'object' && w.message ? String(w.message) : entry ? entry.message : 'warning ' + code;
out.push({ code, message });
}
};
const result = resp && resp.result;
push(result && result.warning);
push(resp && resp.warnings);
return out;
}
function objOrNull(v) {
return v && typeof v === 'object' && !Array.isArray(v) ? v : null;
}
/**
* Connected speech mode (coreType connected): the total is result.connected_overall and the result
* has no `overall`. Returns null for every other mode.
*/
function connectedScores(res) {
if (res.connected_overall === undefined) return null;
return {
overall: toNumberOrNull(res.connected_overall),
linking: toNumberOrNull(res.linking),
rhythm: toNumberOrNull(res.rhythm),
elision: toNumberOrNull(res.elision),
reduction: toNumberOrNull(res.reduction),
nBoundaries: toNumberOrNull(res.n_boundaries),
boundaries: Array.isArray(res.boundaries) ? res.boundaries : [],
coverage: objOrNull(res.coverage),
metrics: objOrNull(res.raw),
};
}
/**
* Open question mode (coreType open): content, languageUse and delivery sub scores, transcript,
* feedback and the task audit. Returns null for every other mode.
*/
function openScores(res) {
const isOpen = res._coreType === 'open' || res.content !== undefined || res.delivery !== undefined ||
res.languageUse !== undefined || res.openTaskAudit !== undefined;
if (!isOpen) return null;
return {
taskType: nn(res.taskType),
transcript: typeof res.transcript === 'string' ? res.transcript : null,
hasSpeech: typeof res.hasSpeech === 'boolean' ? res.hasSpeech : null,
durationSec: toNumberOrNull(res.duration_s),
content: objOrNull(res.content),
languageUse: objOrNull(res.languageUse),
delivery: objOrNull(res.delivery),
feedback: objOrNull(res.feedback),
openTaskAudit: objOrNull(res.openTaskAudit),
audioQuality: objOrNull(res.audioQuality),
aggregation: objOrNull(res.aggregation),
rubricVersion: nn(res.rubricVersion),
};
}
/**
* Normalized EvalResult. Score fields per mode:
* - word, sentence, passage, alpha, pinyin: result.overall, pronunciation (accuracy), fluency,
* integrity, rhythm, tone (Chinese), rear_tone, speed, words[], sentences[] (passage sentences carry
* details[] when paragraphNeedWordScore is 1);
* - connected: no result.overall; `overall` falls back to result.connected_overall and `connected`
* holds linking, rhythm, elision, reduction, n_boundaries and boundaries[];
* - open: result.overall plus `open` with content, languageUse, delivery, transcript, feedback,
* openTaskAudit.
* `raw` keeps the whole platform response.
*/
export function parseEvalResult(resp) {
const r = resp && typeof resp === 'object' ? resp : {};
const res = r.result && typeof r.result === 'object' ? r.result : {};
const pronunciation = toNumberOrNull(res.pronunciation);
const accuracy = toNumberOrNull(res.accuracy);
const connected = connectedScores(res);
let overall = toNumberOrNull(res.overall);
if (overall === null && connected) overall = connected.overall;
return {
recordId: r.recordId !== undefined && r.recordId !== null ? String(r.recordId) : null,
eof: r.eof !== undefined ? toInt(r.eof) : null,
coreType: typeof res._coreType === 'string' ? res._coreType : null,
language: typeof res._language === 'string' ? res._language : null,
overall,
dims: {
integrity: toNumberOrNull(res.integrity),
pronunciation,
accuracy: accuracy !== null ? accuracy : pronunciation,
fluency: toNumberOrNull(res.fluency),
tone: toNumberOrNull(res.tone),
rhythm: toNumberOrNull(res.rhythm),
emotion: toNumberOrNull(res.emotion),
},
speed: toNumberOrNull(res.speed),
rearTone: nn(res.rear_tone),
duration: nn(res.duration),
durationSec: toNumberOrNull(res.numeric_duration !== undefined ? res.numeric_duration : res.duration),
words: Array.isArray(res.words) ? res.words : [],
sentences: Array.isArray(res.sentences) ? res.sentences : [],
connected,
open: openScores(res),
asrText: nn(r.asrText),
report: nn(r.report),
standardAudio: nn(r.standardAudio),
yuguScores: nn(res.yuguScores),
warnings: extractWarnings(r),
localWarnings: [],
idempotencyKey: null,
replayed: r.replayed === true,
attempts: 0,
raw: r,
};
}
// A count of a progress frame: the integer, or -1 when the frame has none (CONTRACT 5.5).
function countOrAbsent(v) {
if (typeof v === 'number' && isFinite(v)) return Math.trunc(v);
if (typeof v === 'string' && /^\s*-?\d+\s*$/.test(v)) return parseInt(v, 10);
return -1;
}
/**
* onPartial model of a partial frame {"eof":0,"result":{...}} of either realtime channel (CONTRACT
* 5.5). `bytes` and `raw` (the whole frame) keep their 2.0.0 meaning. Reading progress: result.progress
* lists every unit of the reference text as {word, read, matched}; readUnits, matchedUnits and
* totalUnits are the platform's counts. A frame without progress (realtime feedback off, audio that is
* not 16 kHz 16 bit mono PCM, tracking not running) gives -1 counts, an empty progress list and
* hasProgress false. hasProgress is true when the frame has a result.progress array.
* The Shengtong style result.words of compat frames (only the units read since the previous frame)
* is never used for the per-unit state; it stays available in `raw`.
*/
export function parsePartialResult(frame) {
const f = frame && typeof frame === 'object' ? frame : {};
const res = f.result && typeof f.result === 'object' && !Array.isArray(f.result) ? f.result : {};
const hasProgress = Array.isArray(res.progress);
const progress = [];
if (hasProgress) {
for (let i = 0; i < res.progress.length; i++) {
// Every item is kept, so indexes stay aligned with the reference text even for a damaged item.
const w = res.progress[i] && typeof res.progress[i] === 'object' ? res.progress[i] : {};
progress.push({
word: w.word === undefined || w.word === null ? '' : String(w.word),
read: w.read === true,
matched: w.matched === true,
});
}
}
return {
bytes: toInt(res.bytes),
readUnits: countOrAbsent(res.readUnits),
matchedUnits: countOrAbsent(res.matchedUnits),
totalUnits: countOrAbsent(res.totalUnits),
progress,
hasProgress,
raw: f,
};
}
export function isReplayedHeader(headers) {
const v = headerValue(headers, 'Idempotency-Replayed');
return typeof v === 'string' && v.trim().toLowerCase() === 'true';
}
/** JSON body of a 2xx response; a body that is not JSON is a protocol violation (90005). */
export function parseJsonBody(text, init) {
try {
const v = JSON.parse(text);
if (v && typeof v === 'object') return v;
} catch (e) {
// handled below
}
const o = init || {};
return failProtocol('response body is not a JSON object', text, o);
}
function failProtocol(message, text, o) {
throw new ProtocolViolationException(message, {
code: 90005,
rawBody: typeof text === 'string' ? text : null,
httpStatus: o.httpStatus,
idempotencyKey: o.idempotencyKey,
});
}
/** Unwraps {code: 0, data} envelopes; a non zero code becomes the typed error of that code. */
export function unwrapEnvelope(json, init) {
const o = init || {};
const code = toInt(json.code);
if (code !== 0) {
// An envelope is a server error body: its code is looked up in the platform error table only.
throw fromCode(code, {
namespace: 'errors',
message: json.message ? String(json.message) : undefined,
httpStatus: o.httpStatus,
idempotencyKey: o.idempotencyKey,
rawBody: o.rawBody,
});
}
return json.data && typeof json.data === 'object' ? json.data : {};
}
/** Absolute URL of a TTS audio file. Paths under /audio/ are served at <base>/tts/audio/... (CONTRACT 2.1). */
export function resolveAudioUrl(baseUrl, audioUrl) {
const u = String(audioUrl || '');
if (!u) return '';
if (/^https?:\/\//i.test(u)) return u;
if (u.indexOf('/audio/') === 0) return baseUrl + '/tts' + u;
if (u.charAt(0) === '/') return baseUrl + u;
return u;
}
export function parseTtsData(data, baseUrl) {
const audioUrl = data.audioUrl ? String(data.audioUrl) : '';
return {
audioUrl,
fullUrl: resolveAudioUrl(baseUrl, audioUrl),
duration: toNumberOrNull(data.duration),
format: data.format ? String(data.format) : null,
warnings: extractWarnings({ warnings: data.warnings }),
};
}
|