
    hc*              	           S SK Jr  S SKJr  S SKJrJrJrJrJ	r	  S SK
r
S SKJr  SSKJr  SSKJr  SS	KJrJrJrJr  \" 5       rSS
\S\\   S\SS4S jjr " S S\5      r " S S5      rg)    )OrderedDict)Path)AnyDictListOptionalUnionN)BloomFilter   )Errors)get_string_id)SimpleFrozenDictensure_pathload_language_dataregistrylangtablesstrictreturnLookupsc                    [        5       nU [        R                  ;  aN  U(       aE  [        U5      S:  a6  [	        [
        R                  R                  SR                  U5      U S95      eU$ [        R                  R                  U 5      nU HV  nXT;  a0  U(       a&  [	        [
        R                  R                  XPS95      e0 nO[        XE   5      nUR                  XV5        MX     U$ )a  Load the data from the spacy-lookups-data package for a given language,
if available. Returns an empty `Lookups` container if there's no data or if the package
is not installed.

lang (str): The language code (corresponds to entry point exposed by
    the spacy-lookups-data package).
tables (List[str]): Name of tables to load, e.g. ["lemma_lookup", "lemma_exc"]
strict (bool): Whether to raise an error if a table doesn't exist.
RETURNS (Lookups): The lookups container containing the loaded tables.
r   z, )tabler   )r   r   lookupslen
ValueErrorr   E955formatjoingetr   	add_table)r   r   r   r   datar   language_datas          G/home/james-whalen/.local/lib/python3.13/site-packages/spacy/lookups.pyload_lookupsr$      s     iG8###c&kAoV[[//dii6Gd/STT%D !3!3%!3!KLLM.t{;M%/  N    c                   *   \ rS rSrSr\SS\S\\   SS 4S jj5       r	SS\\   S\\   SS4S jjr
S	\\\4   S
\SS4S jrS	\\\4   S
\SS4S jrS	\\\4   S\4S jrSS	\\\4   S\\   S\4S jjrS	\\\4   S\4S jrS\4S jrS\SS 4S jrSrg)Table,   zA table in the lookups. Subclass of builtin dict that implements a
slightly more consistent and unified API.

Includes a Bloom filter to speed up missed lookups.
Nr!   namer   c                 4    U " US9nUR                  U5        U$ )zInitialize a new table from a dict.

data (dict): The dictionary.
name (str): Optional table name for reference.

DOCS: https://spacy.io/api/lookups#table.from_dict
r)   )update)clsr!   r)   selfs       r#   	from_dictTable.from_dict3   s     ~Dr%   c                     [         R                  " U 5        Xl        SU l        Ub  [	        [        U5      S5      OU R                  n[        R                  " U5      U l        U(       a  U R                  U5        gg)zInitialize a new table.

name (str): Optional table name for reference.
data (dict): Initial data, used to hint Bloom Filter.

DOCS: https://spacy.io/api/lookups#table.init
g    .ANr   )
r   __init__r)   default_sizemaxr   r
   from_error_ratebloomr,   )r.   r)   r!   sizes       r#   r2   Table.__init__@   sb     	T"	$($4s3t9a $:K:K 006
KK r%   keyvaluec                 ~    [        U5      n[        R                  " XU5        U R                  R	                  U5        g)zoSet new key/value pair. String keys will be hashed.

key (str / int): The key to set.
value: The value to set.
N)r   r   __setitem__r6   addr.   r9   r:   s      r#   r<   Table.__setitem__Q   s.     C 51

sr%   c                     X U'   g)zSet new key/value pair. String keys will be hashed.
Same as table[key] = value.

key (str / int): The key to set.
value: The value to set.
N r>   s      r#   set	Table.set[   s     S	r%   c                 D    [        U5      n[        R                  " X5      $ )zqGet the value for a given key. String keys will be hashed.

key (str / int): The key to get.
RETURNS: The value.
)r   r   __getitem__r.   r9   s     r#   rE   Table.__getitem__d   s     C &&t11r%   defaultc                 F    [        U5      n[        R                  " XU5      $ )zGet the value for a given key. String keys will be hashed.

key (str / int): The key to get.
default: The default value to return.
RETURNS: The value.
)r   r   r   )r.   r9   rH   s      r#   r   	Table.getm   s     C t'22r%   c                 d    [        U5      nXR                  ;  a  g[        R                  " X5      $ )zCheck whether a key is in the table. String keys will be hashed.

key (str / int): The key to check.
RETURNS (bool): Whether the key is in the table.
F)r   r6   r   __contains__rF   s     r#   rL   Table.__contains__w   s,     C jj ''22r%   c                     U R                   [        U R                  5       5      U R                  R	                  5       S.n[
        R                  " U5      $ )z|Serialize table to a bytestring.

RETURNS (bytes): The serialized table.

DOCS: https://spacy.io/api/lookups#table.to_bytes
)r)   dictr6   )r)   rO   itemsr6   to_bytessrslymsgpack_dumps)r.   r!   s     r#   rQ   Table.to_bytes   sC     II&ZZ((*

 ""4((r%   
bytes_datac                     [         R                  " U5      nUR                  S0 5      nUS   U l        [	        5       R                  US   5      U l        U R                  5         U R                  U5        U $ )zLoad a table from a bytestring.

bytes_data (bytes): The data to load.
RETURNS (Table): The loaded table.

DOCS: https://spacy.io/api/lookups#table.from_bytes
rO   r)   r6   )	rR   msgpack_loadsr   r)   r
   
from_bytesr6   clearr,   )r.   rU   loadedr!   s       r#   rX   Table.from_bytes   sa     $$Z0zz&"%6N	 ]--fWo>


Dr%   )r6   r3   r)   )N)NN)__name__
__module____qualname____firstlineno____doc__classmethodrO   r   strr/   r2   r	   intr   r<   rB   rE   r   boolrL   bytesrQ   rX   __static_attributes__rA   r%   r#   r'   r'   ,   s    
T 
# 
' 
 
Xc] $ SW "uS#X s t uS#X s t 2uS#X 23 23uS#X 3# 3# 3
3c3h 
3D 
3)% )U w r%   r'   c                   H   \ rS rSrSrSS jrS\S\4S jrS\	4S jr
\S\\   4S	 j5       r\" 5       4S\S
\S\4S jjrS\S\SS4S jr\4S\S\S\4S jjrS\S\4S jrS\S\4S jrS\4S jrS\SS 4S jr SS\\\4   S\SS4S jjr SS\\\4   S\SS 4S jjrSrg)r      a?  Container for large lookup tables and dictionaries, e.g. lemmatization
data or tokenizer exception lists. Lookups are available via vocab.lookups,
so they can be accessed before the pipeline components are applied (e.g.
in the tokenizer and lemmatizer), as well as within the pipeline components
via doc.vocab.lookups.
r   Nc                     0 U l         g)zHInitialize the Lookups object.

DOCS: https://spacy.io/api/lookups#init
N_tablesr.   s    r#   r2   Lookups.__init__   s    
 *,r%   r)   c                 $    U R                  U5      $ )zCheck if the lookups contain a table of a given name. Delegates to
Lookups.has_table.

name (str): Name of the table.
RETURNS (bool): Whether a table of that name is in the lookups.
)	has_tabler.   r)   s     r#   rL   Lookups.__contains__   s     ~~d##r%   c                 ,    [        U R                  5      $ )z3RETURNS (int): The number of tables in the lookups.)r   rk   rl   s    r#   __len__Lookups.__len__   s    4<<  r%   c                 H    [        U R                  R                  5       5      $ )z8RETURNS (List[str]): Names of all tables in the lookups.)listrk   keysrl   s    r#   r   Lookups.tables   s     DLL%%'((r%   r!   c                     XR                   ;   a&  [        [        R                  R	                  US95      e[        XS9nX0R                  U'   U$ )zAdd a new table to the lookups. Raises an error if the table exists.

name (str): Unique name of table.
data (dict): Optional data to add to the table.
RETURNS (Table): The newly added table.

DOCS: https://spacy.io/api/lookups#add_table
r+   )r)   r!   )r   r   r   E158r   r'   rk   )r.   r)   r!   r   s       r#   r    Lookups.add_table   sF     ;;V[[//T/:;;4+"Tr%   r   c                      X R                   U'   g)zSet a table.

name (str): Name of the table to set.
table (Table): The Table to set.

DOCS: https://spacy.io/api/lookups#set_table
Nrj   )r.   r)   r   s      r#   	set_tableLookups.set_table   s     #Tr%   rH   c                     XR                   ;  a<  U[        :X  a0  [        [        R                  R                  XR                  S95      eU$ U R                   U   $ )a  Get a table. Raises an error if the table doesn't exist and no
default value is provided.

name (str): Name of the table.
default (Any): Optional default value to return if table doesn't exist.
RETURNS (Table): The table.

DOCS: https://spacy.io/api/lookups#get_table
r)   r   )rk   UNSETKeyErrorr   E159r   r   )r.   r)   rH   s      r#   	get_tableLookups.get_table   sK     ||#%v{{11tKK1PQQN||D!!r%   c                     XR                   ;  a0  [        [        R                  R	                  XR
                  S95      eU R                   R                  U5      $ )zRemove a table. Raises an error if the table doesn't exist.

name (str): Name of the table to remove.
RETURNS (Table): The removed table.

DOCS: https://spacy.io/api/lookups#remove_table
r   )rk   r   r   r   r   r   poprp   s     r#   remove_tableLookups.remove_table   sD     ||#6;;--4-LMM||%%r%   c                     XR                   ;   $ )zCheck if the lookups contain a table of a given name.

name (str): Name of the table.
RETURNS (bool): Whether a table of that name exists.

DOCS: https://spacy.io/api/lookups#has_table
rj   rp   s     r#   ro   Lookups.has_table   s     ||##r%   c                 B    [         R                  " U R                  5      $ )z~Serialize the lookups to a bytestring.

RETURNS (bytes): The serialized Lookups.

DOCS: https://spacy.io/api/lookups#to_bytes
)rR   rS   rk   )r.   kwargss     r#   rQ   Lookups.to_bytes  s     ""4<<00r%   rU   c                     0 U l         [        R                  " U5      R                  5        H  u  p4[	        X45      U R                   U'   M     U $ )zLoad the lookups from a bytestring.

bytes_data (bytes): The data to load.
RETURNS (Lookups): The loaded Lookups.

DOCS: https://spacy.io/api/lookups#from_bytes
)rk   rR   rW   rP   r'   )r.   rU   r   r9   r:   s        r#   rX   Lookups.from_bytes  sD     --j9??AJC %c 1DLL Br%   pathfilenamec                     [        U5      nUR                  5       (       d  UR                  5         X-  nUR                  S5       nUR	                  U R                  5       5        SSS5        g! , (       d  f       g= f)zSave the lookups to a directory as lookups.bin. Expects a path to a
directory, which will be created if it doesn't exist.

path (str / Path): The file path.

DOCS: https://spacy.io/api/lookups#to_disk
wbN)r   existsmkdiropenwriterQ   )r.   r   r   r   filepathfile_s         r#   to_diskLookups.to_disk  sU     4 {{}}JJL?]]4 EKK( !  s    A//
A=c                     [        U5      nX-  nUR                  5       (       a;  UR                  S5       nUR                  5       nSSS5        U R	                  W5      $ U $ ! , (       d  f       N!= f)zLoad lookups from a directory containing a lookups.bin. Will skip
loading if the file doesn't exist.

path (str / Path): The directory path.
RETURNS (Lookups): The loaded lookups.

DOCS: https://spacy.io/api/lookups#from_disk
rbN)r   r   r   readrX   )r.   r   r   r   r   r   r!   s          r#   	from_diskLookups.from_disk)  s]     4 ???t$zz| %??4(( %$s   A""
A0rj   )r   N)zlookups.bin) r\   r]   r^   r_   r`   r2   rb   rd   rL   rc   rs   propertyr   r   r   rO   r'   r    r}   r   r   r   r   ro   re   rQ   rX   r	   r   r   r   rf   rA   r%   r#   r   r      sH   ,$ $ $! ! )S	 ) ) 1A0B c  u #c #% #D # 38 "c "C "E " 
& 
& 
&$c $d $1E 1U   7D)#t)$)03)	)$ 7D#t)$03	 r%   )T)collectionsr   pathlibr   typingr   r   r   r   r	   rR   preshed.bloomr
   errorsr   stringsr   utilr   r   r   r   objectr   rb   rd   r$   r'   r   rA   r%   r#   <module>r      sj    #  3 3  %  " M Ms DI t y :sK slX Xr%   