o
    mi[                  	   @   s  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlZde
e dee fdd	Zd
eeee f dee fddZd
eeee f defddZd
eeee f dede	eee  ee f fddZdedefddZdede	edf fddZdS )z-Provides utility functions for the phonemizer    N)Number)Path)UnionListTupleIterableiterablereturnc                 C   s(   g }d}| D ]}||7 }| | q|S )z6Returns the cumulative sum of the `iterable` as a listr   )append)r   resZ
cumulativevalue r   J/home/kim/smarthome/.venv/lib/python3.10/site-packages/phonemizer/utils.pycumsum   s   r   textc                 C   s"   t | tr| tjtjS | S )z8Returns the string `text` as a list of lines, split by 
)
isinstancestrstriposlinesepsplitr   r   r   r   str2list#   s   
r   c                 C   s   t | tr| S tj| S )zBReturns the list of lines `text` as a single string separated by 
)r   r   r   r   joinr   r   r   r   list2str*   s   
r   numc                    s   t ttdt|  t|t} fddt|d D }|d   d }|r5|| dgtdd |dd D  }||fS )	a  Return a maximum of `num` equally sized chunks of a `text`

    This method is usefull when phonemizing a single text on multiple jobs.

    The exact number of chunks returned is `m = min(num, len(str2list(text)))`.
    Only the m-1 first chunks have equal size. The last chunk can be longer.
    The input `text` can be a list or a string. Return a list of `m` strings.

    Parameters
    ----------
    text (str or list) : The text to divide in chunks

    num (int) : The number of chunks to build, must be a strictly positive
    integer.

    Returns
    -------
    chunks (list of list of str) : The chunked text with utterances separated
        by '
'.

    offsets (list of int) : offset used below to recover the line numbers in
        the input text wrt the chunks

       c                    s$   g | ]}|  |d     qS )r   r   ).0isizer   r   r   
<listcomp>O   s    zchunks.<locals>.<listcomp>Nr   c                 s       | ]}t |V  qd S N)len)r   cr   r   r   	<genexpr>V       zchunks.<locals>.<genexpr>)r   intmaxr$   minranger
   r   )r   r   ZnchunksZtext_chunkslastoffsetsr   r   r   chunks1   s   

 r/   pathc                 C   s   zt jdd |  } W n$ ty0   t jdd}||  } W d   n1 s)w   Y  Y nw |  s<td|  |  S )a;  Returns the absolute path to a phonemizer resource file or directory

    The packages resource are stored within the source tree in the
    'phonemizer/share' directory and, once the package is installed, are moved
    to another system directory (e.g. /share/phonemizer).

    Parameters
    ----------
    path (str) : the file or directory to get, must be relative to
        'phonemizer/share'.

    Raises
    ------
    ValueError if the required `path` is not found

    Returns
    -------
    The absolute path to the required resource as a `pathlib.Path`

    Z
phonemizershareNz'the requested resource does not exist: )	importlib	resourcesfilesAttributeErrorr0   exists
ValueErrorresolve)r0   r1   r   r   r   get_package_resourceZ   s   
r9   version.c                 C   s    t dd | dddD S )zReturns a tuple of integers from a version string

    Any '-dev' in version string is ignored. For instance, returns (1, 2, 3)
    from '1.2.3' or (0, 2) from '0.2-dev'

    c                 s   r"   r#   )r)   )r   vr   r   r   r&      r'   z#version_as_tuple.<locals>.<genexpr>z-dev .)tuplereplacer   )r:   r   r   r   version_as_tuple|   s    r@   )__doc__r   numbersr   pathlibr   typingr   r   r   r   r2   r   r   r   r   r)   r/   r9   r@   r   r   r   r   <module>   s   "

)"