o
    ic*                  	   @   s   d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
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 e Zddedee deddfddZG dd deZG dd dZdS )    )OrderedDict)Path)AnyDictListOptionalUnionN)BloomFilter   )Errors)get_string_id)SimpleFrozenDictensure_pathload_language_dataregistryTlangtablesstrictreturnLookupsc                 C   s   t  }| tjvr|rt|dkrttjjd|| d|S tj	| }|D ]!}||vr<|r9ttjj|| di }nt
|| }||| q'|S )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_data r!   A/home/ubuntu/.local/lib/python3.10/site-packages/spacy/lookups.pyload_lookups   s   
r#   c                   @   s  e Zd ZdZeddedee dd fddZddee dee ddfdd	Z	d
e
eef deddfddZd
e
eef deddfddZd
e
eef defddZdd
e
eef dee defddZd
e
eef defddZdefddZdedd fddZdS )TablezA 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                 C   s   | |d}| | |S )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%   selfr!   r!   r"   	from_dict3   s   
	
zTable.from_dictc                 C   sT   t |  || _d| _|durtt|dn| j}t|| _|r(| 	| dS dS )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   sizer!   r!   r"   r+   @   s   
zTable.__init__keyvaluec                 C   s&   t |}t| || | j| dS )zSet 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__r/   addr)   r1   r2   r!   r!   r"   r3   Q   s   zTable.__setitem__c                 C   s   || |< dS )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.
        Nr!   r5   r!   r!   r"   set[      z	Table.setc                 C   s   t |}t| |S )zGet 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)   r1   r!   r!   r"   r8   d   s   zTable.__getitem__defaultc                 C   s   t |}t| ||S )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)   r1   r:   r!   r!   r"   r   m   s   z	Table.getc                 C   s"   t |}|| jvrdS t| |S )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   r/   r   __contains__r9   r!   r!   r"   r;   w   s   
zTable.__contains__c                 C   s&   | j t|  | j d}t|S )zSerialize table to a bytestring.

        RETURNS (bytes): The serialized table.

        DOCS: https://spacy.io/api/lookups#table.to_bytes
        )r%   dictr/   )r%   r<   itemsr/   to_bytessrslymsgpack_dumps)r)   r   r!   r!   r"   r>      s
   

zTable.to_bytes
bytes_datac                 C   sH   t |}|di }|d | _t |d | _|   | | | S )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
        r<   r%   r/   )	r?   msgpack_loadsr   r%   r	   
from_bytesr/   clearr'   )r)   rA   loadedr   r!   r!   r"   rC      s   


zTable.from_bytes)N)NN)__name__
__module____qualname____doc__classmethodr<   r   strr*   r+   r   intr   r3   r6   r8   r   boolr;   bytesr>   rC   r!   r!   r!   r"   r$   ,   s     
	$	
r$   c                   @   s$  e Zd ZdZd&ddZdedefddZdefd	d
Z	e
dee fddZe fdededefddZdededdfddZefd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fddZdedd fddZ	d'd eeef d!eddfd"d#Z	d'd eeef d!edd fd$d%ZdS )(r   aS  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                 C   s
   i | _ dS )zXInitialize the Lookups object.

        DOCS: https://spacy.io/api/lookups#init
        N_tablesr)   r!   r!   r"   r+      s   
zLookups.__init__r%   c                 C   s
   |  |S )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%   r!   r!   r"   r;      s   
zLookups.__contains__c                 C   s
   t | jS )z3RETURNS (int): The number of tables in the lookups.)r   rP   rQ   r!   r!   r"   __len__   s   
zLookups.__len__c                 C   s   t | j S )z8RETURNS (List[str]): Names of all tables in the lookups.)listrP   keysrQ   r!   r!   r"   r      s   zLookups.tablesr   c                 C   s6   || j v rttjj|dt||d}|| j|< |S )a  Add 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$   rP   )r)   r%   r   r   r!   r!   r"   r      s
   
	
zLookups.add_tabler   c                 C   s   || j |< dS )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
        NrO   )r)   r%   r   r!   r!   r"   	set_table   s   zLookups.set_tabler:   c                 C   s6   || j vr|tkrttjj|| jd|S | j | S )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   )rP   UNSETKeyErrorr   E159r   r   )r)   r%   r:   r!   r!   r"   	get_table   s
   


zLookups.get_tablec                 C   s,   || j vrttjj|| jd| j |S )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
        rY   )rP   r[   r   r\   r   r   poprS   r!   r!   r"   remove_table   s   
zLookups.remove_tablec                 C   s
   || j v S )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
        rO   rS   r!   r!   r"   rR      s   
zLookups.has_tablec                 K   s   t | jS )zSerialize the lookups to a bytestring.

        RETURNS (bytes): The serialized Lookups.

        DOCS: https://spacy.io/api/lookups#to_bytes
        )r?   r@   rP   )r)   kwargsr!   r!   r"   r>     r7   zLookups.to_bytesrA   c                 K   s2   i | _ t| D ]\}}t||| j |< q
| S )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
        )rP   r?   rB   r=   r$   )r)   rA   r`   r1   r2   r!   r!   r"   rC     s   zLookups.from_byteslookups.binpathfilenamec                 K   s^   t |}| s|  || }|d}||   W d   dS 1 s(w   Y  dS )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writer>   )r)   rb   rc   r`   filepathfile_r!   r!   r"   to_disk  s   
"zLookups.to_diskc                 K   sX   t |}|| }| r*|d}| }W d   n1 s w   Y  | |S | S )a
  Load 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   re   rg   readrC   )r)   rb   rc   r`   ri   rj   r   r!   r!   r"   	from_disk)  s   

zLookups.from_disk)r   N)ra   )rF   rG   rH   rI   r+   rK   rM   r;   rL   rT   propertyr   r   r   r<   r$   r   rX   rZ   r   r]   r_   rR   rN   r>   rC   r   r   rk   rn   r!   r!   r!   r"   r      s<    
	

	


)T)collectionsr   pathlibr   typingr   r   r   r   r   r?   preshed.bloomr	   errorsr   stringsr   utilr   r   r   r   objectrZ   rK   rM   r#   r$   r   r!   r!   r!   r"   <module>   s     v