Esempio n. 1
0
    def allomorph(self,
                  word,
                  morph,
                  tolerance_style=DEFAULT_TOLERANCE_STYLE,
                  guess_coda=DEFAULT_GUESS_CODA):
        """Determines one of allomorphic morphs based on a word.

        .. see also:: :meth:`allomorph`.

        """
        suffix = self.match(morph)
        if suffix is None:
            return None
        coda = guess_coda(word)
        if coda is not None:
            # Coda guessed successfully.
            morph = self.rule(coda)
        elif isinstance(tolerance_style, text_type):
            # User specified the style themselves
            morph = tolerance_style
        elif not suffix or not is_consonant(suffix[0]):
            # Choose the tolerant morph.
            morph = self.tolerance(tolerance_style)
        else:
            # Suffix starts with a consonant.  Generate a new tolerant morph
            # by combined morphs.
            morph1 = (combine_words(self.morph1, suffix)
                      if self.morph1 else suffix[1:])
            morph2 = (combine_words(self.morph2, suffix)
                      if self.morph2 else suffix[1:])
            tolerances = generate_tolerances(morph1, morph2)
            return get_tolerance_from_iterator(tolerances, tolerance_style)
        return combine_words(morph, suffix)
Esempio n. 2
0
def test_tolerance_style():
    assert Gwa[u'Hello'::OPTIONAL_MORPH2_AND_MORPH1] == u'(와)과'
    assert parse_tolerance_style(0) == MORPH1_AND_OPTIONAL_MORPH2
    assert parse_tolerance_style(u'을(를)') == MORPH1_AND_OPTIONAL_MORPH2
    assert parse_tolerance_style(u'(를)을') == OPTIONAL_MORPH2_AND_MORPH1
    with pytest.raises(ValueError):
        parse_tolerance_style(u'과')
    with pytest.raises(ValueError):
        parse_tolerance_style(u'이다')
    with pytest.raises(ValueError):
        parse_tolerance_style(u'(이)')
    assert get_tolerance([u'예제'], OPTIONAL_MORPH2_AND_MORPH1) == u'예제'
    assert get_tolerance_from_iterator(iter([u'예제']),
                                       OPTIONAL_MORPH2_AND_MORPH1) == u'예제'
Esempio n. 3
0
def test_tolerance_style():
    assert Gwa[u'Hello'::OPTIONAL_MORPH2_AND_MORPH1] == u'(와)과'
    assert parse_tolerance_style(0) == MORPH1_AND_OPTIONAL_MORPH2
    assert parse_tolerance_style(u'을(를)') == MORPH1_AND_OPTIONAL_MORPH2
    assert parse_tolerance_style(u'(를)을') == OPTIONAL_MORPH2_AND_MORPH1
    with pytest.raises(ValueError):
        parse_tolerance_style(u'과')
    with pytest.raises(ValueError):
        parse_tolerance_style(u'이다')
    with pytest.raises(ValueError):
        parse_tolerance_style(u'(이)')
    assert get_tolerance([u'예제'], OPTIONAL_MORPH2_AND_MORPH1) == u'예제'
    assert get_tolerance_from_iterator(iter([u'예제']),
                                       OPTIONAL_MORPH2_AND_MORPH1) == u'예제'