
    Ng)                         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	n# e
$ rZefdZ	Y dZ[ndZ[ww xY wd	 Zd
 Z G d de          Zd ZdS )    )ParserI)TreeBllipParser)RerankingParserget_unified_model_parametersc                      d S N r       L/var/www/html/ai-engine/env/lib/python3.11/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HHr   Nc                     	 t          |           D ]\  }}|                    d           d S # 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    	! 	!   :Q : :$ : : :
 
 	s   *. 
AAAc                 N    t          j        t          | j                            S r
   )r   
fromstringstr	ptb_parse)scored_parses    r   _scored_parse_to_nltk_treer"   n   s    ?3|566777r   c                   J    e Zd ZdZ	 	 	 	 	 ddZd Zd Ze	 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                     t                       |pi }|pi }t                      | _         | j        j        |fi | |r|r | j        j        d||d| dS dS 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)
        )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   s    B 	&''''-2+1r"$$""<BB>BBB 	!1 	(DH( "3!1  #    	 	 	 	r   c              #      K   t          |           | j                            |          }|D ]}t          |          V  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!   s       r   r2   zBllipParser.parse   sY       	hX^^H--
& 	; 	;L,\::::::	; 	;r   c              #      K   g }i }t          |          D ]$\  }\  }}|                    |           ||||<   %t          |           | j                            ||          }|D ]}t          |          V  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'   parse_taggedr"   )	r*   word_and_tag_pairsr   tag_mapr   r   tagr4   r!   s	            r   tagged_parsezBllipParser.tagged_parse   s        '(:;; 	! 	!NA{cLL 
eX**5'::
& 	; 	;L,\::::::	; 	;r   c                 F    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   s@    < )33		
&%s&%
 
 	
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                              |          }t          d           d                                }d                                }d                                }|||fD ]x}t          d	d
                    |          z             	 t          |	                    |                    }t          |           Z# t          $ r t          d           Y uw xY wt          |	                    |                    D ]\  }}	t          d||	fz             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)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    .//4I	
+,,,..y99E	'NNN5;;==I4::<<IE	51 $ $nsxx111222	$H--..D$KKKK 	$ 	$ 	$"#####	$ ekk)4455 , ,5oE
*++++ 
$Un =>>??   
8Uo >??@@   
"U~ >??@@    s   81C**DD)nltk.parse.apir   	nltk.treer   __all__bllipparserr   bllipparser.RerankingParserr   r   r   r   r   r"   r   r[   r   r   r   <module>ra      sA   # " " " " "      DL /
I++++++HHHHHH     I I I)+ I I I I I I I I II  8 8 8I
 I
 I
 I
 I
' I
 I
 I
X- - - - -s   " 616