~linuxgoose/linguistics-robin

ref: b6fd21664674d9f785a5bb71de9abf2aa8854ef3 linguistics-robin/pyphonetics/utils.py -rw-r--r-- 821 bytes
b6fd2166 — Ilias Koutsakis update pyproject.toml to include description and python version 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from itertools import groupby

from .exceptions import WrongLengthException, UnicodeException, \
    EmptyStringError


def translation(first, second):
    """Create an index of mapped letters (zip to dict)."""
    if len(first) != len(second):
        raise WrongLengthException('The lists are not of the same length!')
    return dict(zip(first, second))


def squeeze(word):
    """Squeeze the given sequence by dropping consecutive duplicates."""
    return ''.join(x[0] for x in groupby(word))


def check_str(word):
    """Throw exception at non-string input."""
    if not isinstance(word, str):
        raise UnicodeException('Expected a unicode string!')


def check_empty(word):
    """Throw exception at empty string input."""
    if not len(word):
        raise EmptyStringError('The given string is empty.')