o
    xiN                     @   sV  d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZ d?dee fd	d
ZdeddfddZdedededdfddZ			d@dededee dee ddf
ddZdAdededefddZddefdede
d d edee d!ee d"edefd#d$Zd%edefd&d'Z d%edefd(d)Z!deddfd*d+Z"ded ee dee d!ee d"edefd,d-Z#dedefd.d/Z$dedefd0d1Z%d%edefd2d3Z&dedefd4d5Z'dedefd6d7Z(	d?d8ed9e)d:ee deee)f fd;d<Z*	d?d8ed9e)d:ee deee)f fd=d>Z+dS )B    N)OptionalTupleListAnyDictMatch)Literal)errors)my_math)utils)TextgridFormatsMIN_INTERVAL_LENGTHIntervalPointINTERVAL_TIER
POINT_TIERreturnc                 C   s2   |r
t | ||}nt | |}|std|S )zESearch for the string to match. Throws an error if no match is found.z#Expected field in Textgrid missing.)researchr	   ParsingError)patternstringflagsmatches r   `/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/praatio/utilities/textgrid_io.pyreSearch   s   
r   tierc                 C   s   dd }t || d | d< d S )Nc                 S   s   | d dkS )N r   )entryr   r   r   
hasContent"   s   z!_removeBlanks.<locals>.hasContententries)filter)r   r!   r   r   r   _removeBlanks!   s   r$   	minLengthminTimestampc                 C   s2  g }d}| d D ]F\}}}|| |k r-t |dkr,||d  \}}	}
t|||
||d < qt |dkrA||krA|t||| n	|t||| |d7 }qd}|t |d k rt|| d ||d  d  }|dkr||k rt|| d ||d  d || d ||< |d7 }|t |d k sY|| d< dS )zRemove intervals that are very tiny

    Doing many small manipulations on intervals can lead to the creation
    of ultrashort intervals (e.g. 1*10^-15 seconds long).  This function
    removes such intervals.
    r   r"         N)lenr   appendabs)r   r%   r&   
newEntriesjstartendlabel	lastStart_	lastLabeldiffr   r   r   _removeUltrashortIntervals(   s0   
 


r5   r   
blankLabelminTimemaxTimec           
      C   sx  |du r| d }|du r| d }t | d dkr"| d |||f | d }|d }t|d }|g}|dd D ]!}t|d }t|d }	||k rS||||f || |	}q9t|d d t|k rltdt|d d t|kr|d||d d |f |durt|d d t|krtd	t|d d t|k r||d d ||f |  || d< dS )
zFills in the space between intervals with empty space

    This is necessary to do when saving to create a well-formed textgrid
    Nxminxmaxr"   r   r'   zDThe entries are shorter than the min time specified in the textgrid.r   zCThe entries are longer than the max time specified in the textgrid.)r)   r*   floatr	   r   insertsort)
r   r6   r7   r8   r"   r    prevEndr,   newStartnewEndr   r   r   _fillInBlanksV   s@   

rA   FdataincludeEmptyIntervalsc                 C   s   zt | }d| v rt|}W n ty/   d| v }d| v}|s$|r)t| }nt| }Y nw |du r?|d D ]}t| q8|S )a|  Converts a string representation of a Textgrid into a dictionary

    https://www.fon.hum.uva.nl/praat/manual/TextGrid_file_formats.html

    Args:
        fnFullPath: the path to the textgrid to open
        includeEmptyIntervals: if False, points and intervals with
             an empty label '' are not included in the returned dictionary

    Returns:
        Dictionary
    r.   zooTextFile shortzitem [Ftiers)jsonloadskeys_upconvertDictionaryFromJson
ValueError_parseShortTextgrid_parseNormalTextgridr$   )rB   rC   tgAsDictcaseAcaseBr   r   r   r   parseTextgridStr   s"   


rO   tgformat)short_textgridlong_textgridrE   textgrid_jsonincludeBlankSpacesmaxTimestampminimumIntervalLengthc                 C   sz   t d|t t| ||||} |tjkrt| }|S |tjkr%t| }|S |tjkr2t	t
| }|S |tjkr;t	| }|S )a  Converts a textgrid to a string, suitable for saving

    Args:
        tg: the textgrid to convert to a string
        format: one of ['short_textgrid', 'long_textgrid', 'json', 'textgrid_json']
        includeBlankSpaces: if True, blank sections in interval
            tiers will be filled in with an empty interval
            (with a label of "")
        minTimestamp: the minTimestamp of the saved Textgrid;
            if None, use whatever is defined in the Textgrid object.
            If minTimestamp is larger than timestamps in your textgrid,
            an exception will be thrown.
        maxTimestamp: the maxTimestamp of the saved Textgrid;
            if None, use whatever is defined in the Textgrid object.
            If maxTimestamp is smaller than timestamps in your textgrid,
            an exception will be thrown.
        minimumIntervalLength: any labeled intervals smaller
            than this will be removed, useful for removing ultrashort
            or fragmented intervals; if None, don't remove any.
            Removed intervals are merged (without their label) into
            adjacent entries.

    Returns:
        a string representation of the textgrid
    rQ   )r   validateOptionr   _prepTgForSavingLONG_TEXTGRID_tgToLongTextFormSHORT_TEXTGRID_tgToShortTextFormJSON	_tgToJson_downconvertDictionaryForJsonTEXTGRID_JSON)rP   rQ   rU   r&   rV   rW   	outputTxtr   r   r   getTextgridAsStr   s    "




rc   rL   c              	   C   sp   i }| d |d< | d |d< g |d< | d   D ]}| d | }|d |d || d | d |d d q|S )	z^
    Convert from the sparse json format to the one shaped more literally like a textgrid
    r.   r9   r/   r:   rD   typer"   classnamer9   r:   r"   )rG   r*   )rL   transformedDicttierNamer   r   r   r   rH      s   
rH   c                 C   s@   i }| d D ]}|d |d d||d < q| d | d |dS )	zT
    Convert from the textgrid-shaped json format to a more minimal json format
    rD   rf   r"   )rd   r"   rg   r9   r:   )r.   r/   rD   r   )rL   rD   r   r   r   r   r`      s   r`   c                 C   s"   | d D ]
}t |d |d< qd S )NrD   r"   )sorted)rP   r   r   r   r   _sortEntries  s   rk   c                 C   s   t |  |d u r| d }n|| d< |d u r| d }n|| d< |rFg }| d D ]}|d tkr4|| q&t|d|| |d urEt||| q&t |  | S )Nr9   r:   rD   rf   r   )rk   r   r*   rA   r5   )rP   rU   r&   rV   rW   newTierListr   r   r   r   rY     s&   


rY   c                 C   s  d}|d7 }|d7 }|dt | d t | d f 7 }|dt| d  7 }| d D ]\}d}|d	|d
  7 }|d	t|d  7 }|dt |d t |d t|d f 7 }|d D ]%}dd |d d D dt|d  g }|ddd |D d 7 }q\||7 }q*|S )Nr   File type = "ooTextFile"
Object class = "TextGrid"

z%s
%s
r9   r:   z<exists>
%d
rD   z"%s"
rf   rg   z	%s
%s
%s
r"   c                 S   s   g | ]}t |qS r   )r
   numToStr.0valr   r   r   
<listcomp>N  s    z&_tgToShortTextForm.<locals>.<listcomp>r   z"%s"
c                 S   s   g | ]}t |qS r   )strrp   r   r   r   rs   R      )r
   ro   r)   r   escapeQuotesjoin)rP   rb   r   textr    r   r   r   r]   6  s0   

r]   c                 C   s6  d}|d7 }|d7 }d}|dt | d  7 }|dt | d  7 }|d	7 }|d
t| d  7 }|d7 }t| d D ]\}}||d|d   7 }||d d|d   7 }||d dt|d   7 }||d dt |d   7 }||d dt |d   7 }|d }|d tkr||d dt|  7 }t|D ]<\}}|\}}	}
||d d|d   7 }||d dt |  7 }||d dt |	  7 }||d dt|
  7 }qq:||d dt|  7 }t|D ].\}}|\}}
||d d|d   7 }||d dt |  7 }||d dt|
  7 }qq:|S )Nr   rm   rn   z    zxmin = %s 
r9   zxmax = %s 
r:   ztiers? <exists> 
zsize = %d 
rD   z
item []: 
zitem [%d]:
r'   r(   zclass = "%s" 
rf   zname = "%s" 
rg   r"   zintervals: size = %d 
zintervals [%d]:
   ztext = "%s" 
zpoints: size = %d 
zpoints [%d]:
znumber = %s 
zmark = "%s" 
)r
   ro   r)   	enumerater   rw   r   )rP   rb   tabtierNumr   r"   intervalNumr    r.   r/   r0   pointNum	timestampr   r   r   r[   Y  sB   
r[   c                 C   s   t j| ddS )z+Returns a json representation of a textgridF)ensure_ascii)rE   dumps)rL   r   r   r   r_     s   r_   c              	   C   s~  |  dd} tjd| dtjd\}} |d}t|d dd  }t|d dd  }g }tjd| tjd	dd
 }|D ]}d|v rMt}d}	nt}d}	ztj|	|tjd	}
|
d |
dd
 }}W n ty|   t	d|ry|}g }n Y nw t
d|tjd	 d }tdd|}t
d|tjd	 d }t|}t
d|tjd	 d }t|}g }|tkr|D ]=}t
d|tjd	 d }t
d|tjd	 d }t
d|tjtjB d	 d }| }tdd|}|t||| qn,|D ])}t
d|tjd	 d }t
d|tjtjB d	 d }| }|t|| q||t|t||d}|| qB|||d}|S )z!
    Reads a normal textgrid
    
rt   zitem ?\[r'   )maxsplitr   rz   =   )r   Nzclass = "IntervalTier"zintervals ?\[z
points ?\[r   z
size ?= ?0zname ?= ?\"(.*)\"\s*$"""zxmin ?= ?-?([\d.]+)\s*$zxmax ?= ?([\d.]+)\s*$ztext ?= ?\"(.*)\"\s*$znumber ?= ?-?([\d.]+)\s*$zmark ?= ?\"(.*)\"\s*$re   r9   r:   rD   )replacer   split	MULTILINEr;   stripr   r   rI   r   r   groupssubr   strToIntOrFloatDOTALLr*   r   r   )rB   header
headerListtgMintgMaxrD   tierListtierTxttierType
searchWorddtierDatari   tierStartTimeStrtierStartTimetierEndTimeStrtierEndTimer"   element	timeStarttimeEndr0   time
tierAsDicttgDictr   r   r   rK     s   




rK   c           !   
      s0  |  dd} dd t| dD }dd t| dD }g ||  t| df     fd	dtt d
 D }| d|d d  }|d}t|d 	 }t|d 	 }g }|D ]\}	}
}| |	|
 }t
|dd
 }t||\}}t
||\}}t
||\}}t
||d
 }t|}t|}g }|rt}	 zt
||\}}t
||\}}t||\}}W n ttfy   Y nw |	 }|t||| qn,t}	 zt
||\}}t||\}}W n ttfy   Y nw |	 }|t|| q||t|t||d}|| qa|||d} | S )zReads a short textgrid filer   rt   c                 S      g | ]}|d fqS )Tr   rq   ir   r   r   rs     rv   z'_parseShortTextgrid.<locals>.<listcomp>z"IntervalTier"c                 S   r   Fr   r   r   r   r   rs     rv   z
"TextTier"Tc                    s2   g | ]} | d   |d  d   | d fqS )r   r'   r   r   	indexListr   r   rs     s    $r'   Nr   rz   r   re   r   )r   r   findAllr*   r)   r=   ranger   r;   r   	_fetchRow_fetchTextRowr   r   rI   
IndexErrorr   r   r   )!rB   intervalIndiciespointIndicies	tupleListr   r   r   r   rD   blockStartI	blockEndI
isIntervalr   
metaStartIri   tierNameEndIr   tierStartTimeIr   tierEndTimeI
startTimeIr   r   r"   	className	startTimeendTimeIendTimelabelIr0   r   r   r   r   r   r   rJ     st   




rJ   dataStrindex	searchStrc                 C   sz   |d u r|}n
|  ||t| }|  d|}| || }| }|d dkr3|d dkr3|dd }| }||d fS )Nrt   r   r   r   r'   )r   r)   r   )r   r   r   
startIndexendIndexwordr   r   r   r   7  s   r   c                 C   s   |d u r|}n
|  ||t| }|d }	 |  d|}|}| | dkr.|d7 }| | dks$|}|| d dkr9nq| || }|dd }| }|dd}|  d|}||d fS )	Nr'   Tr   r(   r   r   r   rt   )r   r)   r   r   )r   r   r   r   r   quoteStartIndexquoteEndIndexr   r   r   r   r   J  s(   r   )N)r   NNr   ),r   rE   typingr   r   r   r   r   r   typing_extensionsr   praatio.utilitiesr	   r
   r   praatio.utilities.constantsr   r   r   r   r   r   ru   r   r$   r;   r5   rA   boolrO   rc   dictrH   r`   rk   rY   r]   r[   r_   rK   rJ   intr   r   r   r   r   r   <module>   s      

0
8%
4
$
#*aO


