Main parser of annotated data: Reader and writer of annotated data.
All the 3 types of annotated files are supported: ANNOT, MEASURE, TABLE.
Main parser of annotated data: Reader and writer of annotated data.
All the 3 types of annotated files are supported: ANNOT, MEASURE, TABLE.
Create a Transcription reader-writer.
def __init__(self, filename: str):
"""Create a Transcription reader-writer.
:param filename: (str)
"""
self.__filename = u(filename)
Return the whole list of supported extensions (case-sensitive).
@staticmethod
def extensions():
"""Return the whole list of supported extensions (case-sensitive)."""
return list(sppasTrsRW.TRANSCRIPTION_TYPES.keys())
Return the list of supported extensions if the reader exists.
@staticmethod
def extensions_in() -> list:
"""Return the list of supported extensions if the reader exists."""
e = list()
for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
fp = FileFormatProperty(extension=ext)
if fp.get_reader() is True:
e.append(ext)
return e
Return the list of supported extensions if the writer exists.
@staticmethod
def extensions_out() -> list:
"""Return the list of supported extensions if the writer exists."""
e = list()
for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
fp = FileFormatProperty(extension=ext)
if fp.get_writer() is True:
e.append(ext)
return e
Return the list of ANNOT extensions (case-sensitive).
@staticmethod
def annot_extensions() -> list:
"""Return the list of ANNOT extensions (case-sensitive)."""
e = list()
for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
fp = FileFormatProperty(extension=ext)
if fp.get_trs_type() == 'ANNOT':
e.append(ext)
return e
Return the list of MEASURE extensions (case-sensitive).
@staticmethod
def measure_extensions() -> list:
"""Return the list of MEASURE extensions (case-sensitive)."""
e = list()
for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
fp = FileFormatProperty(extension=ext)
if fp.get_trs_type() == 'MEASURE':
e.append(ext)
return e
Return the list of TABLE extensions (case-sensitive).
@staticmethod
def table_extensions() -> list:
"""Return the list of TABLE extensions (case-sensitive)."""
e = list()
for ext in list(sppasTrsRW.TRANSCRIPTION_TYPES.keys()):
fp = FileFormatProperty(extension=ext)
if fp.get_trs_type() == 'TABLE':
e.append(ext)
return e
Return the filename.
def get_filename(self) -> str:
"""Return the filename."""
return self.__filename
Set a new filename.
def set_filename(self, filename: str) -> None:
"""Set a new filename.
:param filename: (str)
"""
self.__filename = u(filename)
Read a transcription from a file.
def read(self, heuristic: bool=False) -> object:
"""Read a transcription from a file.
:param heuristic: (bool) if the extension of the file is unknown, use
a heuristic to detect the format, then to choose the reader-writer.
:return: sppasTranscription reader-writer
"""
try:
trs = sppasTrsRW.create_trs_from_extension(self.__filename)
except IOExtensionError:
if heuristic is True:
trs = sppasTrsRW.create_trs_from_heuristic(self.__filename)
else:
raise
if os.path.exists(self.__filename) is False:
raise AioError(self.__filename)
try:
fn = u(self.__filename)
trs.set_meta('file_reader', trs.__class__.__name__)
trs.set_meta('file_name', os.path.basename(fn))
trs.set_meta('file_path', os.path.dirname(fn))
trs.set_meta('file_ext', os.path.splitext(fn)[1])
trs.set_meta('file_read_date', sppasTime().now)
trs.read(self.__filename)
except UnicodeError as e:
raise AioEncodingError(filename=self.__filename, error_msg=str(e))
except IOError:
raise
except Exception:
raise
return trs
Return a transcription according to a given filename.
Only the extension of the filename is used.
@staticmethod
def create_trs_from_extension(filename: str) -> object:
"""Return a transcription according to a given filename.
Only the extension of the filename is used.
:param filename: (str) Name of the annotated file
:raises: IOExtensionError: un-supported extension
:return: (sppasTranscription)
"""
extension = os.path.splitext(filename)[1][1:]
logging.debug('Parse an annotated file: {:s}'.format(filename))
for ext in sppasTrsRW.TRANSCRIPTION_TYPES.keys():
if ext.lower() == extension.lower():
return sppasTrsRW.TRANSCRIPTION_TYPES[ext]()
raise IOExtensionError(filename)
Return a transcription according to a given filename.
The given file is opened and a heuristic allows to fix the format.
@staticmethod
def create_trs_from_heuristic(filename: str) -> object:
"""Return a transcription according to a given filename.
The given file is opened and a heuristic allows to fix the format.
:param filename: (str)
:return: (Transcription)
"""
for file_reader in sppasTrsRW.TRANSCRIPTION_TYPES.values():
try:
if file_reader.detect(filename) is True:
return file_reader()
except:
continue
return sppasRawText()
Write a transcription into a file.
def write(self, transcription):
"""Write a transcription into a file.
:param transcription: (sppasTranscription)
"""
trs_rw = sppasTrsRW.create_trs_from_extension(self.__filename)
trs_rw.set(transcription)
trs_rw.set_meta('file_writer', trs_rw.__class__.__name__)
trs_rw.set_meta('file_name', os.path.basename(self.__filename))
if trs_rw.is_meta_key('file_path') is False:
trs_rw.set_meta('file_path', os.path.dirname(self.__filename))
trs_rw.set_meta('file_ext', os.path.splitext(self.__filename)[1])
trs_rw.set_meta('file_write_date', '{:s}'.format(sppasTime().now))
file_version = int(trs_rw.get_meta('file_version', '0')) + 1
trs_rw.set_meta('file_version', str(file_version))
try:
trs_rw.write(self.__filename)
except UnicodeError as e:
raise AioEncodingError(self.__filename, str(e))
except Exception:
raise