Manage information of a resource for a language.
In most of the automatic annotations, we have to deal with language resources. Here, we store information about the type of resources, the path to get them, etc.
Manage information of a resource for a language.
In most of the automatic annotations, we have to deal with language resources. Here, we store information about the type of resources, the path to get them, etc.
Create a sppasLangResource instance.
def __init__(self):
"""Create a sppasLangResource instance.
"""
self._rtype = ''
self._rpath = ''
self._rname = ''
self._rext = ''
self._rlang = True
self.langlist = list()
self.lang = ''
self.langresource = ''
Set all members to their default value.
def reset(self):
"""Set all members to their default value."""
self._rtype = ''
self._rpath = ''
self._rname = ''
self._rext = ''
self._rlang = True
self.langlist = []
self.lang = ''
self.langresource = ''
Return False if the given resource is not representing a language.
def is_lang_resource(self):
"""Return False if the given resource is not representing a language."""
return self._rlang
Return the language name.
Language names in SPPAS are commonly represented in iso-639-3. It is a code that aims to define three-letter identifiers for all known human languages. "und" is representing an undetermined language. See http://www-01.sil.org/iso639-3/ for details...
def get_lang(self):
"""Return the language name.
Language names in SPPAS are commonly represented in iso-639-3.
It is a code that aims to define three-letter identifiers for all
known human languages. "und" is representing an undetermined language.
See <http://www-01.sil.org/iso639-3/> for details...
:returns: (str) Language code or an empty string if no lang was set.
"""
return self.lang
Return the list of available languages.
def get_langlist(self):
"""Return the list of available languages.
:returns: List of str
"""
return self.langlist
Return the resource name defined for the given language.
def get_langresource(self):
"""Return the resource name defined for the given language."""
if self._rlang is True:
if self.lang in self.langlist:
if len(self._rname) > 0:
return self.langresource + self.lang + self._rext
else:
return os.path.join(self.langresource, self.lang + self._rext)
return self.langresource
Return the language type.
def get_resourcetype(self):
"""Return the language type."""
return self._rtype
Return the language extension.
def get_resourceext(self):
"""Return the language extension."""
return self._rext
Set the type of the resource.
def set_type(self, resource_type):
"""Set the type of the resource.
:param resource_type: (str) One of "file" or "directory".
"""
resource_type = str(resource_type)
if resource_type not in sppasLangResource.RESOURCES_TYPES:
self.reset()
raise LangTypeError(resource_type)
self._rtype = resource_type
Fix the language resource path.
def set_path(self, resource_path):
"""Fix the language resource path.
:param resource_path: (str) Relative path to find the resource.
"""
resource_path = str(resource_path)
folder = os.path.join(paths.resources, resource_path)
if os.path.exists(folder) is False:
self.reset()
raise LangPathError(folder)
self._rpath = resource_path
Fix the language resource filename.
def set_filename(self, resource_filename):
"""Fix the language resource filename.
:param resource_filename: (str) Resource filename.
"""
self._rname = str(resource_filename)
Fix the language resource file extension.
def set_extension(self, resource_extension):
"""Fix the language resource file extension.
:param resource_extension: (str) Resource filename extension.
"""
self._rext = str(resource_extension)
Set resources then fix the list of available languages.
def set(self, rtype, rpath, rname='', rext='', rlang=True):
"""Set resources then fix the list of available languages.
:param rtype: (str) Resource type. One of: "file" or "directory"
:param rpath: (str) Resource path
:param rname: (str) Resource file name
:param rext: (str) Resource extension
:param rlang: (bool) Language-dependent resource
"""
self.reset()
self.set_type(rtype)
self.set_path(rpath)
self.set_filename(rname)
self.set_extension(rext)
self._rlang = rlang
directory = os.path.join(paths.resources, self._rpath)
if len(self._rname) > 0:
self.langresource = os.path.join(paths.resources, self._rpath, self._rname)
else:
self.langresource = directory
if rlang is True:
if rtype == 'file':
if len(rext) > 0:
sd = sppasDirUtils(directory)
for selectedfile in sd.get_files(self._rext):
filename, fext = os.path.splitext(selectedfile)
filename = os.path.basename(filename)
if filename.startswith(self._rname):
self.langlist.append(filename.replace(self._rname, ''))
else:
self._rext = ''
if len(self._rname) > 0:
for dirname in os.listdir(directory):
if dirname.startswith(rname) is True:
self.langlist.append(dirname.replace(self._rname, ''))
Set the language.
To reset the language, fix lang to None.
def set_lang(self, lang):
"""Set the language.
To reset the language, fix lang to None.
:param lang: (str) The language must be either UNDETERMINED
or one of the language of the list.
"""
if lang is None:
self.lang = ''
return
if lang.lower() != annots.UNDETERMINED and lang not in self.langlist:
raise LangNameError(lang)
self.lang = lang