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

Added CHAT file reader #174

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/senselab/utils/data_structures/talk_bank_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Functionality for interfacing with TalkBank data."""

from typing import Any, Dict, List

import pylangacq

from senselab.utils.data_structures.script_line import ScriptLine


def chats_to_script_lines(
path: str,
**kwargs: Any, # noqa: ANN401
) -> Dict[str, List[ScriptLine]]:
"""Connvert .cha files to script lines.

Converts .cha files to script lines using pylangacq's built-in read_chats functionality.

Args:
path (str): The path to a .cha file or a directory to recursively scan and use
**kwargs (Any): key-word arguments to be used with pylangacq's read_chats. For further info,
please refer to https://pylangacq.org/api.html#pylangacq.read_chat

Returns:
Dictionary mapping filepaths (e.g. individual .cha files) to a list of ScriptLines
"""
chats = pylangacq.read_chat(path, **kwargs)
script_lines_by_file: Dict[str, List[ScriptLine]] = {}
paths = chats.file_paths()
utterances = chats.utterances(by_files=True)
words = chats.words(by_files=True, by_utterances=True)
assert len(paths) == len(utterances) and len(utterances) == len(words)
for i in range(len(paths)):
path = paths[i]
script_lines_by_file[path] = []
utterances_in_file = utterances[i]
words_in_file = words[i]
assert len(words_in_file) == len(utterances_in_file)
for utt_idx, utterance in enumerate(utterances_in_file):
words_in_utterance = words_in_file[utt_idx]
if utterance.time_marks:
start = utterance.time_marks[0] / 1000
end = utterance.time_marks[1] / 1000
else:
start = None
end = None

utterance_transcript = " ".join(words_in_utterance[:-1]) + words_in_utterance[-1]
script_lines_by_file[path].append(
ScriptLine(text=utterance_transcript, speaker=utterance.participant, start=start, end=end)
)
return script_lines_by_file
Loading