JL i)ddlmZddlmZ dgZ ddlmZddlmZdZ d Z d Z Gd deZd Zy#e $rZ e fdZ YdZ [ $dZ [ wwxYw) )ParserI)Tree BllipParser)RerankingParserget_unified_model_parameterscyNr V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/parse/bllip.py_ensure_bllip_import_or_errorrZs r ctd|z)Nz&Couldn't import bllipparser module: %s) ImportError)ies r rr_sBRGHHr Nc t|D]\}}|jdy#t$r}tddd|d}~wwxYw)NasciizToken z (zH) is non-ASCII. BLLIP Parser currently doesn't support non-ASCII inputs.) enumerateencodeUnicodeEncodeError ValueError)wordsiwordes r _ensure_asciircsd ' !GAt KK  ! QCr$": :  s$' AAAcRtjt|jSr )r fromstringstr ptb_parse) scored_parses r _scored_parse_to_nltk_treer"ns ??3|556 77r cBeZdZdZ ddZdZdZe ddZy) rz 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. Nct|xsi}|xsi}t|_|jj|fi||r#|r |jjd||d|yyy)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_filenameweights_filenameNr )rrrrpload_parser_modelload_reranker_model)self parser_modelreranker_featuresreranker_weightsparser_optionsreranker_optionss r __init__zBllipParser.__init__yszB &''-2+1r"$"""<B>B !1 (DHH ( ( "3!1 # "2 r c#Kt||jj|}|D]}t|yw)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)rr'parser")r*sentence nbest_listr!s r r2zBllipParser.parses< hXX^^H- & ;L,\: : ;s<>c#Kg}i}t|D]!\}\}}|j|||||<#t||jj ||}|D]}t |yw)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)rappendrr' parse_taggedr") r*word_and_tag_pairsrtag_maprrtagr4r!s r tagged_parsezBllipParser.tagged_parses '(:; !NA{c LL    ! eXX**5': & ;L,\: : ;s ,A2AA2c8t|\}}}||||||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_dirreranker_features_filenamereranker_weights_filenames r from_unified_model_dirz"BllipParser.from_unified_model_dirs6< ) 3   & %  & %     r )NNNNN)NN) __name__ __module__ __qualname____doc__r0r2r; classmethodrBr r r rrrs? -^;$;8>B$ $ r c ddlm}|dj}tdtj |}tddj }dj }dj }|||fD]E}td d j|z t|j|}t|Gt|j|D]\}} td || fztd t|jddgtdt|jddgtdt|jddgy#t$rtd YwxYw)z8This assumes the Python module bllipparser is installed.r)findzmodels/bllip_wsj_no_auxzLoading BLLIP Parsing models...zDone.z#British left waffles on Falklands .z"I saw the man with the telescope .z # ! ? : -z Sentence: %r z(parse failed)z parse %d: %szforcing 'tree' to be 'NN':)AN)treeNNz.forcing 'A' to be 'DT' and 'tree' to be 'NNP':)rKDT)rLNNPzforcing 'A' to be 'NNP':)rKrO)rLNN) nltk.datarIpathprintrrBsplitjoinnextr2 StopIterationrr;) rIr>bllip sentence1 sentence2fail1r3rLrr2s r demor[sj ./44I +,  . .y 9E 'N5;;=I4::? 8 U  o > ?@  " U  ~ > ?@% $ " # $s%E&&E>=E>)nltk.parse.apir nltk.treer__all__ bllipparserrbllipparser.RerankingParserrrrrrr"rr[r r r rasp#DL / I+H 8I 'I X-CI)+IIIs6A AA