ó
ù`]c           @  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 „  Z Wn e k
 r‡ Z e d „ Z n Xd	 „  Z d
 „  Z d e f d „  ƒ  YZ d „  Z d „  Z d S(   iÿÿÿÿ(   t   print_function(   t   ParserI(   t   Treet   BllipParser(   t   RerankingParser(   t   get_unified_model_parametersc           C  s   d  S(   N(    (    (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   _ensure_bllip_import_or_error\   s    c         C  s   t  d |  ƒ ‚ d  S(   Ns&   Couldn't import bllipparser module: %s(   t   ImportError(   t   ie(    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyR   b   s    c         C  s\   y. x' t  |  ƒ D] \ } } | j d ƒ q WWn' t k
 rW t d | | f ƒ ‚ n Xd  S(   Nt   asciisT   Token %d (%r) is non-ASCII. BLLIP Parser currently doesn't support non-ASCII inputs.(   t	   enumeratet   decodet   UnicodeDecodeErrort
   ValueError(   t   wordst   it   word(    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   _ensure_asciif   s    c         C  s   t  j t |  j ƒ ƒ S(   N(   R   t
   fromstringt   strt	   ptb_parse(   t   scored_parse(    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   _scored_parse_to_nltk_treeq   s    c           B  sM   e  Z d  Z d d d d d d „ Z d „  Z d „  Z e d d d „ ƒ Z RS(   sÑ   
    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.
    c         C  sm   t  ƒ  | p i  } | p i  } t ƒ  |  _ |  j j | |  | ri | ri |  j j d | d | |  n  d S(   s´  
        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)
        t   features_filenamet   weights_filenameN(   R   R   t   rrpt   load_parser_modelt   load_reranker_model(   t   selft   parser_modelt   reranker_featurest   reranker_weightst   parser_optionst   reranker_options(    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   __init__|   s    !c         c  s<   t  | ƒ |  j j | ƒ } x | D] } t | ƒ Vq# Wd S(   s›  
        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   t   parseR   (   R   t   sentencet
   nbest_listR   (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyR#   «   s    
c   	      c  s”   g  } i  } xF t  | ƒ D]8 \ } \ } } | j | ƒ | d k	 r | | | <q q Wt | ƒ |  j j | | ƒ } x | D] } t | ƒ Vq{ Wd S(   sœ  
        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
   t   appendt   NoneR   R   t   parse_taggedR   (	   R   t   word_and_tag_pairsR   t   tag_mapR   R   t   tagR%   R   (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   tagged_parse½   s    
c         C  s+   t  | ƒ \ } } } |  | | | | | ƒ S(   s  
        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   (   t   clst	   model_dirR    R!   t   parser_model_dirt   reranker_features_filenamet   reranker_weights_filename(    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   from_unified_model_dirÙ   s    N(	   t   __name__t
   __module__t   __doc__R'   R"   R#   R,   t   classmethodR2   (    (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyR   u   s   )		c    
      C  s|  d d l  m }  |  d ƒ j } t d ƒ t j | ƒ } t d ƒ d j ƒ  } d j ƒ  } d j ƒ  } xo | | | f D]^ } t d	 d
 j | ƒ ƒ y# t | j	 | ƒ ƒ } t | ƒ Wqv t
 k
 rÓ t d ƒ qv Xqv Wx7 t | j	 | ƒ ƒ D]  \ } }	 t d | |	 f ƒ qî Wt d t | j d d g ƒ ƒ ƒ t d t | j d d g ƒ ƒ ƒ t d t | j d d g ƒ ƒ ƒ d S(   s8   This assumes the Python module bllipparser is installed.iÿÿÿÿ(   t   finds   models/bllip_wsj_no_auxs   Loading BLLIP Parsing models...s   Done.s#   British left waffles on Falklands .s"   I saw the man with the telescope .s	   # ! ? : -s   Sentence: %rt    s   (parse failed)s   parse %d:
%ss   forcing 'tree' to be 'NN':t   At   treet   NNs.   forcing 'A' to be 'DT' and 'tree' to be 'NNP':t   DTt   NNPs   forcing 'A' to be 'NNP':N(   R9   N(   R:   R;   (   R9   R<   (   R:   R=   (   R9   R=   (   R:   N(   t	   nltk.dataR7   t   patht   printR   R2   t   splitt   joint   nextR#   t   StopIterationR
   R,   R'   (
   R7   R.   t   bllipt	   sentence1t	   sentence2t   fail1R$   R:   R   R#   (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   demo  s4    

"c         C  s?   d d l  m } y t ƒ  Wn t k
 r: | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTestsZ   doctests from nltk.parse.bllip are skipped because the bllipparser module is not installed(   t   noseRJ   R   R   (   t   moduleRJ   (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   setup_module1  s    N(   t
   __future__R    t   nltk.parse.apiR   t	   nltk.treeR   t   __all__t   bllipparserR   t   bllipparser.RerankingParserR   R   R   R   R   R   R   RI   RM   (    (    (    s/   lib/python2.7/site-packages/nltk/parse/bllip.pyt   <module>	   s   H			Œ	0