
    g)                         d dl mZ d dlmZ 	 dgZ	 d dlmZ d dlmZ d Z	d	 Zd
 Z G d de      Zd Zy# e
$ rZefdZ	Y dZ[$dZ[ww xY w)    )ParserI)TreeBllipParser)RerankingParserget_unified_model_parametersc                       y N r       E/var/www/openai/venv/lib/python3.12/site-packages/nltk/parse/bllip.py_ensure_bllip_import_or_errorr   Z   s    r   c                     t        d| z        )Nz&Couldn't import bllipparser module: %s)ImportError)ies    r   r   r   _   s    BRGHHr   Nc                     	 t        |       D ]  \  }}|j                  d        y # t        $ r}t        d dd      |d }~ww xY 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es       r   _ensure_asciir   c   s_     'GAtKK  ( QCr$ ": :
 	s   $' 	AAAc                 R    t        j                  t        | j                              S r
   )r   
fromstringstr	ptb_parse)scored_parses    r   _scored_parse_to_nltk_treer"   n   s    ??3|55677r   c                   B    e Zd ZdZ	 	 	 	 	 ddZd Zd Ze	 dd       Zy)	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                     t                |xs i }|xs i }t               | _         | j                  j                  |fi | |r#|r  | j                  j                  d||d| yyy)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)
        )features_filenameweights_filenameNr   )r   r   rrpload_parser_modelload_reranker_model)selfparser_modelreranker_featuresreranker_weightsparser_optionsreranker_optionss         r   __init__zBllipParser.__init__y   sz    B 	&''-2+1r"$"""<B>B!1(DHH(( "3!1 # "2r   c              #      K   t        |       | j                  j                  |      }|D ]  }t        |        yw)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!   s       r   r2   zBllipParser.parse   s7      	hXX^^H-
&L,\:: 's   <>c              #      K   g }i }t        |      D ]!  \  }\  }}|j                  |       ||||<   # t        |       | j                  j	                  ||      }|D ]  }t        |        yw)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'   parse_taggedr"   )	r*   word_and_tag_pairsr   tag_mapr   r   tagr4   r!   s	            r   tagged_parsezBllipParser.tagged_parse   sy       '(:;NA{cLL 
 <
 	eXX**5':
&L,\:: 's   ,A2AA2c                 8    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/   parser_model_dirreranker_features_filenamereranker_weights_filenames          r   from_unified_model_dirz"BllipParser.from_unified_model_dir   s6    < )3		
&%&%
 	
r   )NNNNN)NN)	__name__
__module____qualname____doc__r0   r2   r;   classmethodrB   r   r   r   r   r   r   s?     -^;$;8 >B$
 $
r   c            	         ddl m}   | d      j                  }t        d       t        j                  |      }t        d       dj                         }dj                         }dj                         }|||fD ]E  }t        d	d
j                  |      z         	 t        |j                  |            }t        |       G t        |j                  |            D ]  \  }}	t        d||	fz          t        dt        |j                  ddg                   t        dt        |j                  ddg                   t        dt        |j                  ddg                   y# t        $ r t        d       Y w xY w)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':)rK   DT)rL   NNPzforcing 'A' to be 'NNP':)rK   rO   )rL   NN)	nltk.datarI   pathprintr   rB   splitjoinnextr2   StopIterationr   r;   )
rI   r>   bllip	sentence1	sentence2fail1r3   rL   r   r2   s
             r   demor[      s`    ./44I	
+,..y9E	'N5;;=I4::<IE	51nsxx112	$H-.D$K	 2 ekk)455oE
*+ 6 
$Un =>? 
8Uo >?@ 
"U~ >?@%  	$"#	$s   %E&&E>=E>)nltk.parse.apir   	nltk.treer   __all__bllipparserr   bllipparser.RerankingParserr   r   r   r   r   r"   r   r[   r   r   r   <module>ra      sp    # DL /
I+H8I
' I
X-C  I)+ I IIs   6 A
AA
