
    _j.                     \   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ  ee          	                                j
        d         ZdZdZdddgd	d
dddgd	d
dddgd	d
dddgd	d
dddgdd
dddgdd
dddgdd
gZg dZd Zd Zd Zd Zd  Zd! Zd" Zd# Zed$k    r e             dS dS )%    N)Path   zgma-pronunciation-v1u   Các bạn đang nghe truyện được phát từ Gác Mái Audio, chúc các bạn có những giây phút nghe truyện vui vẻ. Hãy ủng hộ chúng tôi bằng cách like video và đăng ký kênh.   05 giờ 12 phútu   năm giờ mười hai phút	narrationz8expand exact clock expression for natural Vietnamese TTS)sourcespokenscopereason   05 giờ 09u   năm giờ không chín phút   01 giờ 37 phútu!   một giờ ba mươi bảy phút   03 giờ 11u   ba giờ mười một phútAudiou	   au đi ôintroz2brand pronunciation only; semantic intro unchangedlikelaiz8English CTA pronunciation only; semantic intro unchangedvideou	   vi đi ôz=English loanword pronunciation only; semantic intro unchanged)r   r   r   r   r   r   r   c                 N    t          j        |                                           S )N)hashlibsha256	hexdigest)datas    !script/normalize-pronunciation.py	sha_bytesr      s    >$))+++    c                    t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                  .                          d          S )Ni   )read)handles   r   <lambda>zsha_path.<locals>.<lambda>    s    &++o">"> r   r   )r   r   openiterupdater   )pathdigestblockr   s      @r   sha_pathr'      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     | j                             dd           |                     | j        dz             }|                    |           t          j        ||            d S )NT)parentsexist_ok.part)parentmkdirwith_suffixsuffixwrite_bytesosreplace)r$   r   parts      r   atomic_bytesr4   %   sc    KdT222DK'122DTJtTr   c                 |    t          | t          j        |dd          dz                       d                     d S )NF   ensure_asciiindent
utf-8)r4   jsondumpsencode)r$   values     r   atomic_jsonr@   ,   s=    
5uQGGG$NVVW^__`````r   c                 V    t          | d |                             d                    S )Nr;   )lenr>   )textchar_offsets     r   byte_offsetrE   0   s'    tL[L!((11222r   c                     t          j        dt          j        |           z   dz   t           j        t           j        z            S )Nz	(?<![\w])z(?![\w]))flags)recompileescape
IGNORECASEUNICODE)r   s    r   patternrM   4   s6    :lRYv%6%66DBM\^\fLfggggr   c                    g }t          t                    D ]\  }}||d         vrt          |d                                       |           D ]Y}|                    |                                |                                ||                    d          |d         f           Z|                    d            g }d}|D ],}|d         |k     r|                    |           |d         }-g }	g }
d}d}|D ]\  }}}}}| ||         }|	                    |           |t          |          z  }|}|	                    |           |t          |          z  }|}|
                    ||||||gt          | |          t          | |          g||gd	           |}|	                    | |d                     d
                    |	          }|
D ]0}|d         \  }}t          ||          t          ||          g|d<   1g }d}|
D ]G}|d         \  }}|                    |||                    |                    |d                    |}H|                    ||d                     d
                    |          }|                    d          |                     d          k    rt          d|           ||
fS )Nr	   r   r   r   c                 D    | d         | d         | d         z
   | d         fS )Nr   r   r6    )rows    r   r    ztransform.<locals>.<lambda>?   s$    SVs1vA-?Q$H r   )keyr   )r	   lexicon_indexr   r   source_char_spansource_byte_spanoutput_char_span rW   output_byte_spanr;   z"reverse reconstruction failed for )	enumerateLEXICON_ENTRIESrM   finditerappendstartendgroupsortrB   rE   joinr>   RuntimeError)rC   r	   
candidatesentry_indexentrymatchselectedcursoritempiecesreplacementssource_cursoroutput_charsr^   r_   source_valuer   	unchanged	out_startout_endoutputrQ   abrebuiltreconstructeds                             r   	transformrx   8   s<   J'88 j jUg&&U8_--66t<< 	j 	jEu{{}}eiikk;TUX]^fXghiiii	jOOHHOIIIHF  7VaFLML9A  5sKvu,-	i   I& 	fF#("!&!,T5!9!9;tS;Q;Q R!*G 4
 
 	 	 	 
MM$}~~&'''WWV__F S S%&1#.vq#9#9;vq;Q;Q"RGF  +,
svfUl+,,,s8}%%%NN6&''?###GGG$$MG$$G(<(<<<GGGHHH<r   c                  D   t           dz  } t          j        |                     d                    }|                    d          dk    s-|                    di                               d          dk    rt          d	          |d
         }t           |d         z  }t           |d         z  }t           |d         z  }t           |d         z  }t           |d         z  }t           |d         z  }t           |d         z  }	|||	fD ]W}
|
                                s/|
                    |
j        dz                                             rt          d|
           Xt          j        |                    d                    }|                    d          dk    s|                    d          durt          d          |	                                }|	                                }t          |          }t          |          }||                    d          k    s||                    d          k    rt          d          t          j        |                    d                    }|                    d          dk    s|                    d          durt          d          |                    d          |k    s|                    d          |k    rt          d          |                    d           d!k    rt          d"          g }|                    d#g           D ]l}t           |d$         z  }|	                                }t          |          |d%         k    rt          d&|d$                    |                    |           mt          |          d'k    sd(                    |          |k    rt          d)          |                    d          }t          j        d*|          |k    rt          d+          t#          |d,          \  }}t#          t$          d-          \  }}d. t'          j        d/|          D             }|rt          d0|d d1                    g }g }t+          t,                    D ] \  }}d,|d2         v r5t          t/          |d3                                       |                    nd4}d-|d2         v r:t          t/          |d3                                       t$                              nd4} d-|d2         v p	d,|d2         v }!|| z   d4k    rd5nd6}"|                    ||d3         |d7         |d2         || |"d8           |!r!|"d5k    r|                    |d3                    |rt          d9|           t3          d: |D                       }#t3          d; |D                       }$t          |          |#k    st          |          |$k    rt          d<          d=dt4          d>t,          t6          g d?}%t          j        |%d@dAB          dCz                       d          }&|                    d          }'t<          j                            t<          j         j!                  "                                }(i dddddDt4          d|dE|d         d|dF|d         dGt          |'          dH|d         dIt          |&          dJ|dK|dLt          |          dMddNddOd4dPd4t$          t          t$                              d                    |t          |                    d                    |t          |          ddQ|(dR})tG          ||&           tG          ||'           tI          |	|)           tK          |          |)dI         k    stK          |          |)dG         k    rt          dS          dT|d<   |)dG         |dG<   |)dI         |dU<   d|d         dV<   dd|d         ||)dG         t          |          d4|(dW|dV<   |(|dX<   tI          | |           tM          t          j        dt          |          t          |          |)dG         dYd@Z                     d S )[Nzscript/project-manifest.jsonr;   )encoding
active_runzrun-002stepsstory	completedzDpronunciation blocked until active run-002 Story Promotion Gate PASSactive_pathspromotion_receiptspoken_narration_sourcestory_canontts_semantic_assembly_receiptspoken_narrationtts_pronunciation_lexicontts_pronunciation_receiptr+   z%pronunciation output must be virgin: statusverifiedTzpromotion not terminal PASSspoken_narration_source_sha256source_canon_sha256z!promotion source/canon hash driftPASSz/TTS semantic assembly receipt not terminal PASSsemantic_source_sha256source_candidate_sha256z#TTS semantic assembly lineage driftseparator_contractz.exactly three blank lines between source partszTTS separator contract driftsource_partsr$   r   zTTS source part hash drift:    s   


z+TTS semantic source reverse assembly failedNFCznarration source must be NFCr   r   c                 b    g | ],}|                     d           |                                d-S )r   )charrD   )r`   r^   ).0rg   s     r   
<listcomp>zmain.<locals>.<listcomp>   sB        Q>>  r   z[0-9]z.spoken narration has unresolved ASCII digits:    r	   r   r   resolved
unresolvedr   )rT   r   r   r	   narration_occurrencesintro_occurrencesr   z$lexicon entries have no occurrence: c              3   &   K   | ]}|d          V  dS )r   NrP   r   rQ   s     r   	<genexpr>zmain.<locals>.<genexpr>   s(      OOcS!89OOOOOOr   c              3   &   K   | ]}|d          V  dS )r   NrP   r   s     r   r   zmain.<locals>.<genexpr>   s(      GGc01GGGGGGr   z(pronunciation occurrence inventory driftlockedzJexact Unicode token boundary; longest span first; no substring replacement)r   r   transform_versionmatchingentriesreviewed_foreign_inventoryr   Fr6   r7   r:   r   spoken_narration_source_pathspoken_narration_pathspoken_narration_sha256lexicon_pathlexicon_sha256	inventoryrl   replacement_count#outside_replacement_spans_unchangedreverse_reconstruction_verifiedunresolved_countremaining_ascii_digits)source_textsource_text_sha256tts_texttts_text_sha256rl   r   r   )r   completed_atz"pronunciation readback hash failedpronunciation_completed tts_pronunciation_lexicon_sha256pronunciation)r   r   receiptsource_sha256output_sha256r   r   r   
updated_at)r   rl   intro_replacementsr   )r8   )'ROOTr<   loads	read_textgetrc   existsr.   r/   
read_bytesr   r]   rB   rb   decodeunicodedata	normalizerx   INTRO_SOURCErH   r\   rZ   r[   rM   findallsumTRANSFORM_VERSIONREVIEWED_INVENTORYr=   r>   datetimenowtimezoneutc	isoformatr4   r@   r'   print)*manifest_pathmanifestactivepromotion_pathsource_path
canon_pathtts_assembly_pathoutput_pathr   receipt_pathrs   	promotionsource_bytescanon_bytes
source_sha	canon_shatts_assembly
part_bytesrQ   r$   rawr   narration_textnarration_replacements
intro_textr   r   r   r   indexrf   narration_countintro_countrequiredr   expected_narrationexpected_introlexiconlexicon_bytesnarration_bytesr   r   s*                                             r   mainr   r   s	   99Mz-1171CCDDH||L!!Y..(,,w2K2K2O2OPW2X2X\g2g2gabbbn%FF#677N 9::K}--Jv&EFF 233K&!<==L&!<==Ll; Q Q==?? 	Qf001HIIPPRR 	QOvOOPPP	Q
>33W3EEFFI}}X+--z1J1JRV1V1V8999))++L''))K<((J+&&IY]]#CDDDD	U^UbUbcxUyUyHyHy>???:/9979KKLLL!!V++|/?/?
/K/KSW/W/WLMMM011Z??<CSCSTmCnCnr{C{C{@AAA,--1aaa9:::J33  c&k!ooS>>S]**Kc&kKKLLL#
:!y~~j99\IIHIII%%g..KUK00K??9:::-6{K-P-P*N*%.|W%E%E"J" [>::    kiLbcfdfcfLgiijjjIJ!/22 / /uP[_del_mPmPm#geHo66>>{KKLLLstMTX]^eXfMfMfc'%/22::<HHIIIlmeGn,MuW~0M.<q@@l"eHox7^o!,
 
 	 	 	
  	/*,,eHo... PN*NNOOOOOYOOOOOGGYGGGGGN
!""&888C@R<S<SWe<e<eEFFF CT`"BT	 G ZeAFFFMUUV]^^M$++G44O



 1 5
6
6
@
@
B
BC+)41DFWy 	'/H(I 	)*	
 	 (:!; 	"9_#=#= 	:; >NyYfOgOg 	Y !/0F 	S!788 ;`ae 	*4 2DQ Iabc (yI\I\]dIeIe?f?f"yARARSZA[A[7\7\.SI[E\E\/3	
 
 #  G& }---o...g&&&)9!:::h{>S>SW^_xWy>y>y?@@@2HX*12K*LH&'3:;K3LH/0)4HWo&4FC^<_#g>W6X !788aad! !H_
 !H\x(((	$*SAW=X=Xps  uG  qH  qH  el  mF  eG  H  H  W\  ]  ]  ]  ^  ^  ^  ^  ^r   __main__)r   r   r<   r1   rH   r   pathlibr   __file__resolver)   r   r   r   r[   r   r   r'   r4   r@   rE   rM   rx   r   __name__rP   r   r   <module>r      s      				 				          tH~~'**  `".LXcWd  qk  l  l(GS^R_  lf  g  g".Q]h\i  vp  q  q(FR]Q^  ke  f  f+	  NB  C  C'  GA  B  B+	  NM  N  N xww , , ,    a a a3 3 3h h h7  7  7 tx^ x^ x^v zDFFFFF r   