o
    %ݫi                     @   s6   d Z ddlZddlmZ ddlmZ G dd dZdS )zSDictionary utilities, e.g. synonym dictionaries.

Authors
 * Sylvain de Langen 2024    N)defaultdict)Iterablec                   @   sr   e Zd ZdZdd ZedddZedddZd	ee	 dd
fddZ
de	de	defddZde	defddZd
S )SynonymDictionarya  Loads sets of synonym words and lets you look up if two words are
    synonyms.

    This could, for instance, be used to check for equality in the case of two
    spellings of the same word when normalization might be unsuitable.

    Synonyms are not considered to be transitive:
    If A is a synonym of B and B is a synonym of C, then A is NOT considered a
    synonym of C unless they are added in the same synonym set.c                 C   s   t t| _d S )N)r   setword_map)self r   R/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/utils/dictionaries.py__init__   s   zSynonymDictionary.__init__returnc                 C   sF   t | }t }|D ]}t|tr|| q
tdt| d|S )a  Parses an opened file as JSON, where the top level structure is a
        list of sets of synonyms (i.e. words that are all synonyms with each
        other), e.g. `[ ["hello", "hi"], ["say", "speak", "talk"] ]`.

        Arguments
        ---------
        file : file object
            File object that supports reading (e.g. an `open`ed file)

        Returns
        -------
        SynonymDictionary
            Synonym dictionary frm the parsed JSON file with all synonym sets
            added.
        zUnexpected entry type z! in synonyms JSON (expected list))jsonloadr   
isinstancelistadd_synonym_set
ValueErrortype)filedsynonym_dictentryr   r   r	   from_json_file   s   

z SynonymDictionary.from_json_filec                 C   s<   t | ddd}t|W  d   S 1 sw   Y  dS )a  Opens a file and parses it as JSON, with otherwise the same semantics
        as :meth:`~SynonymDictionary.from_json_file`, which uses an opened file.

        Arguments
        ---------
        path : str
            Path to the JSON file

        Returns
        -------
        SynonymDictionary
            Synonym dictionary frm the parsed JSON file with all synonym sets
            added.
        rutf8)encodingN)openr   r   )pathfr   r   r	   from_json_path8   s   $z SynonymDictionary.from_json_pathwordsNc                 C   s,   t |}|D ]}| j| ||h  qdS )zAdd a set of words that are all synonyms with each other.

        Arguments
        ---------
        words : Iterable[str]
            List of words that should be defined as synonyms to each otherN)r   r   update)r   r   word_setwordr   r   r	   r   K   s   z!SynonymDictionary.add_synonym_setabc                 C   s   ||kp
|| j | v S )a  Check for the equality or synonym equality of two words.

        Arguments
        ---------
        a : str
            First word to compare. May be outside of the known dictionary.
        b : str
            Second word to compare. May be outside of the known dictionary.
            The order of arguments does not matter.

        Returns
        -------
        bool
            Whether `a` and `b` should be considered synonyms. Not transitive,
            see the main class documentation.)r   )r   r#   r$   r   r   r	   __call__X   s   zSynonymDictionary.__call__r"   c                 C   s   | j |t S )a  Returns the set of synonyms for a given word.

        Arguments
        ---------
        word : str
            The word to look up the synonyms of. May be outside of the known
            dictionary.

        Returns
        -------
        set of str
            Set of known synonyms for this word. Do not mutate (or copy it
            prior). May be empty if the word has no known synonyms.)r   getr   )r   r"   r   r   r	   get_synonyms_fork   s   z"SynonymDictionary.get_synonyms_for)r   r   )__name__
__module____qualname____doc__r
   staticmethodr   r   r   strr   boolr%   r   r'   r   r   r   r	   r      s    
r   )r+   r   collectionsr   typingr   r   r   r   r   r	   <module>   s
    