
    ^j                        d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ  ee          	                                j
        d         Zedz  Zedz  Zedz  Zedz  Zd	d
ddddddgZd Zd Zd Zd Zedk    rS	  ej         e                       dS # e$ r0Z ede ej                    ej        d           Y dZ[dS dZ[ww xY wdS )    N)datetimetimezone)Path   zstory/story-canon.txtzstory/spoken-narration.txtzstory/tts-pronunciation.txtzlog/tts-pronunciation.jsonaudiou	   au đi ôF)sourcespokenrequiredvalizva liTc                 N    t          j        |                                           S N)hashlibsha256	hexdigest)datas    #script/prepare-tts-pronunciation.py	sha_bytesr      s    >$))+++    c                 D    t          |                                           S r   )r   
read_bytes)paths    r   sha_filer      s    T__&&'''r   c                    |                                  r|                                S | d d                                          rC| dd                                          r'|d d                                         |dd          z   S |S )Nr   )isupperupperislower)source_textr	   s     r   case_spokenr      s     ||~~2A2   /[_%<%<%>%> /bqbz!!F122J..Mr   c                     t                                           rt                                          st          d          t                                          } t                                           }| |k    rt          d          |                     d          }t          t          d d          }d |D             }d                    d	 |D                       }t          j
        d
| dt          j                  }g }g }d}	d}
d |D             }|                    |          D ]Y}||	|                                         }|                    |           |
t          |          z  }
|                    d          }||                                         }t%          ||d                   }|                    dd|                                          dz   }|                    |||                                |                                |
|
t          |          z   |d           ||d         xx         dz  cc<   |                    |           |
t          |          z  }
|                                }	[|                    ||	d                     d                    |          }|}t+          |          D ]S}|d         |d         }}|||         |d         k    rt          d          |d |         |d         z   ||d          z   }T||k    }|st          d          i }g }|D ]}t          j
        dt          j        |d                    dt          j                  }t          |                    |                    }|||d         <   |d         r*||d                  dk    rt          d|d                    |d         r|dk    rt          d|d                    |                    i |||d                  |d           |                    d          }t2                              d          }|                    |           |                    t2                     t;          |          o3|o1t=          |                                          t          |          k    }i ddd |d!|rd"nd#d$tA          t           !                    tD                              d%tG          t                     d&tA          t          !                    tD                              d'tG          t                    d(tA          t2          !                    tD                              d)tG          t2                    d*d+d,|d-|d.t          |          d/|d0d1d2dd3tI          j%        tL          j'                  (                                }tR          j*        +                    dd4           tR          ,                    t[          j.        |d1d56          dz   d7           t_          t[          j.        ||d'         |d)         t          |          |d8d19                     |rdndS ):Nz!canon or spoken narration missingz/spoken narration is not byte-identical to canonzutf-8c                 ,    t          | d                   S )Nr   )len)rows    r   <lambda>zmain.<locals>.<lambda>-   s    c#h-.@.@ r   T)keyreversec                 D    i | ]}|d                                           |S )r   )casefold.0r"   s     r   
<dictcomp>zmain.<locals>.<dictcomp>.   s*    BBB3X''))3BBBr   |c              3   J   K   | ]}t          j        |d                    V  dS )r   N)reescaper(   s     r   	<genexpr>zmain.<locals>.<genexpr>/   s0      HHBIc(m44HHHHHHr   z
(?<!\w)(?:z)(?!\w)r   c                      i | ]}|d          dS )r   r    r(   s     r   r*   zmain.<locals>.<dictcomp>6   s    2223c(mQ222r   r	   
r   )r   r	   source_start
source_endoutput_start
output_endliner    r5   r6   z2pronunciation reverse verification offset mismatchz)pronunciation reverse verification failedz(?<!\w)z(?!\w)r
   z1required pronunciation token absent from source: z&required pronunciation token remains: )replacement_countremaining_countz	.part.txtversionverifiedstatus	completedfailedsource_canon_pathsource_canon_sha256source_pathsource_sha256output_pathoutput_sha256lexicon_strategyBdeterministic_longest_match_first_unicode_word_boundary_case_awarelexiconoccurrencesr9   reverse_verifiedsemantic_content_changedFnon_lexicon_changes
checked_at)parentsexist_ok   )ensure_asciiindent)encoding)r<   rC   rE   r9   counts)rQ   )0CANONis_fileSOURCERuntimeErrorr   decodesortedLEXICONjoinr-   compile
IGNORECASEfinditerstartappendr!   groupr'   r   countendreversedr.   findallencodeOUTPUTwith_suffixwrite_bytesreplaceboolsumvaluesstrrelative_toPROJECTr   r   nowr   utc	isoformatRECEIPTparentmkdir
write_textjsondumpsprint) source_bytescanon_bytestextordered	by_sourcealternativespatternpartsrI   source_cursoroutput_cursorrT   match	unchangedr   r"   r	   r7   	projectedrestoreditemr`   rd   rJ   	remaininglexicon_receiptregexr:   output_bytestempr<   receipts                                    r   mainr   %   s   ==?? @&.."2"2 @>???$$&&L""$$K{""LMMMw''DW"@"@$OOOGBB'BBBI88HHHHHHHLj<|<<<bmLLGEKMM22'222F!!$'' $ $u{{}}45	YY'kk!nn,,../[#h-88zz$5;;==11A5!!KKMM))++)'#f++5
 
 	 	 	 	s8}"VV$			LLmnn%&&&I H%% F F.)4+=sE#I$x.00STTTFUF#d8n4x~E4' HFGGGIO x x
FbiH&>&>FFFVVemmI6677#2	#h- z? 	dvc(m499bSVW_S`bbcccz? 	Y!33WHWWXXXv#vF3x=<Qfuvvvwwww##G,,Lk**D\"""LLL!!c&6c3v}};O;OSVWbScSc;cH1H 	7++x 	S!2!27!;!;<<	
 	x 	s6--g6677 	&)) 	s6--g6677 	&)) 	` 	? 	{ 	S-- 	, 	#E  	q!" 	hl8<00::<<#G& N555tz'aHHH4OZabbb	$* 1 1 --        11ar   __main__zPronunciation gate blocked: )file)r   ry   r-   sysr   r   pathlibr   __file__resolverN   rq   rU   rW   rh   ru   r[   r   r   r   r   __name__exit	Exceptionexcr{   stderrr1   r   r   <module>r      s     				 



 ' ' ' ' ' ' ' '      
$x..
 
 
"
"
*1
-))	/	/	0	0
0
0 +5AAd;;, , ,( ( (  c  c  c L z   2S22DDDD s   :B C	%CC	