
    (HJj:                         d Z ddlZddlmZmZmZ ddlZddlZddlZddl	Z	ddl
mZ ddlZddlZddlmZmZ ddlmZ ddlmZ eeej.                  f   Z G d d	      Zy)
z
Orthography profiles.
    N)UnionOptionalAny)	Generator)
TableGroupColumn)Tree)grapheme_patternc                      e Zd ZdZdZdZddddded	dd
geddgiZeddee	   de
eef   fd       Zde
fdZdee
eef   ddf   fdZedde	dee   dd fd       Zeddededd fd       Zedde	dedd fd       Zd Zy)ProfilezG
    An Orthography Profile as specified by Moran and Cysouw 2018.
    GraphemeNULLtables	Tutf-8)	delimiterheaderencodingstring)namedatatyperequired)columns
primaryKey)dialecttableSchemaNfnamereturnc                 t    t        j                   | j                        }t        |xs d      |d   d   d<   |S )zPThe default CSVW metadata to interpret a tab-separated values file as a Profile. r   r   url)copyMDstr)clsr   mds      Z/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/segments/profile.pydefault_metadatazProfile.default_metadata2   s6     YYsvv!$U[b!18Q	    specsc           
         t        j                         | _        t               | _        |j                  dd      | _        |j                  dd      | _        || _        t        j                  t              }t        |      D ]  \  }}| j                  |vrt        d      | j                  rc|j                         D ci c]H  \  }}t!        j"                  | j                  |      |dnt!        j"                  | j                  |      J }}}|j                  | j                        }|st        d      | j                  j%                  |j'                               | _        || j                  vr|| j                  |<   |j)                  d|dz   |        t+        t-        | j                  j'                                     | _        yc c}}w )a  

        Parameters
        ----------
        specs : list of dict
            A list of grapheme specifications.
        kw :
            The following keyword arguments are recognized:
            - fname: Path of the profile or profile metadata.
            - form: Unicode normalization to apply to the data in the profile before use.
            - remaining keyword arguments are assigned as dict to `Profile.metadata`.
        r   Nformzinvalid grapheme specificationzGrapheme must not be emptyz)line %s:duplicate grapheme in profile: %s   )collectionsOrderedDict	graphemessetcolumn_labelspopr   r,   metadatalogging	getLogger__name__	enumerateGRAPHEME_COL
ValueErroritemsunicodedata	normalizeunionkeyswarningr	   listtree)	selfr*   kwlogispeckvgraphemes	            r'   __init__zProfile.__init__9   s    %002 UVVGT*
FF64(	) 'GAt  , !ABByy !%

. !-1  ))$))Q7 !	{/D/DTYYPQ/RS ,  .
 xx 1 12H !=>>!%!3!3!9!9$))+!FD t~~-+/x(GQPXY) (* dnn11345	!.s   AGc              #     K   | j                   j                         D ]`  \  }}| j                  |i}|j                  | j                  D ci c]  }|d c}       |j                  |j                                | b yc c}w w)z&Yield grapheme specs from the Profile.N)r0   r;   r9   updater2   )rC   rJ   rG   resrH   s        r'   	iteritemszProfile.iteritemsd   sr     "nn224NHd$$h/CJJ););<);A4);<=JJtzz|$I	 5<s   AB

B
2Br,   c                    	 t        j                  |      }d}t        |j                        dk7  rt        d      |j                  }|j                  t        j                  |      |       t        j                         5  t        j                   d        | |j                  d   j#                  |      D cg c]F  }|j%                         D ci c](  \  }}||| j&                  k7  r|| j(                  k(  rdn|* c}}H c}}}i |}	ddd       |	S # t        j                  j                  $ r* t        j
                  | j                  |            }|}Y Hw xY wc c}}w c c}}}w # 1 sw Y   	S xY w)zk
        Read an orthography profile from a metadata file or a default tab-separated profile file.
        N   z2profile description must contain exactly one table)r   r,   ignorer   r   )r   	from_filejsondecoderJSONDecodeError	fromvaluer(   lenr   r:   common_propsrM   pathlibPathwarningscatch_warningssimplefilter	iterdictsr;   r9   r   )
r%   r   r,   tgopfnamer4   drH   rI   rN   s
             r'   rT   zProfile.from_filel   s]   
	%%e,BG ryy>QQRR??gll51=$$&!!(+ 99Q<111@B@a  !wwy*(tq! qC$4$44chhdQN(*@B 	C ' 
 ||++ 	%%c&:&:5&ABBG	* B ' 
sB   D ;E2=E+-E%E+	E2AE"!E"%E++E22E<mappingtextc           
          t        j                  t        j                  |            }|j	                         D cg c]-  \  }}t        j
                  | j                  |fd|f||fg      / }}} | | S c c}}w )z
        Create a Profile instance from the Unicode graphemes found in `text`.

        Parameters
        ----------
        text
        mapping

        Returns
        -------
        A Profile instance.

        	frequency)r.   Counterr
   findallmost_commonr/   r9   )r%   re   rd   r0   rJ   rg   r*   s          r'   	from_textzProfile.from_text   s      ''(8(@(@(FG	 (1'<'<'>@
 (?#)	 ##!!8,i((#%% & (? 	 @ E{@s   2A6c                     t        j                  |      j                  d      5 }|j                         }| j	                  dj                  |      |      cddd       S # 1 sw Y   yxY w)z=Initialize a Profile from the graphemes found in a text file.r   )r    rd   N)r[   r\   open	readlinesrk   join)r%   r   rd   fpliness        r'   from_textfilezProfile.from_textfile   sO     \\% %%w%72LLNE==%'=B 877s   2A""A+c                    t        j                  | j                               }| j                  D ]d  }|| j                  k7  s|j
                  d   j                  j                  j                  t        j                  || j                  d             f |j
                  d   j                  | j                         d      j                  d      j                         S )z]
        A Profile is represented as tab-separated lines of grapheme specifications.
        r   )r   nullNrS   utf8)r   rX   r(   r2   r9   r   r   r   appendr   r   writerO   decodestrip)rC   ra   cols      r'   __str__zProfile.__str__   s     !!$"7"7"9:%%Cd'''		!((0077$$c499%EFH &
 yy|!!$.."2$!?FFvNTTVVr)   )Nrn   )r7   
__module____qualname____doc__r9   r   r#   classmethodr   PathTypedictr$   r   r(   rK   r   rO   rT   rk   rt   r}    r)   r'   r   r      s<    LD "&" ' %1(0(,  #/	 

B, Xh%7 4S>  )6t )6V9T#s(^T4%?@  h hsm y  . S 3 y  . C( CS C C C
Wr)   r   )r   r"   typingr   r   r   r5   r[   r]   r.   collections.abcr   r<   json.decoderrU   csvwr   r   segments.treer	   segments.utilr
   r$   r\   r   r   r   r)   r'   <module>r      sT     ' '     %   #  *gll"#WW WWr)   