a
    ±Kbâ*  ã                
   @   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 n0 e
yr Z zefdd„Z	W Y d	Z[n
d	Z[0 0 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	   ú/lib/python3.9/site-packages/nltk/parse/bllip.pyÚ_ensure_bllip_import_or_errorZ   s    r   c                 C   s   t d|  ƒ‚d S )Nz&Couldn't import bllipparser module: %s)ÚImportError)Úier	   r	   r
   r   `   s    Nc              
   C   sb   z t | ƒD ]\}}| d¡ q
W n< ty\ } z$td|› d|›dƒ|‚W Y d }~n
d }~0 0 d S )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_asciid   s    ÿýr   c                 C   s   t  t| jƒ¡S r   )r   Z
fromstringÚstrZ	ptb_parse)Úscored_parser	   r	   r
   Ú_scored_parse_to_nltk_treeo   s    r   c                   @   s8   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   sV   t ƒ  |pi }|pi }tƒ | _| jj|fi |¤Ž |rR|rR| jjf ||dœ|¤Ž 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_filenameN)r   r   ÚrrpZload_parser_modelZload_reranker_model)ÚselfZparser_modelZreranker_featuresZreranker_weightsÚparser_optionsÚreranker_optionsr	   r	   r
   Ú__init__z   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   sf   g }i }t |ƒD ]&\}\}}| |¡ |dur|||< qt|ƒ | j ||¡}|D ]}t|ƒV  qRd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   s   s        ú
/ ÿc            
   	   C   s  ddl m}  | dƒj}tdƒ t |¡}tdƒ d ¡ }d ¡ }d ¡ }|||fD ]L}td	d
 |¡ ƒ zt| 	|¡ƒ}t|ƒ W qR t
yœ   tdƒ Y qR0 qRt| 	|¡ƒD ]\}}	td||	f ƒ q®t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   H" 