ó <¿CVc@sCdZddlmZmZddlmZmZddlmZm Z m Z m Z ddl m Z ddlmZddlmZmZmZmZmZmZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#ed efd „ƒYƒZ$d efd „ƒYZ%d efd„ƒYZ&de fd„ƒYZ'de#fd„ƒYZ(de"fd„ƒYZ)defd„ƒYZ*de!fd„ƒYZ+defd„ƒYZ,eƒe(ƒe)ƒe'ƒgZ-eƒe,ƒe*ƒe'ƒgZ.eƒe,ƒe+ƒe'ƒgZ/defd„ƒYZ0de0fd„ƒYZ1de0fd „ƒYZ2d!e0fd"„ƒYZ3d#e%fd$„ƒYZ4d%„Z5e6e6e6e6d&e0d'd(„Z7d)„Z8e9d*kr?dd+l:m;Z;e7ƒe<ƒe;d,ƒZ=e0e=d-d.ƒZ>d/Z?e?j@ƒZAe>jBeAƒZCxeCD]ZDe<eDƒq(Wnd0S(1u` Extension of chart parsing implementation to handle grammars with feature structures as nodes. iÿÿÿÿ(tprint_functiontunicode_literals(txrangetpython_2_unicode_compatible(t FeatStructtunifytTYPEtfind_variables(tlogic(tTree(t Nonterminalt ProductiontCFGtFeatStructNonterminaltis_nonterminalt is_terminal( tTreeEdgetChartt ChartParsertEdgeItFundamentalRulet LeafInitRuletEmptyPredictRuletBottomUpPredictRuletSingleEdgeFundamentalRuletBottomUpPredictCombineRuletCachedTopDownPredictRuletTopDownInitRuletFeatureTreeEdgecBseeZdZdd d„Zed„ƒZd d„Zd„Zd„Z d„Z d„Z d „Z RS( u A specialized tree edge that allows shared variable bindings between nonterminals on the left-hand side and right-hand side. Each ``FeatureTreeEdge`` contains a set of ``bindings``, i.e., a dictionary mapping from variables to values. If the edge is not complete, then these bindings are simply stored. However, if the edge is complete, then the constructor applies these bindings to every nonterminal in the edge whose symbol implements the interface ``SubstituteBindingsI``. icCs·|dkri}n|t|ƒkrm|rm|j||ƒ}g|D]}|j||ƒ^qF}i}ntj|||||ƒ||_|jtt|j ƒƒƒf|_dS(uz Construct a new edge. If the edge is incomplete (i.e., if ``dot€su%s %s(t is_completeRt __unicode__tjoinR$R!R%(R&R+((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt__str__|s   N( t__name__t __module__t__doc__RR t staticmethodR/R5RR:R+R@RF(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyR#s      t FeatureChartcBs>eZdZd„Zd„Zd„Zd„Zed„ZRS(uQ A Chart for feature grammars. :see: ``Chart`` for more information. c s’ˆikrtˆjƒStˆjƒƒ}t|ƒ}|ˆjkrVˆj|ƒnt‡‡fd†|Dƒƒ}tˆj|j|gƒƒS(u¦ Returns an iterator over the edges in this chart. See ``Chart.select`` for more information about the ``restrictions`` on the edges. c3s"|]}ˆjˆ|ƒVqdS(N(t_get_type_if_possible(RAtkey(t restrictionsR&(si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pys ¢s(titert_edgesR$R>R#t_indexest _add_indextget(R&RNt restr_keystvals((RNR&si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pytselect‘s   cs”x0|D](}tt|ƒstd|ƒ‚qqWi}ˆj|‰t‡‡fd†|Dƒƒ}|j|gƒjˆƒqNWdS(u‹ A helper function for ``select``, which creates a new index for a given set of attributes (aka restriction keys). uBad restriction: %sc3s*|] }ˆjtˆ|ƒƒƒVqdS(N(RLtgetattr(RARM(tedgeR&(si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pys µsN(thasattrRt ValueErrorRQRPR#t setdefaulttappend(R&RTRMR.RU((RXR&si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRR¦s  cs\xUˆjjƒD]D\}}t‡‡fd†|Dƒƒ}|j|gƒjˆƒqWdS(us A helper function for ``insert``, which registers the new edge with all existing indexes. c3s*|] }ˆjtˆ|ƒƒƒVqdS(N(RLRW(RARM(RXR&(si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pys ¿sN(RQR%R#R[R\(R&RXRTR.RU((RXR&si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt_register_with_indexes¹s cCs+t|tƒr#t|kr#|tS|SdS(u” Helper function which returns the ``TYPE`` feature of the ``item``, if it exists, otherwise it returns the ``item`` itself N(R6tdictR(R&RB((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRLÃsccsœx•|jddd|jƒD]x}t|tƒr|jƒt|tkrt|jƒ|dtƒrx+|j|dtd|ƒD] }|Vq‚WqqWdS(Ntstartitendt rename_varstcompletet tree_class( RVt _num_leavesR6RR(RRtTruettrees(R&R_RcRXttree((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pytparsesÍs ""( RGRHRIRVRRR]RLR Rh(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRK‹s    tFeatureFundamentalRulecBseZdZd„ZRS(u¢ A specialized version of the fundamental rule that operates on nonterminals whose symbols are ``FeatStructNonterminal``s. Rather tha simply comparing the nonterminals for equality, they are unified. Variable bindings from these unifications are collected and stored in the chart using a ``FeatureTreeEdge``. When a complete edge is generated, these bindings are applied to all nonterminals in the edge. The fundamental rule states that: - ``[A -> alpha \* B1 beta][i:j]`` - ``[B2 -> gamma \*][j:k]`` licenses the edge: - ``[A -> alpha B3 \* beta][i:j]`` assuming that B1 and B2 can be unified to generate B3. c csB|jƒ|jƒko<|jƒo<|jƒo<t|tƒsCdS|jƒ}|jƒ}t|tƒrít|ƒszdS|jƒt |jƒt krždS|j ƒ}|j d|j ƒƒ}t |||dtƒ}|dkr dSn||krýdS|j ƒ}|j|jƒ|ƒ} |j| ||ƒr>| VndS(Nt used_varsRa(R`R_t is_incompleteRCR6RR(R9RRR+trename_variablesR@RtFalseRR5tinsert_with_backpointer( R&tcharttgrammart left_edget right_edgetfoundR9R+tresulttnew_edge((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pytapplyðs.          (RGRHRIRv(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRiÛst FeatureSingleEdgeFundamentalRulecBs)eZdZeƒZd„Zd„ZRS(uõ A specialized version of the completer / single edge fundamental rule that operates on nonterminals whose symbols are ``FeatStructNonterminal``s. Rather than simply comparing the nonterminals for equality, they are unified. ccsj|j}xZ|jd|jƒdtd|jƒƒD].}x%|j||||ƒD] }|VqSWq4WdS(NR`RCR9(t_fundamental_ruleRVR_RmR(Rv(R&RoRpRrtfrRqRu((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt_apply_completes  ccsj|j}xZ|jd|jƒdtd|jƒƒD].}x%|j||||ƒD] }|VqSWq4WdS(NR_RCR((RxRVR`ReR9Rv(R&RoRpRqRyRrRu((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt_apply_incomplete"s  (RGRHRIRiRxRzR{(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRws  tFeatureTopDownInitRulecBseZd„ZRS(ccsSxL|jd|jƒƒD]2}tj|dƒ}|j|dƒr|VqqWdS(NR(i((t productionsR_RR/tinsert(R&RoRptprodRu((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRv0s(RGRHRv(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyR|/stFeatureTopDownPredictRulecBseZdZd„ZRS(uÖ A specialized version of the (cached) top down predict rule that operates on nonterminals whose symbols are ``FeatStructNonterminal``s. Rather than simply comparing the nonterminals for equality, they are unified. The top down expand rule states that: - ``[A -> alpha \* B1 beta][i:j]`` licenses the edge: - ``[B2 -> \* gamma][j:j]`` for each grammar production ``B2 -> gamma``, assuming that B1 and B2 can be unified. c cso|jƒrdS|jƒ|jƒ}}t|ƒs9dS|jƒ}|jj||fdƒ}|d|kr„|d|kr„dSxË|jd|ƒD]·}|j ƒrþ|j ƒd} t | ƒrþ||j ƒkrÝq—n| |j |ƒkrûq—qûqþnt |jƒ|dtƒr—tj||jƒƒ} |j| dƒrN| VqNq—q—W||f|j||fös(R^R(R@(R&RX((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyRŸõs (RGRHRIR R›R~RœRŸ(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyR—Ès     cCsddlm}|jdƒS(Niÿÿÿÿ(tFeatureGrammaru  S -> NP VP PP -> Prep NP NP -> NP PP VP -> VP PP VP -> Verb NP VP -> Verb NP -> Det[pl=?x] Noun[pl=?x] NP -> "John" NP -> "I" Det -> "the" Det -> "my" Det[-pl] -> "a" Noun[-pl] -> "dog" Noun[-pl] -> "cookie" Verb -> "ate" Verb -> "saw" Prep -> "with" Prep -> "under" (t nltk.grammarR¥t fromstring(R¥((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt demo_grammarÿsiu$I saw John with a dog with my cookiecCsddl}ddl}tƒtƒ} |rBt| ƒtƒntd|jƒ|rhtd|ƒn|jƒ} |jƒ} || d|ƒ} | j| ƒ} t| j | j ƒƒƒ}|rÝtd|jƒ| ƒn|rx.|D]}t|ƒqêWntdt |ƒƒdS(Niÿÿÿÿu*u Sentence:ttraceuTime: %su Nr trees:( tsysttimetprintR¨RGtsplittclockt chart_parseR=RhR_R(t print_timest print_grammart print_treestprint_sentenceR©tparsertsentRªR«RpR˜tttcpRoRfRg((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pytdemos(      cCsyddl}|jddƒddl}|jdƒ}|jƒjddƒjdƒ|jƒjddƒjdƒdS(Niÿÿÿÿufor i in range(1): demo()u/tmp/profile.oututimeucumi<(tprofiletruntpstatstStatst strip_dirst sort_statst print_stats(R¹R»tp((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt run_profile0s   u__main__(tloadu!grammars/book_grammars/feat0.fcfgR©iuKim likes childrenN(ERIt __future__RRt nltk.compatRRtnltk.featstructRRRRtnltk.semRt nltk.treeR R¦R R R R RRtnltk.parse.chartRRRRRRRRRRRRRRKRiRwR|R€R‡R‰RŠR“R•R‘RŒR’R”R–R—R¨ReR¸RÁRGt nltk.dataRÂR¬RpR·RµR­R˜tparseRfRg(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/featurechart.pyt sh".R gP67       7