Ejemplo n.º 1
0
def word_similarity(first_word, second_word, start_phone=None, end_phone=None):
    first_phones = possible_phones(first_word)
    second_phones = possible_phones(second_word)

    if not first_phones or not second_phones:
        return 0

    first_phones = first_phones[0]
    second_phones = second_phones[0]

    first_range = first_phones[start_phone:end_phone]
    second_range = second_phones[start_phone:end_phone]

    first_range = first_range[::-1]
    second_range = second_range[::-1]

    if len(first_range) > len(second_range):
        first_range, second_range = second_range, first_range

    hits = 0
    total = len(first_range)

    for idx, phone in enumerate(first_range):
        other_phone = second_range[idx]

        if phone == other_phone:
            hits += 1

            # Phones with emphasis are better matches, weight them more
            if phone[-1].isdigit():
                hits += 1
                total += 1

    return hits / total
Ejemplo n.º 2
0
def syllables(word):
    phones_list = possible_phones(word)

    if not phones_list:
        return []

    phones = phones_list[0]

    syllables = []
    syllable = ""

    for phone in phones:
        if phone[-1].isdigit():
            syllable += phone[:-1].lower()
            syllables.append(syllable)
            syllable = ""
        else:
            syllable += phone.lower()

    if syllable:
        syllables.append(syllable)

    return syllables
Ejemplo n.º 3
0
def test_possible_phones_multiple():
    phones = utils.possible_phones("advantage")

    assert len(phones) == 4
Ejemplo n.º 4
0
def test_possible_phones_single():
    phones = utils.possible_phones("short")

    assert len(phones) == 1
Ejemplo n.º 5
0
def test_possible_phones_none():
    phones = utils.possible_phones("invalidword")

    assert len(phones) == 0