o
    rZh)                  
   @   s   d dl mZ d dlmZ 	 dgZzd dlmZ d dlmZ dd Z	W n e
y: Z zefddZ	W Y d	Z[nd	Z[ww d
d Zdd ZG dd deZdd Zd	S )    )ParserI)TreeBllipParser)RerankingParserget_unified_model_parametersc                   C   s   d S N r	   r	   r	   ?/var/www/auris/lib/python3.10/site-packages/nltk/parse/bllip.py_ensure_bllip_import_or_errorZ   s   r   c                 C   s   t d|  )Nz&Couldn't import bllipparser module: %s)ImportError)ier	   r	   r
   r   _   s   Nc              
   C   sT   zt | D ]	\}}|d qW d S  ty) } ztd| d|d|d }~ww )NasciizToken z (zH) is non-ASCII. BLLIP Parser currently doesn't support non-ASCII inputs.)	enumerateencodeUnicodeEncodeError
ValueError)wordsiworder	   r	   r
   _ensure_asciic   s   r   c                 C   s   t t| jS r   )r   Z
fromstringstrZ	ptb_parse)scored_parser	   r	   r
   _scored_parse_to_nltk_treen   s   r   c                   @   sD   e Zd ZdZ					dddZdd Zdd Ze	dd	d
ZdS )r   z
    Interface for parsing with BLLIP Parser. BllipParser objects can be
    constructed with the ``BllipParser.from_unified_model_dir`` class
    method or manually using the ``BllipParser`` constructor.
    Nc                 C   s^   t   |pi }|p
i }t | _| jj|fi | |r+|r-| jjd||d| dS dS dS )a  
        Load a BLLIP Parser model from scratch. You'll typically want to
        use the ``from_unified_model_dir()`` class method to construct
        this object.

        :param parser_model: Path to parser model directory
        :type parser_model: str

        :param reranker_features: Path the reranker model's features file
        :type reranker_features: str

        :param reranker_weights: Path the reranker model's weights file
        :type reranker_weights: str

        :param parser_options: optional dictionary of parser options, see
            ``bllipparser.RerankingParser.RerankingParser.load_parser_options()``
            for more information.
        :type parser_options: dict(str)

        :param reranker_options: optional
            dictionary of reranker options, see
            ``bllipparser.RerankingParser.RerankingParser.load_reranker_model()``
            for more information.
        :type reranker_options: dict(str)
        )Zfeatures_filenameZweights_filenameNr	   )r   r   rrpZload_parser_modelZload_reranker_model)selfZparser_modelZreranker_featuresZreranker_weightsparser_optionsreranker_optionsr	   r	   r
   __init__y   s   !
zBllipParser.__init__c                 c   s.    t | | j|}|D ]}t|V  qdS )a  
        Use BLLIP Parser to parse a sentence. Takes a sentence as a list
        of words; it will be automatically tagged with this BLLIP Parser
        instance's tagger.

        :return: An iterator that generates parse trees for the sentence
            from most likely to least likely.

        :param sentence: The sentence to be parsed
        :type sentence: list(str)
        :rtype: iter(Tree)
        N)r   r   parser   )r   sentence
nbest_listr   r	   r	   r
   r       s   zBllipParser.parsec           	      c   sh    g }i }t |D ]\}\}}|| |dur|||< q	t| | j||}|D ]}t|V  q*dS )a  
        Use BLLIP to parse a sentence. Takes a sentence as a list of
        (word, tag) tuples; the sentence must have already been tokenized
        and tagged. BLLIP will attempt to use the tags provided but may
        use others if it can't come up with a complete parse subject
        to those constraints. You may also specify a tag as ``None``
        to leave a token's tag unconstrained.

        :return: An iterator that generates parse trees for the sentence
            from most likely to least likely.

        :param sentence: Input sentence to parse as (word, tag) pairs
        :type sentence: list(tuple(str, str))
        :rtype: iter(Tree)
        N)r   appendr   r   Zparse_taggedr   )	r   Zword_and_tag_pairsr   Ztag_mapr   r   tagr"   r   r	   r	   r
   tagged_parse   s   
zBllipParser.tagged_parsec                 C   s   t |\}}}| |||||S )a(  
        Create a ``BllipParser`` object from a unified parsing model
        directory. Unified parsing model directories are a standardized
        way of storing BLLIP parser and reranker models together on disk.
        See ``bllipparser.RerankingParser.get_unified_model_parameters()``
        for more information about unified model directories.

        :return: A ``BllipParser`` object using the parser and reranker
            models in the model directory.

        :param model_dir: Path to the unified model directory.
        :type model_dir: str
        :param parser_options: optional dictionary of parser options, see
            ``bllipparser.RerankingParser.RerankingParser.load_parser_options()``
            for more information.
        :type parser_options: dict(str)
        :param reranker_options: optional dictionary of reranker options, see
            ``bllipparser.RerankingParser.RerankingParser.load_reranker_model()``
            for more information.
        :type reranker_options: dict(str)
        :rtype: BllipParser
        r   )cls	model_dirr   r   Zparser_model_dirZreranker_features_filenameZreranker_weights_filenamer	   r	   r
   from_unified_model_dir   s   z"BllipParser.from_unified_model_dir)NNNNN)NN)	__name__
__module____qualname____doc__r   r    r%   classmethodr(   r	   r	   r	   r
   r   r   s    
/c            
   	   C   s  ddl m}  | dj}td t|}td d }d }d }|||fD ]%}td	d
|  zt|	|}t| W q) t
yN   td Y q)w t|	|D ]\}}	td||	f  qVtdt|ddg tdt|ddg tdt|ddg dS )z8This assumes the Python module bllipparser is installed.r   )findzmodels/bllip_wsj_no_auxzLoading BLLIP Parsing models...zDone.z#British left waffles on Falklands .z"I saw the man with the telescope .z	# ! ? : -zSentence: %r z(parse failed)zparse %d:
%szforcing 'tree' to be 'NN':)AN)treeZNNz.forcing 'A' to be 'DT' and 'tree' to be 'NNP':)r0   ZDT)r1   NNPzforcing 'A' to be 'NNP':)r0   r2   )r1   NN)Z	nltk.datar.   pathprintr   r(   splitjoinnextr    StopIterationr   r%   )
r.   r'   ZbllipZ	sentence1Z	sentence2Zfail1r!   r1   r   r    r	   r	   r
   demo   s<   

r9   )Znltk.parse.apir   Z	nltk.treer   __all__Zbllipparserr   Zbllipparser.RerankingParserr   r   r   r   r   r   r   r9   r	   r	   r	   r
   <module>   s"   F 