talkTime
#12
by paiheng - opened
- handler.py +123 -3
- tests/test_talk_distribution.py +130 -0
handler.py
CHANGED
|
@@ -26,6 +26,32 @@ REASONING_MODEL = 'ddemszky/student-reasoning'
|
|
| 26 |
QUESTION_MODEL = 'ddemszky/question-detection'
|
| 27 |
FOCUSING_QUESTION_MODEL = 'ddemszky/focusing-questions'
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
class Utterance:
|
| 31 |
def __init__(self, speaker, text, uid=None,
|
|
@@ -33,6 +59,8 @@ class Utterance:
|
|
| 33 |
self.speaker = speaker
|
| 34 |
self.text = text
|
| 35 |
self.uid = uid
|
|
|
|
|
|
|
| 36 |
self.starttime = starttime
|
| 37 |
self.endtime = endtime
|
| 38 |
self.transcript = weakref.ref(transcript) if transcript else None
|
|
@@ -64,6 +92,12 @@ class Utterance:
|
|
| 64 |
def get_num_words(self):
|
| 65 |
return get_num_words(self.text)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def to_dict(self):
|
| 68 |
return {
|
| 69 |
'speaker': self.speaker,
|
|
@@ -102,8 +136,12 @@ class Utterance:
|
|
| 102 |
# On/off-task classification: a measure in its own right, alongside
|
| 103 |
# vocabulary. None means not classified (teacher utterance or empty text).
|
| 104 |
'onTask': None if self.on_task is None else self.on_task.get('label') == ON_TASK_LABEL,
|
|
|
|
|
|
|
|
|
|
| 105 |
'unitMeasure': self.unit_measure,
|
| 106 |
'aggregateUnitMeasure': self.aggregate_unit_measure,
|
|
|
|
| 107 |
'wordCount': self.word_count,
|
| 108 |
'numMathTerms': self.num_math_terms,
|
| 109 |
'mathTerms': self.math_terms,
|
|
@@ -148,7 +186,7 @@ class Transcript:
|
|
| 148 |
|
| 149 |
def get_talk_distribution_and_length(self, uptake_speaker):
|
| 150 |
if ((uptake_speaker is None)):
|
| 151 |
-
return None
|
| 152 |
teacher_words = 0
|
| 153 |
teacher_utt_count = 0
|
| 154 |
student_words = 0
|
|
@@ -162,7 +200,7 @@ class Transcript:
|
|
| 162 |
utt.role = 'student'
|
| 163 |
student_words += utt.get_num_words()
|
| 164 |
student_utt_count += 1
|
| 165 |
-
if teacher_words + student_words > 0:
|
| 166 |
teacher_percentage = round(
|
| 167 |
(teacher_words / (teacher_words + student_words)) * 100)
|
| 168 |
student_percentage = 100 - teacher_percentage
|
|
@@ -172,6 +210,81 @@ class Transcript:
|
|
| 172 |
avg_student_length = student_words / student_utt_count if student_utt_count > 0 else 0
|
| 173 |
return {'teacher': teacher_percentage, 'student': student_percentage}, {'teacher': avg_teacher_length, 'student': avg_student_length}
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
def get_vocabulary_table(self):
|
| 176 |
"""
|
| 177 |
Build a summary of matched vocabulary terms by utterance role.
|
|
@@ -511,7 +624,7 @@ class EndpointHandler():
|
|
| 511 |
|
| 512 |
transcript = Transcript(filename=params.pop("filename", None))
|
| 513 |
for utt in utterances:
|
| 514 |
-
transcript.add_utterance(Utterance(**utt))
|
| 515 |
|
| 516 |
print("Running inference on %d examples..." % transcript.length())
|
| 517 |
logging.set_verbosity_info()
|
|
@@ -557,6 +670,13 @@ class EndpointHandler():
|
|
| 557 |
talk_dist, talk_len = transcript.get_talk_distribution_and_length(uptake_speaker)
|
| 558 |
return_dict['talkDistribution'] = talk_dist
|
| 559 |
return_dict['talkLength'] = talk_len
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
talk_moments = transcript.get_talk_timeline()
|
| 561 |
return_dict['talkMoments'] = talk_moments
|
| 562 |
word_cloud, uptake_word_cloud, teacher_general_cloud, student_general_cloud = transcript.get_word_clouds()
|
|
|
|
| 26 |
QUESTION_MODEL = 'ddemszky/question-detection'
|
| 27 |
FOCUSING_QUESTION_MODEL = 'ddemszky/focusing-questions'
|
| 28 |
|
| 29 |
+
# Speaker label the diarizer gives to unattributed segments such as [crosstalk].
|
| 30 |
+
UNKNOWN_SPEAKER = 'unknown'
|
| 31 |
+
|
| 32 |
+
# Column names in the CSV transcript export, mapped to Utterance arguments.
|
| 33 |
+
CSV_FIELD_ALIASES = {
|
| 34 |
+
'speaker_uid': 'speaker',
|
| 35 |
+
'utterance_text': 'text',
|
| 36 |
+
'utterance_index': 'uid',
|
| 37 |
+
'start_time': 'starttime',
|
| 38 |
+
'end_time': 'endtime',
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def normalize_utterance_fields(utt):
|
| 43 |
+
normalized = dict(utt)
|
| 44 |
+
for alias, name in CSV_FIELD_ALIASES.items():
|
| 45 |
+
if alias in normalized:
|
| 46 |
+
normalized.setdefault(name, normalized.pop(alias))
|
| 47 |
+
return normalized
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def to_float(value):
|
| 51 |
+
if value is None or value == '':
|
| 52 |
+
return None
|
| 53 |
+
return float(value)
|
| 54 |
+
|
| 55 |
|
| 56 |
class Utterance:
|
| 57 |
def __init__(self, speaker, text, uid=None,
|
|
|
|
| 59 |
self.speaker = speaker
|
| 60 |
self.text = text
|
| 61 |
self.uid = uid
|
| 62 |
+
starttime = to_float(starttime)
|
| 63 |
+
endtime = to_float(endtime)
|
| 64 |
self.starttime = starttime
|
| 65 |
self.endtime = endtime
|
| 66 |
self.transcript = weakref.ref(transcript) if transcript else None
|
|
|
|
| 92 |
def get_num_words(self):
|
| 93 |
return get_num_words(self.text)
|
| 94 |
|
| 95 |
+
def get_duration(self):
|
| 96 |
+
"""Seconds spoken, or None if the utterance has no timestamps."""
|
| 97 |
+
if self.starttime is None or self.endtime is None:
|
| 98 |
+
return None
|
| 99 |
+
return max(0.0, self.endtime - self.starttime)
|
| 100 |
+
|
| 101 |
def to_dict(self):
|
| 102 |
return {
|
| 103 |
'speaker': self.speaker,
|
|
|
|
| 136 |
# On/off-task classification: a measure in its own right, alongside
|
| 137 |
# vocabulary. None means not classified (teacher utterance or empty text).
|
| 138 |
'onTask': None if self.on_task is None else self.on_task.get('label') == ON_TASK_LABEL,
|
| 139 |
+
# Seconds when every utterance has timestamps, word counts otherwise
|
| 140 |
+
# (see Transcript.calculate_aggregate_word_count). duration and
|
| 141 |
+
# wordCount always have a single unit.
|
| 142 |
'unitMeasure': self.unit_measure,
|
| 143 |
'aggregateUnitMeasure': self.aggregate_unit_measure,
|
| 144 |
+
'duration': None if self.get_duration() is None else round(self.get_duration(), 2),
|
| 145 |
'wordCount': self.word_count,
|
| 146 |
'numMathTerms': self.num_math_terms,
|
| 147 |
'mathTerms': self.math_terms,
|
|
|
|
| 186 |
|
| 187 |
def get_talk_distribution_and_length(self, uptake_speaker):
|
| 188 |
if ((uptake_speaker is None)):
|
| 189 |
+
return None, None
|
| 190 |
teacher_words = 0
|
| 191 |
teacher_utt_count = 0
|
| 192 |
student_words = 0
|
|
|
|
| 200 |
utt.role = 'student'
|
| 201 |
student_words += utt.get_num_words()
|
| 202 |
student_utt_count += 1
|
| 203 |
+
if teacher_words + student_words > 0:
|
| 204 |
teacher_percentage = round(
|
| 205 |
(teacher_words / (teacher_words + student_words)) * 100)
|
| 206 |
student_percentage = 100 - teacher_percentage
|
|
|
|
| 210 |
avg_student_length = student_words / student_utt_count if student_utt_count > 0 else 0
|
| 211 |
return {'teacher': teacher_percentage, 'student': student_percentage}, {'teacher': avg_teacher_length, 'student': avg_student_length}
|
| 212 |
|
| 213 |
+
def get_talk_time_stats(self, uptake_speaker):
|
| 214 |
+
"""
|
| 215 |
+
Talk measured in seconds (endtime - starttime). Utterances without
|
| 216 |
+
timestamps are skipped and reported as untimed. Returns None when no
|
| 217 |
+
utterance has timestamps.
|
| 218 |
+
|
| 219 |
+
Exact duplicate utterances (same speaker, times and text) are counted
|
| 220 |
+
once. The teacher/student figures are None when there is no teacher
|
| 221 |
+
speaker, and leave out the unknown speaker, which cannot be attributed
|
| 222 |
+
to either role; it is still listed in the per-speaker breakdown.
|
| 223 |
+
"""
|
| 224 |
+
timed = [utt for utt in self.utterances if utt.get_duration() is not None]
|
| 225 |
+
if not timed:
|
| 226 |
+
return None
|
| 227 |
+
totals = {'teacher': 0.0, 'student': 0.0}
|
| 228 |
+
counts = {'teacher': 0, 'student': 0}
|
| 229 |
+
by_speaker = {}
|
| 230 |
+
seen = set()
|
| 231 |
+
for utt in self.utterances:
|
| 232 |
+
duration = utt.get_duration()
|
| 233 |
+
if duration is not None:
|
| 234 |
+
key = (utt.speaker, utt.starttime, utt.endtime, utt.text)
|
| 235 |
+
if key in seen:
|
| 236 |
+
continue
|
| 237 |
+
seen.add(key)
|
| 238 |
+
if uptake_speaker is None:
|
| 239 |
+
role = None
|
| 240 |
+
else:
|
| 241 |
+
role = 'teacher' if utt.speaker == uptake_speaker else 'student'
|
| 242 |
+
speaker_stats = by_speaker.setdefault(utt.speaker, {
|
| 243 |
+
'role': role, 'seconds': 0.0, 'timedUtterances': 0,
|
| 244 |
+
'untimedUtterances': 0, 'confidences': []})
|
| 245 |
+
confidence = to_float(utt.props.get('speaker_id_confidence'))
|
| 246 |
+
if confidence is not None:
|
| 247 |
+
speaker_stats['confidences'].append(confidence)
|
| 248 |
+
if duration is None:
|
| 249 |
+
speaker_stats['untimedUtterances'] += 1
|
| 250 |
+
continue
|
| 251 |
+
speaker_stats['seconds'] += duration
|
| 252 |
+
speaker_stats['timedUtterances'] += 1
|
| 253 |
+
if role is None or (utt.speaker == UNKNOWN_SPEAKER and role == 'student'):
|
| 254 |
+
continue
|
| 255 |
+
totals[role] += duration
|
| 256 |
+
counts[role] += 1
|
| 257 |
+
|
| 258 |
+
for speaker_stats in by_speaker.values():
|
| 259 |
+
confidences = speaker_stats.pop('confidences')
|
| 260 |
+
speaker_stats['seconds'] = round(speaker_stats['seconds'], 2)
|
| 261 |
+
speaker_stats['avgConfidence'] = (
|
| 262 |
+
round(sum(confidences) / len(confidences), 4) if confidences else None)
|
| 263 |
+
session_start = min(utt.starttime for utt in timed)
|
| 264 |
+
session_end = max(utt.endtime for utt in timed)
|
| 265 |
+
stats = {
|
| 266 |
+
'distribution': None,
|
| 267 |
+
'length': None,
|
| 268 |
+
'total': None,
|
| 269 |
+
'sessionDuration': round(session_end - session_start, 2),
|
| 270 |
+
'untimedUtterances': len(self.utterances) - len(timed),
|
| 271 |
+
'bySpeaker': by_speaker,
|
| 272 |
+
}
|
| 273 |
+
if uptake_speaker is None:
|
| 274 |
+
return stats
|
| 275 |
+
|
| 276 |
+
talk = totals['teacher'] + totals['student']
|
| 277 |
+
if talk > 0:
|
| 278 |
+
teacher_percentage = round(totals['teacher'] / talk * 100)
|
| 279 |
+
student_percentage = 100 - teacher_percentage
|
| 280 |
+
else:
|
| 281 |
+
teacher_percentage = student_percentage = 0
|
| 282 |
+
stats['distribution'] = {'teacher': teacher_percentage, 'student': student_percentage}
|
| 283 |
+
stats['length'] = {role: round(totals[role] / counts[role], 2) if counts[role] > 0 else 0
|
| 284 |
+
for role in totals}
|
| 285 |
+
stats['total'] = {role: round(totals[role], 2) for role in totals}
|
| 286 |
+
return stats
|
| 287 |
+
|
| 288 |
def get_vocabulary_table(self):
|
| 289 |
"""
|
| 290 |
Build a summary of matched vocabulary terms by utterance role.
|
|
|
|
| 624 |
|
| 625 |
transcript = Transcript(filename=params.pop("filename", None))
|
| 626 |
for utt in utterances:
|
| 627 |
+
transcript.add_utterance(Utterance(**normalize_utterance_fields(utt)))
|
| 628 |
|
| 629 |
print("Running inference on %d examples..." % transcript.length())
|
| 630 |
logging.set_verbosity_info()
|
|
|
|
| 670 |
talk_dist, talk_len = transcript.get_talk_distribution_and_length(uptake_speaker)
|
| 671 |
return_dict['talkDistribution'] = talk_dist
|
| 672 |
return_dict['talkLength'] = talk_len
|
| 673 |
+
talk_time = transcript.get_talk_time_stats(uptake_speaker) or {}
|
| 674 |
+
return_dict['talkTimeDistribution'] = talk_time.get('distribution')
|
| 675 |
+
return_dict['talkTimeLength'] = talk_time.get('length')
|
| 676 |
+
return_dict['talkTimeTotal'] = talk_time.get('total')
|
| 677 |
+
return_dict['talkTimeBySpeaker'] = talk_time.get('bySpeaker')
|
| 678 |
+
return_dict['sessionDuration'] = talk_time.get('sessionDuration')
|
| 679 |
+
return_dict['talkTimeUntimedUtterances'] = talk_time.get('untimedUtterances')
|
| 680 |
talk_moments = transcript.get_talk_timeline()
|
| 681 |
return_dict['talkMoments'] = talk_moments
|
| 682 |
word_cloud, uptake_word_cloud, teacher_general_cloud, student_general_cloud = transcript.get_word_clouds()
|
tests/test_talk_distribution.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
| 7 |
+
if str(PROJECT_ROOT) not in sys.path:
|
| 8 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 9 |
+
|
| 10 |
+
from handler import Transcript, Utterance, normalize_utterance_fields
|
| 11 |
+
|
| 12 |
+
TEACHER = "teacher-uid"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def build_transcript(rows):
|
| 16 |
+
transcript = Transcript()
|
| 17 |
+
for row in rows:
|
| 18 |
+
transcript.add_utterance(Utterance(**normalize_utterance_fields(row)))
|
| 19 |
+
return transcript
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_word_distribution_unchanged():
|
| 23 |
+
transcript = build_transcript([
|
| 24 |
+
{"speaker": TEACHER, "text": "one two three", "starttime": 0.0, "endtime": 1.0},
|
| 25 |
+
{"speaker": "student-uid", "text": "four", "starttime": 1.0, "endtime": 4.0},
|
| 26 |
+
])
|
| 27 |
+
dist, length = transcript.get_talk_distribution_and_length(TEACHER)
|
| 28 |
+
assert dist == {"teacher": 75, "student": 25}
|
| 29 |
+
assert length == {"teacher": 3, "student": 1}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_no_uptake_speaker_keeps_speaker_level_time():
|
| 33 |
+
transcript = build_transcript([
|
| 34 |
+
{"speaker": TEACHER, "text": "hi", "starttime": 0.0, "endtime": 1.0},
|
| 35 |
+
{"speaker": "s1", "text": "hello", "starttime": 1.0, "endtime": 3.0},
|
| 36 |
+
])
|
| 37 |
+
assert transcript.get_talk_distribution_and_length(None) == (None, None)
|
| 38 |
+
stats = transcript.get_talk_time_stats(None)
|
| 39 |
+
assert stats["distribution"] is None
|
| 40 |
+
assert stats["length"] is None
|
| 41 |
+
assert stats["total"] is None
|
| 42 |
+
assert stats["sessionDuration"] == 3.0
|
| 43 |
+
assert stats["bySpeaker"]["s1"]["role"] is None
|
| 44 |
+
assert stats["bySpeaker"]["s1"]["seconds"] == 2.0
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_talk_time_uses_seconds():
|
| 48 |
+
transcript = build_transcript([
|
| 49 |
+
{"speaker": TEACHER, "text": "one two three four five six", "starttime": 10.0, "endtime": 13.0},
|
| 50 |
+
{"speaker": "s1", "text": "ok", "starttime": 13.0, "endtime": 14.0},
|
| 51 |
+
{"speaker": "s2", "text": "yes", "starttime": 14.0, "endtime": 17.0},
|
| 52 |
+
])
|
| 53 |
+
stats = transcript.get_talk_time_stats(TEACHER)
|
| 54 |
+
# 3s teacher vs 4s student, regardless of word counts
|
| 55 |
+
assert stats["distribution"] == {"teacher": 43, "student": 57}
|
| 56 |
+
assert stats["length"] == {"teacher": 3.0, "student": 2.0}
|
| 57 |
+
assert stats["total"] == {"teacher": 3.0, "student": 4.0}
|
| 58 |
+
assert stats["sessionDuration"] == 7.0
|
| 59 |
+
assert stats["untimedUtterances"] == 0
|
| 60 |
+
assert stats["bySpeaker"]["s2"] == {
|
| 61 |
+
"role": "student", "seconds": 3.0, "timedUtterances": 1,
|
| 62 |
+
"untimedUtterances": 0, "avgConfidence": None}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_untimed_utterances_skipped_and_counted():
|
| 66 |
+
transcript = build_transcript([
|
| 67 |
+
{"speaker": TEACHER, "text": "one", "starttime": 0.0, "endtime": 1.0},
|
| 68 |
+
{"speaker": "s1", "text": "two", "starttime": 1.0, "endtime": 4.0},
|
| 69 |
+
{"speaker": "s1", "text": "Yeah."},
|
| 70 |
+
{"speaker": "s1", "text": "Yeah."},
|
| 71 |
+
])
|
| 72 |
+
stats = transcript.get_talk_time_stats(TEACHER)
|
| 73 |
+
assert stats["distribution"] == {"teacher": 25, "student": 75}
|
| 74 |
+
assert stats["untimedUtterances"] == 2
|
| 75 |
+
# untimed repeats are not treated as duplicates
|
| 76 |
+
assert stats["bySpeaker"]["s1"]["timedUtterances"] == 1
|
| 77 |
+
assert stats["bySpeaker"]["s1"]["untimedUtterances"] == 2
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_talk_time_none_when_no_timestamps():
|
| 81 |
+
transcript = build_transcript([
|
| 82 |
+
{"speaker": TEACHER, "text": "one"},
|
| 83 |
+
{"speaker": "s1", "text": "two"},
|
| 84 |
+
])
|
| 85 |
+
assert transcript.get_talk_time_stats(TEACHER) is None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_unknown_speaker_excluded_from_combined_but_listed():
|
| 89 |
+
transcript = build_transcript([
|
| 90 |
+
{"speaker": TEACHER, "text": "hello", "starttime": 0.0, "endtime": 2.0},
|
| 91 |
+
{"speaker": "s1", "text": "hi", "starttime": 2.0, "endtime": 4.0},
|
| 92 |
+
{"speaker": "unknown", "text": "[crosstalk].", "starttime": 5.0, "endtime": 6.0},
|
| 93 |
+
])
|
| 94 |
+
stats = transcript.get_talk_time_stats(TEACHER)
|
| 95 |
+
assert stats["distribution"] == {"teacher": 50, "student": 50}
|
| 96 |
+
assert stats["total"] == {"teacher": 2.0, "student": 2.0}
|
| 97 |
+
assert stats["bySpeaker"]["unknown"]["seconds"] == 1.0
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_exact_duplicates_counted_once():
|
| 101 |
+
row = {"speaker": "s1", "text": "10 times 6.", "starttime": 447.25, "endtime": 448.0}
|
| 102 |
+
transcript = build_transcript([
|
| 103 |
+
{"speaker": TEACHER, "text": "what times what?", "starttime": 446.0, "endtime": 447.0},
|
| 104 |
+
row,
|
| 105 |
+
dict(row),
|
| 106 |
+
])
|
| 107 |
+
stats = transcript.get_talk_time_stats(TEACHER)
|
| 108 |
+
assert stats["total"]["student"] == 0.75
|
| 109 |
+
assert stats["bySpeaker"]["s1"]["timedUtterances"] == 1
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def test_csv_columns_are_mapped():
|
| 113 |
+
row = {
|
| 114 |
+
"utterance_index": "7",
|
| 115 |
+
"speaker_uid": TEACHER,
|
| 116 |
+
"start_time": "151.283",
|
| 117 |
+
"end_time": "151.646",
|
| 118 |
+
"utterance_text": "Thank you.",
|
| 119 |
+
"speaker_id_confidence": "0.95",
|
| 120 |
+
}
|
| 121 |
+
utt = Utterance(**normalize_utterance_fields(row))
|
| 122 |
+
assert utt.speaker == TEACHER
|
| 123 |
+
assert utt.text == "Thank you."
|
| 124 |
+
assert utt.uid == "7"
|
| 125 |
+
assert utt.get_duration() == pytest.approx(0.363)
|
| 126 |
+
assert utt.props == {"speaker_id_confidence": "0.95"}
|
| 127 |
+
|
| 128 |
+
transcript = Transcript()
|
| 129 |
+
transcript.add_utterance(utt)
|
| 130 |
+
assert transcript.get_talk_time_stats(TEACHER)["bySpeaker"][TEACHER]["avgConfidence"] == 0.95
|