/usr/local/lib64/python3.6/site-packages/tokenizers/tools/__pycache__
NameSizeModeActions
visualizer.cpython-36.pyc115130644editdlrm
__init__.cpython-36.pyc2260644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/tokenizers/tools/__pycache__/visualizer.cpython-36.pyc (11513B)
3 Eg9 @sddlZddlZddlZddlmZmZmZmZmZm Z m Z ddl m Z ddlmZddl mZmZejjeZejjedZeeZejZWdQRXGdddZeeZeeeZGd d d e ZGd d d ZGd ddZGdddZ efee!e!dddZ"dS)N)ListOptionalTupleDictCallableAny NamedTuple)Template)r) TokenizerEncodingzvisualizer-styles.cssc@s,eZdZUeeeeeedddZdS) Annotation)startendlabelcCs||_||_||_dS)N)r rr)selfr rrrG/usr/local/lib64/python3.6/site-packages/tokenizers/tools/visualizer.py__init__szAnnotation.__init__N) __name__ __module__ __qualname__intr rrstrrrrrrr s r c@seZdZUeeeedS) CharStateKeyN)rrrrrtoken_ixanno_ixrrrrrs rc@sDeZdZUeeddZeddZeddZ e ddd Z d S) CharStatecCs||_d|_g|_dS)N)char_ixrtokens)rrrrrr'szCharState.__init__cCst|jdkr|jdSdS)Nr)lenr)rrrrr-szCharState.token_ixcCst|jdkS)zJ BPE tokenizers can output more than one token for a char )rr)rrrr is_multitoken1szCharState.is_multitoken)returncCst|j|jdS)N)rr)rrr)rrrr partition_key8szCharState.partition_keyN) rrrrrrrpropertyrr!rr#rrrrr$s   rc@s eZdZdS)AlignedN)rrrrrrrr%?sr%c@seZdZdZejdejdZdee e e e ge fdddZgdfeee e e ed d d Zeeeeefd d dZeeeeedddZeeeeedddZeeeedddZeeeeeedddZdS)EncodingVisualizera Build an EncodingVisualizer Args: tokenizer (:class:`~tokenizers.Tokenizer`): A tokenizer instance default_to_notebook (:obj:`bool`): Whether to render html output in a notebook by default annotation_converter (:obj:`Callable`, `optional`): An optional (lambda) function that takes an annotation in any format and returns an Annotation object z(.{1})?(unk|oov)(.{1})?)flagsTN) tokenizerdefault_to_notebookannotation_convertercCsZ|rDyddlm}m}Wn*tk rB}ztdWYdd}~XnX||_||_||_dS)Nr)displayHTMLzWe couldn't import IPython utils for html display. Are you running in a notebook? You can also pass `default_to_notebook=False` to get back raw HTML )IPython.core.displayr+r, ImportError Exceptionr(r)annotation_coverter)rr(r)r*r+r,errrrVszEncodingVisualizer.__init__)text annotationsr)r"c Cs|j}|dk r|}|rVyddlm}m}Wn*tk rT}ztdWYdd}~XnX|jdk rptt|j|}|j j |}t j |||} |r||| n| SdS)a Build a visualization of the given text Args: text (:obj:`str`): The text to tokenize annotations (:obj:`List[Annotation]`, `optional`): An optional list of annotations of the text. The can either be an annotation class or anything else if you instantiated the visualizer with a converter function default_to_notebook (:obj:`bool`, `optional`, defaults to `False`): If True, will render the html in a notebook. Otherwise returns an html string. Returns: The HTML string if default_to_notebook is False, otherwise (default) returns None and renders the HTML in the notebook Nr)r+r,zeWe couldn't import IPython utils for html display. Are you running in a notebook?) r)r-r+r,r.r/r0listmapr(encoder&_EncodingVisualizer__make_html) rr2r3r)Zfinal_default_to_notebookr+r,r1encodinghtmlrrr__call__ls   zEncodingVisualizer.__call__)r3r"c Cst|dkriSttdd|}t|}td|}|dkrBd}d}d}d}i}x4t|D](}d |d |d |d ||<||7}q\W|S) a Generates a color palette for all the labels in a given set of annotations Args: annotations (:obj:`Annotation`): A list of annotations Returns: :obj:`dict`: A dictionary mapping labels to colors in HSL format rcSs|jS)N)r)xrrrsz;EncodingVisualizer.calculate_label_colors.. @ zhsl(,z%,%)rsetr5rsorted) r3labelsZ num_labelsZh_stepslhcolorsrrrrcalculate_label_colorss    z)EncodingVisualizer.calculate_label_colors)consecutive_chars_listr2r8cCs2|d}|jdkr*|j|j}d|dS|d}|j}|jd}|||}g} i} |jdk r| jd|jrz| jd|jdr| jd n | jd tjj|j|jdk r| jd |j|j| d <n | jd ddj| d} d} x*| j D]\} }| d| d|d7} qWd| d| d|dS)a Converts a list of "consecutive chars" into a single HTML element. Chars are consecutive if they fall under the same word, token and annotation. The CharState class is a named tuple with a "partition_key" method that makes it easy to compare if two chars are consecutive. Args: consecutive_chars_list (:obj:`List[CharState]`): A list of CharStates that have been grouped together text (:obj:`str`): The original text being processed encoding (:class:`~tokenizers.Encoding`): The encoding returned from the tokenizer Returns: :obj:`str`: The HTML span for a set of consecutive chars rNz(r tokenz multi-tokenz odd-tokenz even-tokenz special-tokenZstokz non-tokenzclass=" "z data-z="zz) rrrappendr!r&unk_token_regexsearchjoinitems)rLr2r8firstZstokenlastr rZ span_textZ css_classesZ data_itemscssdatakeyvalrrrconsecutive_chars_to_htmls6             z,EncodingVisualizer.consecutive_chars_to_html)r2r8r3r"cCs\tj|||}|dg}|dj}g}tj|}|dj}|dk rp||} | j} || } |jd| d| dx|ddD]} | j}||kr|jtj|||d| g}|dk r|jd|dk r||} | j} || } |jd| d| d|}| j|djkr|j| q~|jtj|||d| g}q~W|jtj|||dt|} | S)Nrz&r )r2r8z) r&%_EncodingVisualizer__make_char_statesrrKrrSr^r#HTMLBody)r2r8r3 char_statesZcurrent_consecutive_charsZ prev_anno_ixZspansZlabel_colors_dictZ cur_anno_ixannorcolorcsresrrrZ __make_htmlsX          zEncodingVisualizer.__make_html)r2r3r"cCsHdgt|}x4t|D](\}}xt|j|jD] }|||<q0WqW|S)a Args: text (:obj:`str`): The raw text we want to align to annotations (:obj:`AnnotationList`): A (possibly empty) list of annotations Returns: A list of length len(text) whose entry at index i is None if there is no annotation on charachter i or k, the index of the annotation that covers index i where k is with respect to the list of annotations N)r enumerateranger r)r2r3annotation_maprairrrZ__make_anno_mapCs z"EncodingVisualizer.__make_anno_mapc Cstj||}ddtt|D}xTt|jD]F\}}|j|}|dk r.|\}} x"t|| D]} || jj|q\Wq.Wxt|D]\} } | || _qW|S)a For each character in the original text, we emit a tuple representing it's "state": * which token_ix it corresponds to * which word_ix it corresponds to * which annotation_ix it corresponds to Args: text (:obj:`str`): The raw text we want to align to annotations (:obj:`List[Annotation]`): A (possibly empty) list of annotations encoding: (:class:`~tokenizers.Encoding`): The encoding returned from the tokenizer Returns: :obj:`List[CharState]`: A list of CharStates, indicating for each char in the text what it's state is cSsg|] }t|qSr)r).0rrrr ssz9EncodingVisualizer.__make_char_states..N) r&"_EncodingVisualizer__make_anno_maprgrrfrZtoken_to_charsrSr) r2r8r3rhrarrMoffsetsr rrjrrrrrZ__make_char_statesXs  z%EncodingVisualizer.__make_char_states)TN)rrr__doc__recompile IGNORECASErTr boolrrrr rrAnnotationListr: staticmethodrrKrrr r^r7PartialIntListrmr_rrrrr&Cs("(CCr&)childrenr"cCsdj|}d|d|dS)a[ Generates the full html with css from a list of html spans Args: children (:obj:`List[str]`): A list of strings, assumed to be html elements css_styles (:obj:`str`, `optional`): Optional alternative implementation of the css Returns: :obj:`str`: An HTML string with style markup rQz?
z4
)rV)rwZ css_stylesZ children_textrrrr`s r`)#os itertoolsrptypingrrrrrrrstringr Z tokenizersr r pathdirname__file__rVZ css_filenameopenfreadrZr rtrrvrrr%r&rr`rrrrs($      ?