Skip to content

Commit 4cb7394

Browse files
importation code factorization
1 parent e90a12e commit 4cb7394

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

ChildProject/annotations.py

+4-22
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AnnotationManager:
3131
IndexColumn(name = 'range_onset', description = 'covered range start time in milliseconds, measured since `time_seek`', regex = r"([0-9]+)", required = True),
3232
IndexColumn(name = 'range_offset', description = 'covered range end time in milliseconds, measured since `time_seek`', regex = r"([0-9]+)", required = True),
3333
IndexColumn(name = 'raw_filename', description = 'annotation input filename location, relative to `annotations/<set>/raw`', filename = True, required = True),
34-
IndexColumn(name = 'format', description = 'input annotation format', choices = ['TextGrid', 'eaf', 'vtc_rttm', 'vcm_rttm', 'alice', 'its', 'cha', 'NA'], required = False),
34+
IndexColumn(name = 'format', description = 'input annotation format', choices = converters.keys() + ['NA'], required = False),
3535
IndexColumn(name = 'filter', description = 'source file to filter in (for rttm and alice only)', required = False),
3636
IndexColumn(name = 'annotation_filename', description = 'output formatted annotation location, relative to `annotations/<set>/converted (automatic column, don\'t specify)', filename = True, required = False, generated = True),
3737
IndexColumn(name = 'imported_at', description = 'importation date (automatic column, don\'t specify)', datetime = "%Y-%m-%d %H:%M:%S", required = False, generated = True),
@@ -195,27 +195,9 @@ def _import_annotation(self, import_function: Callable[[str], pd.DataFrame], ann
195195
try:
196196
if callable(import_function):
197197
df = import_function(path)
198-
elif annotation_format == 'TextGrid':
199-
from .converters import TextGridConverter
200-
df = TextGridConverter.convert(path)
201-
elif annotation_format == 'eaf':
202-
from .converters import EafConverter
203-
df = EafConverter.convert(path)
204-
elif annotation_format == 'vtc_rttm':
205-
from .converters import VtcConverter
206-
df = VtcConverter.convert(path, source_file = filter)
207-
elif annotation_format == 'vcm_rttm':
208-
from .converters import VcmConverter
209-
df = VcmConverter.convert(path, source_file = filter)
210-
elif annotation_format == 'its':
211-
from .converters import ItsConverter
212-
df = ItsConverter.convert(path, recording_num = filter)
213-
elif annotation_format == 'alice':
214-
from .converters import AliceConverter
215-
df = AliceConverter.convert(path, source_file = filter)
216-
elif annotation_format == 'cha':
217-
from .converters import ChatConverter
218-
df = ChatConverter.convert(path)
198+
elif annotation_format in converters:
199+
converter = converters[annotation_format]
200+
df = converter.convert(path, filter)
219201
else:
220202
raise ValueError("file format '{}' unknown for '{}'".format(annotation_format, path))
221203
except:

ChildProject/converters.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def convert(filename: str, source_file: str = '') -> pd.DataFrame:
128128

129129
class AliceConverter(AnnotationConverter):
130130
FORMAT = 'alice'
131-
131+
132132
@staticmethod
133133
def convert(filename: str, source_file: str = '') -> pd.DataFrame:
134134
df = pd.read_csv(
@@ -287,10 +287,10 @@ def extract_from_regex(pattern, subject):
287287
return df
288288

289289
class TextGridConverter(AnnotationConverter):
290-
FORMAT = 'textgrid'
290+
FORMAT = 'TextGrid'
291291

292292
@staticmethod
293-
def convert(filename: str) -> pd.DataFrame:
293+
def convert(filename: str, filter = None) -> pd.DataFrame:
294294
import pympi
295295
textgrid = pympi.Praat.TextGrid(filename)
296296

@@ -330,7 +330,7 @@ class EafConverter(AnnotationConverter):
330330
FORMAT = 'eaf'
331331

332332
@staticmethod
333-
def convert(filename: str) -> pd.DataFrame:
333+
def convert(filename: str, filter = None) -> pd.DataFrame:
334334
import pympi
335335
eaf = pympi.Elan.Eaf(filename)
336336

@@ -463,7 +463,7 @@ def role_to_addressee(role):
463463
return ChatConverter.ADDRESSEE_TABLE[ChatConverter.SPEAKER_ROLE_TO_TYPE[role]]
464464

465465
@staticmethod
466-
def convert(filename: str) -> pd.DataFrame:
466+
def convert(filename: str, filter = None) -> pd.DataFrame:
467467

468468
import pylangacq
469469

0 commit comments

Comments
 (0)