Exemplo n.º 1
0
 def __init__(self, labels):
     if not isinstance(labels, list):
         raise TypeError("Parameter labels must be a list of Unicode strings")
     if len(labels) < 1:
         raise ValueError("Parameter labels must contain at least one string")
     for l in labels:
         if not is_unicode_string(l):
             raise TypeError("Parameter labels must be a list of Unicode strings")
     self.__labels = labels
Exemplo n.º 2
0
 def __init__(self, labels):
     if not isinstance(labels, list):
         raise TypeError(
             "Parameter labels must be a list of Unicode strings")
     if len(labels) < 1:
         raise ValueError(
             "Parameter labels must contain at least one string")
     for l in labels:
         if not is_unicode_string(l):
             raise TypeError(
                 "Parameter labels must be a list of Unicode strings")
     self.__labels = labels
Exemplo n.º 3
0
 def __init__(self, ipa_chars=None, unicode_string=None, ignore=False, single_char_parsing=False):
     self.ipa_chars = []
     if ipa_chars is not None:
         self.ipa_chars = ipa_chars
     elif unicode_string is not None:
         if not is_unicode_string(unicode_string):
             raise ValueError("The given string is not a Unicode string.")
         if (not ignore) and (not is_valid_ipa(unicode_string)):
             raise ValueError("The given string contains characters not IPA valid. Use the 'ignore' option to ignore them.")
         substrings = remove_invalid_ipa_characters(
             unicode_string=unicode_string,
             return_invalid=False,
             single_char_parsing=single_char_parsing
         )
         self.ipa_chars = [UNICODE_TO_IPA[substring] for substring in substrings]
Exemplo n.º 4
0
    def is_equivalent(self, other, ignore=False):
        """
        Return ``True`` if the IPA string is equivalent to the ``other`` object.

        The ``other`` object can be:

        1. a Unicode string,
        2. a list of IPAChar objects, and
        3. another IPAString.

        :param variant other: the object to be compared against
        :param bool ignore: if other is a Unicode string, ignore Unicode characters not IPA valid
        :rtype: bool
        """
        def is_equivalent_to_list_of_ipachars(other):
            """
            Return ``True`` if the list of IPAChar objects
            in the canonical representation of the string
            is the same as the given list.

            :param list other: list of IPAChar objects
            :rtype: bool
            """
            my_ipa_chars = self.canonical_representation.ipa_chars
            if len(my_ipa_chars) != len(other):
                return False
            for i in range(len(my_ipa_chars)):
                if not my_ipa_chars[i].is_equivalent(other[i]):
                    return False
            return True

        if is_unicode_string(other):
            try:
                return is_equivalent_to_list_of_ipachars(
                    IPAString(unicode_string=other, ignore=ignore).ipa_chars)
            except:
                return False
        if is_list_of_ipachars(other):
            try:
                return is_equivalent_to_list_of_ipachars(other)
            except:
                return False
        if isinstance(other, IPAString):
            return is_equivalent_to_list_of_ipachars(
                other.canonical_representation.ipa_chars)
        return False
Exemplo n.º 5
0
def variant_to_list(obj):
    """
    Return a list containing the descriptors in the given object.

    The ``obj`` can be a list or a set of descriptor strings, or a Unicode string.
    
    If ``obj`` is a Unicode string, it will be split using spaces as delimiters.

    :param variant obj: the object to be parsed
    :rtype: list 
    :raise TypeError: if the ``obj`` has a type not listed above
    """
    if isinstance(obj, list):
        return obj
    elif is_unicode_string(obj):
        return [s for s in obj.split() if len(s) > 0]
    elif isinstance(obj, set) or isinstance(obj, frozenset):
        return list(obj)
    raise TypeError(
        "The given value must be a list or a set of descriptor strings, or a Unicode string."
    )
Exemplo n.º 6
0
 def test_is_unicode_string(self):
     values = [
         (None, None),
         (u"", True),
         (b"", False),
         (u"foo", True),
         (b"foo", False),
     ]
     self.do_test(values, is_unicode_string)
     if PY2:
         self.assertFalse(is_unicode_string("foo"))
         self.assertTrue(is_unicode_string(u"foo"))
         self.assertFalse(is_unicode_string(b"foo"))
     else:
         self.assertTrue(is_unicode_string("foo"))
         self.assertTrue(is_unicode_string(u"foo"))
         self.assertFalse(is_unicode_string(b"foo"))
Exemplo n.º 7
0
    def is_equivalent(self, other):
        """
        Return ``True`` if the IPA character is equivalent to the ``other`` object.

        The ``other`` object can be:

        1. a Unicode string, containing the representation of the IPA character,
        2. a Unicode string, containing a space-separated list of descriptors,
        3. a list of Unicode strings, containing descriptors, and
        4. another IPAChar.

        :rtype: bool
        """
        if (self.unicode_repr is not None) and (is_unicode_string(other)) and (
                self.unicode_repr == other):
            return True
        if isinstance(other, IPAChar):
            return self.canonical_representation == other.canonical_representation
        try:
            return self.canonical_representation == IPAChar(
                name=None, descriptors=other).canonical_representation
        except:
            return False
Exemplo n.º 8
0
 def _check_value(self, value):
     if not is_unicode_string(value):
         raise TypeError("The given value must be a Unicode string")