SPPAS 4.22

https://sppas.org/

Module sppas.src.structs

Class sppasLangResource

Description

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.

Constructor

Create a sppasLangResource instance.

View Source
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 = ''

Public functions

reset

Set all members to their default value.

View Source
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 = ''

is_lang_resource

Return False if the given resource is not representing a language.

View Source
def is_lang_resource(self):
    """Return False if the given resource is not representing a language."""
    return self._rlang

get_lang

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.
View Source
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

get_langlist

Return the list of available languages.

Returns
  • List of str
View Source
def get_langlist(self):
    """Return the list of available languages.

        :returns: List of str

        """
    return self.langlist

get_langresource

Return the resource name defined for the given language.

View Source
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

get_resourcetype

Return the language type.

View Source
def get_resourcetype(self):
    """Return the language type."""
    return self._rtype

get_resourceext

Return the language extension.

View Source
def get_resourceext(self):
    """Return the language extension."""
    return self._rext

set_type

Set the type of the resource.

Parameters
  • resource_type: (str) One of "file" or "directory".
View Source
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

set_path

Fix the language resource path.

Parameters
  • resource_path: (str) Relative path to find the resource.
View Source
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

set_filename

Fix the language resource filename.

Parameters
  • resource_filename: (str) Resource filename.
View Source
def set_filename(self, resource_filename):
    """Fix the language resource filename.

        :param resource_filename: (str) Resource filename.

        """
    self._rname = str(resource_filename)

set_extension

Fix the language resource file extension.

Parameters
  • resource_extension: (str) Resource filename extension.
View Source
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

Set resources then fix the list of available languages.

Parameters
  • rtype: (str) Resource type. One of: "file" or "directory"
  • rpath: (str) Resource path
  • rname: (str) Resource file name
  • rext: (str) Resource extension
  • rlang: (bool) Language-dependent resource
View Source
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_lang

Set the language.

To reset the language, fix lang to None.

Parameters
  • lang: (str) The language must be either UNDETERMINED or one of the language of the list.
View Source
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