3
d*                 @   s   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
rp Z zefddZ	W Y d	d	Z[X nX 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   0/tmp/pip-build-v9q4h5k9/nltk/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   sd   y$xt | D ]\}}|jd qW W n: tk
r^ } ztd| d|d|W Y d d }~X nX 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 jt| jS )N)r   
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   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__z   s    !zBllipParser.__init__c             c   s0   t | | j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 ]&\}\}}|j| |dk	r|||< qW t| | j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   s   s       
)c        
      C   s  ddl m}  | dj}td tj|}td dj }dj }dj }x\|||fD ]N}td	d
j|  yt|j	|}t| W qT t
k
r   td Y qTX qTW x*t|j	|D ]\}}	td||	f  qW tdt|jddg tdt|jddg tdt|j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treeNNz.forcing 'A' to be 'DT' and 'tree' to be 'NNP':DTNNPzforcing 'A' to be 'NNP':)r/   N)r0   r1   )r/   r2   )r0   r3   )r/   r3   )r0   N)Z	nltk.datar-   pathprintr   r'   splitjoinnextr   StopIterationr   r$   )
r-   r&   ZbllipZ	sentence1Z	sentence2Zfail1r    r0   r   r   r   r   r   demo   s4    

r:   )Znltk.parse.apir   Z	nltk.treer   __all__Zbllipparserr   Zbllipparser.RerankingParserr   r	   r
   r   r   r   r   r:   r   r   r   r   <module>	   s   H 