B
    P?ð[¼*  ã            
   @   s²   d dl mZ d dlmZ d dlmZ dgZy$d dlmZ d dl	m
Z
 dd„ ZW n. ek
r| Z zefd	d„ZW d
d
Z[X Y nX dd„ Zdd„ ZG dd„ deƒZdd„ Zdd„ Zd
S )é    )Úprint_function)ÚParserI)ÚTreeÚBllipParser)ÚRerankingParser)Úget_unified_model_parametersc               C   s   d S )N© r   r   r   ú/lib/python3.7/site-packages/nltk/parse/bllip.pyÚ_ensure_bllip_import_or_error\   s    r
   c             C   s   t d|  ƒ‚d S )Nz&Couldn't import bllipparser module: %s)ÚImportError)Úier   r   r	   r
   b   s    Nc             C   sN   y$xt | ƒD ]\}}| d¡ qW W n$ tk
rH   td||f ƒ‚Y nX d S )NÚasciizTToken %d (%r) is non-ASCII. BLLIP Parser currently doesn't support non-ASCII inputs.)Ú	enumerateÚdecodeÚUnicodeDecodeErrorÚ
ValueError)ÚwordsÚiÚwordr   r   r	   Ú_ensure_asciif   s    r   c             C   s   t  t| jƒ¡S )N)r   Z
fromstringÚstrZ	ptb_parse)Úscored_parser   r   r	   Ú_scored_parse_to_nltk_treeq   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   sR   t ƒ  |pi }|pi }tƒ | _| jj|f|Ž |rN|rN| 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__|   s    !zBllipParser.__init__c             c   s0   t |ƒ | j |¡}x|D ]}t|ƒV  qW 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   sn   g }i }x2t |ƒD ]&\}\}}| |¡ |dk	r|||< qW t|ƒ | j ||¡}x|D ]}t|ƒV  qXW 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   u   s       
)c        
   	   C   s  ddl m}  | dƒj}tdƒ t |¡}tdƒ d ¡ }d ¡ }d ¡ }x\|||fD ]N}td	d
 |¡ ƒ yt| 	|¡ƒ}t|ƒ W qT t
k
r    tdƒ Y qTX qTW x*t| 	|¡ƒD ]\}}	td||	f ƒ q¶W 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':)r.   ZDT)r/   ÚNNPzforcing 'A' to be 'NNP':)r.   r0   )r/   NN)Z	nltk.datar,   ÚpathÚprintr   r&   ÚsplitÚjoinÚnextr   ÚStopIterationr   r#   )
r,   r%   ZbllipZ	sentence1Z	sentence2Zfail1r   r/   r   r   r   r   r	   Údemo  s4    

r7   c             C   s8   ddl m} y
tƒ  W n tk
r2   |dƒ‚Y nX d S )Nr   )ÚSkipTestzZdoctests from nltk.parse.bllip are skipped because the bllipparser module is not installed)Znoser8   r
   r   )Úmoduler8   r   r   r	   Úsetup_module1  s    
r:   )Z
__future__r   Znltk.parse.apir   Z	nltk.treer   Ú__all__Zbllipparserr   Zbllipparser.RerankingParserr   r
   r   r   r   r   r   r7   r:   r   r   r   r	   Ú<module>	   s   H 0