a
     ˜ëjG  ã                   @   sV   d dl Z d dlmZ d dlmZ d dlmZ G dd„ deƒZdd„ Ze	d	krReƒ  dS )
é    N)Údefaultdict)Úreduce)ÚCorpusReaderc                       s^   e Zd ZdZe d¡Zedd„ ƒZd‡ fdd„	Z	dd	d
„Z
ddd„Zddd„Zdd„ Z‡  ZS )ÚLinThesaurusCorpusReaderzEWrapper for the LISP-formatted thesauruses distributed by Dekang Lin.z \("?([^"]+)"? \(desc [0-9.]+\).+c                   C   s   t tƒS )z6Factory for creating defaultdict of defaultdict(dict)s)r   Údict© r   r   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/lin.pyZ__defaultdict_factory   s    z.LinThesaurusCorpusReader.__defaultdict_factoryç        c              	      sÜ   t ƒ  |d¡ ttjƒ| _|| _| jddd�D ]¨\}}}t|ƒ�†}d}|D ]l}| 	¡ }|rntj
 d|¡}	d}qJ|dkr|d}qJ| d¡}
t|
ƒdkrJ|
\}}t|ƒ| j| |	 | 	d	¡< qJW d
  ƒ q.1 sÌ0    Y  q.d
S )a  
        Initialize the thesaurus.

        :param root: root directory containing thesaurus LISP files
        :type root: C{string}
        :param badscore: the score to give to words which do not appear in each other's sets of synonyms
        :type badscore: C{float}
        zsim[A-Z]\.lspT)Zinclude_encodingZinclude_fileidz\1Fz))ú	é   ú"N)ÚsuperÚ__init__r   r   Ú._LinThesaurusCorpusReader__defaultdict_factoryÚ
_thesaurusÚ	_badscoreZabspathsÚopenÚstripÚ_key_reÚsubÚsplitÚlenÚfloat)ÚselfÚrootZbadscoreÚpathÚencodingÚfileidZlin_fileÚfirstÚlineÚkeyZ
split_lineÚngramZscore©Ú	__class__r   r   r      s*    
ÿ

ÿz!LinThesaurusCorpusReader.__init__Nc                    sl   ˆ ˆkr"|rdS dd„ ˆj D ƒS nF|rPˆˆj| ˆ  v rJˆj| ˆ  ˆ S ˆjS ‡ ‡‡fdd„ˆj D ƒS dS )aÝ  
        Returns the similarity score for two ngrams.

        :param ngram1: first ngram to compare
        :type ngram1: C{string}
        :param ngram2: second ngram to compare
        :type ngram2: C{string}
        :param fileid: thesaurus fileid to search in. If None, search all fileids.
        :type fileid: C{string}
        :return: If fileid is specified, just the score for the two ngrams; otherwise,
                 list of tuples of fileids and scores.
        ç      ð?c                 S   s   g | ]}|d f‘qS )r$   r   ©Ú.0Zfidr   r   r   Ú
<listcomp>Q   ó    z7LinThesaurusCorpusReader.similarity.<locals>.<listcomp>c                    s:   g | ]2}|ˆˆj | ˆ  v r.ˆj | ˆ  ˆ nˆjf‘qS r   )r   r   r%   ©Úngram1Úngram2r   r   r   r'   Z   s   	ùÿûN)Ú_fileidsr   r   )r   r*   r+   r   r   r)   r   Ú
similarity?   s    ÿý	÷z#LinThesaurusCorpusReader.similarityc                    s0   |rˆj | ˆ   ¡ S ‡ ‡fdd„ˆjD ƒS dS )a   
        Returns a list of scored synonyms (tuples of synonyms and scores) for the current ngram

        :param ngram: ngram to lookup
        :type ngram: C{string}
        :param fileid: thesaurus fileid to search in. If None, search all fileids.
        :type fileid: C{string}
        :return: If fileid is specified, list of tuples of scores and synonyms; otherwise,
                 list of tuples of fileids and lists, where inner lists consist of tuples of
                 scores and synonyms.
        c                    s"   g | ]}|ˆj | ˆ   ¡ f‘qS r   )r   Úitems©r&   r   ©r!   r   r   r   r'   u   s   ÿz<LinThesaurusCorpusReader.scored_synonyms.<locals>.<listcomp>N)r   r.   r,   ©r   r!   r   r   r0   r   Úscored_synonymsf   s
    þz(LinThesaurusCorpusReader.scored_synonymsc                    s0   |rˆj | ˆ   ¡ S ‡ ‡fdd„ˆjD ƒS dS )aœ  
        Returns a list of synonyms for the current ngram.

        :param ngram: ngram to lookup
        :type ngram: C{string}
        :param fileid: thesaurus fileid to search in. If None, search all fileids.
        :type fileid: C{string}
        :return: If fileid is specified, list of synonyms; otherwise, list of tuples of fileids and
                 lists, where inner lists contain synonyms.
        c                    s"   g | ]}|ˆj | ˆ   ¡ f‘qS r   )r   Úkeysr/   r0   r   r   r'   ˆ   s   ÿz5LinThesaurusCorpusReader.synonyms.<locals>.<listcomp>N)r   r3   r,   r1   r   r0   r   Úsynonymsz   s
    þz!LinThesaurusCorpusReader.synonymsc                    s   t ‡ ‡fdd„ˆjdƒS )zÔ
        Determines whether or not the given ngram is in the thesaurus.

        :param ngram: ngram to lookup
        :type ngram: C{string}
        :return: whether the given ngram is in the thesaurus.
        c                    s   | pˆ ˆj | v S )N)r   )Úaccumr   r0   r   r   Ú<lambda>–   r(   z7LinThesaurusCorpusReader.__contains__.<locals>.<lambda>F)r   r,   )r   r!   r   r0   r   Ú__contains__�   s
    ýz%LinThesaurusCorpusReader.__contains__)r	   )N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler   Ústaticmethodr   r   r-   r2   r4   r7   Ú__classcell__r   r   r"   r   r      s   

$
'

r   c                  C   s®   ddl m}  d}d}td| ƒ t|  |¡ƒ td| ƒ t|  |¡ƒ td| ƒ t| j|dd	�ƒ td| ƒ t| j|dd	�ƒ td
|› d|› d�ƒ t|  ||¡ƒ d S )Nr   )Úlin_thesaurusZbusinessZ
enterprisezGetting synonyms for zGetting scored synonyms for z5Getting synonyms from simN.lsp (noun subsection) for zsimN.lsp)r   zSimilarity score for z and ú:)Znltk.corpusr@   Úprintr4   r2   r-   )ZthesZword1Zword2r   r   r   Údemo¡   s    rC   Ú__main__)
r<   Úcollectionsr   Ú	functoolsr   Znltk.corpus.readerr   r   rC   r8   r   r   r   r   Ú<module>   s    