U C^Y@sddlmZddlmZddlmZddlmZmZm Z ddl m Z m Z m Z ddd Zdd d Zd d ZddZddZddZddZdddZdS))unicode_literals) OrderedDict)Printer)DocTokenSpan)ErrorsWarnings user_warningTcCs||d|kst|d|}t|dg}tdd|D}|rn|D]&\}} t| dg} | D] } d|| <q^qFg} |D].\} } | sz| | |rzttjj|| dqz| S) adAnalyze a pipeline component with respect to its position in the current pipeline and the other components. Will check whether requirements are fulfilled (e.g. if previous components assign the attributes). pipeline (list): A list of (name, pipe) tuples e.g. nlp.pipeline. name (unicode): The name of the pipeline component to analyze. pipe (callable): The pipeline component function to analyze. index (int): The index of the component in the pipeline. warn (bool): Show user warning if problem is found. RETURNS (list): The problems found for the given pipeline component. rNrequirescSsg|] }|dfqS)F).0annotr r 1/tmp/pip-install-6_kvzl1k/spacy/spacy/analysis.py sz!analyze_pipes..assignsT)nameattr) AssertionErrorgetattrritemsappendr r ZW025format)pipelinerpipeindexwarnZ prev_pipesZ pipe_requiresr Z prev_nameZ prev_pipeZ prev_assignsrproblemsZ fulfilledr r r analyze_pipes s       rcCs4i}t|D]"\}\}}t|||||d||<q |S)zAnalyze all pipes in the pipeline in order. pipeline (list): A list of (name, pipe) tuples e.g. nlp.pipeline. warn (bool): Show user warning if problem is found. RETURNS (dict): The problems found, keyed by component name. r) enumerater)rrrirrr r ranalyze_all_pipes)sr#cCsZi}|D]L}|}|d}t|D],\}}|t|dk}|||rLdni}q&q|S)zConvert dot notation to a dict. For example: ["token.pos", "token._.xyz"] become {"token": {"pos": True, "_": {"xyz": True }}}. values (iterable): The values to convert. RETURNS (dict): The converted values. .rT)lowersplitr!len setdefault)valuesresultvaluepathpartsr"itemZis_lastr r r dot_to_dict6sr/c st|}tttd}|D]\}dkrfdd|D}dd|D}|rfttjjd |d|krd fdd |D}ttj j|d t |t sttj jd |D] \}}|d kr@|d krttj jdd |D]B\}} | d k rd|} d| d | } ttjj| | dqq|d rhttjj||ddd|d k rd|} d| d |} ttjj| | d|} t| |sttjj|dqq|S)aValidate component attributes provided to "assigns", "requires" etc. Raises error for invalid attributes and formatting. Doesn't check if custom extension attributes are registered, since this is something the user might want to do themselves later in the component. values (iterable): The string attributes to check, e.g. `["token.pos"]`. RETURNS (iterable): The checked attributes. )doctokenspanr2cSsg|]}|dr|qS)zspan. startswithrrr r rrUs z"validate_attrs..cSsg|]}|ds|qS)zspan._.r3r5r r rrVs , )attrsc3s|]}|r|VqdSNr3)raZobj_keyr r Zs z!validate_attrs..)objr7)r_Tz{}._z{}._.{}z{}.{}r$)rZsolutionN)r<r)r/rrrr ValueErrorr ZE180rjoinZE181 isinstancedictZE182ZE183endswithZE184hasattrZE185) r)dataobjsr7Z span_attrsZ invalid_attrsrr+Zext_attrZ ext_valuegoodbadr<r r:rvalidate_attrsGsB         rIcCsD|dks tg}|D]*\}}t||g}||kr|||fq|S)N)rr )rrr)rrZfeaturer*Z pipe_namerZ pipe_assignsr r r_get_feature_for_attrus   rJcCs t||dS)a Get all pipeline components that assign an attr, e.g. "doc.tensor". pipeline (list): A list of (name, pipe) tuples e.g. nlp.pipeline. attr (unicode): The attribute to check. RETURNS (list): (name, pipeline) tuples of components that assign the attr. rrJrrr r rget_assigns_for_attrsrMcCs t||dS)a Get all pipeline components that require an attr, e.g. "doc.tensor". pipeline (list): A list of (name, pipe) tuples e.g. nlp.pipeline. attr (unicode): The attribute to check. RETURNS (list): (name, pipeline) tuples of components that require the attr. r rKrLr r rget_requires_for_attrsrNFcCs,t||d}g}i}t|jD]\\}\}}t|dg} t|dg} t|dd} |||| | | ft|j|||dd||<q|dd} |j|| d d d td d | D} t d d | Dr|d | | D](\}}|rd |}|d ||qn |d|r(||dSdS)aPrint a formatted summary for the current nlp object's pipeline. Shows a table with the pipeline components and why they assign and require, as well as any problems if available. nlp (Language): The nlp object. pretty (bool): Pretty-print the results (color etc). no_print (bool): Don't print anything, just return the data. RETURNS (dict): A dict with "overview" and "problems". )prettyno_printr rZ retokenizesFr zPipeline Overview)#Z ComponentRequiresZAssignsZ RetokenizesT)headerdivider multilinecss|]}t|VqdSr8)r'rpr r rr;sz print_summary..css|] }|VqdSr8r rVr r rr;sz Problems ({})r6z'{}' requirements not met: {}zNo problems found.)overviewrN)rr!rrrrrTtablesumr)anyrrr@rrG)ZnlprOrPmsgrXrr"rrr rZretokrSZ n_problemsZproblemr r r print_summarys,       r]N)T)T)TF) __future__r collectionsrZwasabirtokensrrrerrorsr r r rr#r/rIrJrMrNr]r r r rs     .