
    h.                     ,   S SK r S SKJr  S SKJrJrJrJrJrJ	r	J
r
  S SKrS SKrS SKJr  S SKJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSK J!r"  SSK J#r#   " S S5      r$S r%S r&S r'\RP                  " \$\&\'5        \$r)S/r*g)    N)Path)DictIterableIteratorListOptionalSetUnion)ndarray)NumpyOps   )IDSORTHSPACYintify_attr)copy_reg)Errors)SimpleFrozenListensure_path)Vocab   )
SpanGroups)DOCBIN_ALL_ATTRS)Docc            	           \ rS rSrSr\S\" 5       4S\\   S\	S\\
   SS4S	 jjrS\4S
 jrS\
SS4S jrS\S\\
   4S jrSS jrS\4S jrS\SS 4S jrS\\\4   SS4S jrS\\\4   SS 4S jrSrg)DocBin   au  Pack Doc objects for binary serialization.

The DocBin class lets you efficiently serialize the information from a
collection of Doc objects. You can control which information is serialized
by passing a list of attribute IDs, and optionally also specify whether the
user data is serialized. The DocBin is faster and produces smaller data
sizes than pickle, and allows you to deserialize without executing arbitrary
Python code.

The serialization format is gzipped msgpack, where the msgpack object has
the following structure:

{
    "attrs": List[uint64], # e.g. [TAG, HEAD, ENT_IOB, ENT_TYPE]
    "tokens": bytes, # Serialized numpy uint64 array with the token data
    "spans": List[Dict[str, bytes]], # SpanGroups data for each doc
    "spaces": bytes, # Serialized numpy boolean array with spaces data
    "lengths": bytes, # Serialized numpy int32 array with the doc lengths
    "strings": List[str] # List of unique strings in the token data
    "version": str, # DocBin version number
}

Strings for the words, tags, labels etc are represented by 64-bit hashes in
the token data, and every string that occurs at least once is passed via the
strings object. This means the storage is more efficient if you pack more
documents together, because you have less duplication in the strings.

A notable downside to this format is that you can't easily extract just one
document from the DocBin.
Fattrsstore_user_datadocsreturnNc                    U Vs/ s H  n[        U5      PM     nnSU;   a\  U Vs/ s H  n[        U5      b  M  UPM     nn[        [        R                  R	                  SU[
        R                  " 5       S95      Se[        U5      nSU l        U Vs/ s H  oD[        :w  d  M  U[        :w  d  M  UPM     snU l        U R                  R                  S[        5        / U l        / U l        / U l        / U l        / U l        / U l        ['        5       U l        X l        U H  nU R-                  U5        M     gs  snf s  snf s  snf )aN  Create a DocBin object to hold serialized annotations.

attrs (Iterable[str]): List of attributes to serialize. 'orth' and
    'spacy' are always serialized, so they're not required.
store_user_data (bool): Whether to write the `Doc.user_data` to bytes/file.
docs (Iterable[Doc]): Docs to add.

DOCS: https://spacy.io/api/docbin#init
Nr   )dictkeykeysz0.1r   )r   KeyErrorr   E983formatr   r%   sortedversionr   r   r   inserttokensspacescatsspan_groups	user_dataflagssetstringsr   add)selfr   r   r    attr	int_attrs	non_validdocs           Q/home/james-whalen/.local/lib/python3.13/site-packages/spacy/tokens/_serialize.py__init__DocBin.__init__4   s    49954[&5	99*/M%$;t3D%IM""YSXXZ"P y!',OutdduO


!T"%'%' "	(*02!#
!$.CHHSM % :M Ps!   D9D>D>E%E1Ec                 ,    [        U R                  5      $ )z7RETURNS: The number of Doc objects added to the DocBin.)lenr,   )r5   s    r:   __len__DocBin.__len__X   s    4;;    r9   c                 :   UR                  U R                  5      n[        UR                  5      S:X  a   UR	                  UR                  S   S45      nU R
                  R                  U5        UR                  [        5      nUR                  S   UR                  S   :X  d   eUR	                  UR                  S   S45      nU R                  R                  [        R                  " U[        S95        U R                  R                  SUR                  05        U GHZ  nU R                  R                  UR                   5        U R                  R                  UR"                  5        U R                  R                  UR$                  5        U R                  R                  UR&                  5        U R                  R                  [)        UR*                  5      5        U R                  R                  UR,                  5        U R                  R                  UR.                  5        U R                  R                  UR0                  5        U R                  R                  UR2                  5        GM]     U R4                  R                  UR4                  5        U R6                  (       a9  U R8                  R                  [:        R<                  " UR8                  5      5        U R>                  R                  UR@                  RC                  5       5        UR@                  RE                  5        H  u  pVU H  nU R                  R                  URF                  5        URH                  URJ                  RL                  R                  ;   a%  U R                  R                  URN                  5        URP                  URJ                  RL                  R                  ;   d  M  U R                  R                  URR                  5        M     M     g)zAdd a Doc's annotations to the DocBin for serialization.

doc (Doc): The Doc object to add.

DOCS: https://spacy.io/api/docbin#add
r   r   dtypehas_unknown_spacesN)*to_arrayr   r>   shapereshaper,   appendr   r-   numpyasarrayboolr1   rE   r3   r4   texttag_lemma_norm_strmorphdep_	ent_type_
ent_kb_id_ent_id_r.   r   r0   srslymsgpack_dumpsr/   spansto_bytesitemslabel_kb_idr9   vocabkb_id_idid_)r5   r9   arrayr-   tokenr$   groupspans           r:   r4   
DocBin.add\   s    TZZ(u{{q MM5;;q>1"56E5!e${{1~a000a! 455==t<=

/1G1GHIELLUZZ(LLUZZ(LLU\\*LLU[[)LLS-.LLUZZ(LLU__-LLU--.LLU]]+  			"NN!!%"5"5cmm"DE		 2 2 45))//+JC  -::!7!77LL$$T[[177dhhnn444LL$$TXX.  ,rA   r^   c              #     #    U R                    H  nX     M	     U R                  R                  [        5      n[	        [        U R                  5      5       GHl  nU R                  U   nU R                  U   nU R                  U   nUR                  S5      (       a  Sn[        XSS2U4   US9nUR                  U R                  U5      nU R                  U   Ul        U R                  U   (       aJ  U R                  U   [        R                  :w  a)  UR                   R#                  U R                  U   5        OUR                   R%                  5         U[        U R&                  5      :  aM  U R&                  U   b=  [(        R*                  " U R&                  U   SS9n	UR&                  R-                  U	5        Uv   GMo     g7f)a1  Recover Doc objects from the annotations, using the given vocab.
Note that the user data of each doc will be read (if available) and returned,
regardless of the setting of 'self.store_user_data'.

vocab (Vocab): The shared vocab.
YIELDS (Doc): The Doc objects.

DOCS: https://spacy.io/api/docbin#get_docs
rE   N)wordsr-   F)use_list)r3   r   indexr   ranger>   r,   r1   r-   getr   
from_arrayr.   r/   r   _EMPTY_BYTESrY   
from_bytesclearr0   rW   msgpack_loadsupdate)
r5   r^   stringorth_colir1   r,   r-   r9   r0   s
             r:   get_docsDocBin.get_docs   s\     llFM #::##D)s4;;'(AJJqME[[^F(,AFyy-..e!X+#6vFC..V4Cyy|CH"t'7'7':j>U>U'U		$$T%5%5a%89		!3t~~&&4>>!+<+H!//q0AER	$$Y/I# )s   GG	c                 b   U R                   UR                   :w  a<  [        [        R                  R	                  SU R                   UR                   S95      eU R
                  UR
                  :w  a<  [        [        R                  R	                  SU R
                  UR
                  S95      eU R                  R                  UR                  5        U R                  R                  UR                  5        U R                  R                  UR                  5        U R                  R                  UR                  5        U R                  R                  UR                  5        U R                  R                  UR                  5        U R                  R                  UR                  5        g)a2  Extend the annotations of this DocBin with the annotations from
another. Will raise an error if the pre-defined attrs of the two
DocBins don't match, or if they differ in whether or not to store
user data.

other (DocBin): The DocBin to merge into the current bin.

DOCS: https://spacy.io/api/docbin#merge
r   )paramcurrentotherr   N)r   
ValueErrorr   E166r(   r   r,   extendr-   r3   rr   r.   r/   r1   r0   )r5   r{   s     r:   mergeDocBin.merge   s1    ::$""$**EKK"X  5#8#88""+ 00// #   	5<<(5<<(EMM*		$ 1 12

%++&eoo.rA   c           
         U R                    H-  n[        UR                  5      S:X  a  M   UR                  5       e   U R                    Vs/ s H  n[        U5      PM     nnU R                   (       a   [        R                  " U R                   5      O[        R
                  " / 5      nU R                  (       a   [        R                  " U R                  5      O[        R
                  " / 5      nU R                  U R                  UR                  S5      UR                  S5      [        R
                  " USS9R                  S5      [        [        U R                  5      5      U R                  U R                  U R                  S.	nU R                   (       a  U R"                  US'   [$        R&                  " [(        R*                  " U5      5      $ s  snf )zSerialize the DocBin's annotations to a bytestring.

RETURNS (bytes): The serialized DocBin.

DOCS: https://spacy.io/api/docbin#to_bytes
r   Cint32rC   )	r*   r   r,   r-   lengthsr3   r.   r1   r/   r0   )r,   r>   rG   rJ   vstackrK   r-   r*   r   tobyteslistr)   r3   r.   r1   r/   r   r0   zlibcompressrW   rX   )r5   r,   r   r-   msgs        r:   rZ   DocBin.to_bytes   s:    kkFv||$)76<<7) "-1[[9[63v;[9.2kkdkk*u}}R?P.2kkdkk*u}}R?P||ZZnnS)nnS)}}WG<DDSIF4<<01IIZZ++

 #~~C}}U00566! :s   G
bytes_datac                 p    [         R                  " [        R                  " U5      5      nUS   U l        [        US   5      U l
        [        R                  " US   SS9n[        R                  " US   [        S9n[        R                  " US   SS9nUR                  [        U R                  5      -  [        U R                  5      4nUR!                  U5      nUR!                  UR                  S	45      n[#        5       R%                  XS5      U l        [#        5       R%                  XC5      U l        US
   U l        UR-                  SU Vs/ s H  nSPM     sn5      U l        UR-                  SU Vs/ s H  n0 PM     sn5      U l        SU;   a  [3        US   5      U l        OS/[        U 5      -  U l        U R&                   H-  n[        UR6                  5      S:X  a  M   UR6                  5       e   U $ ! [        R                   a    [        [        R                  5      ef = fs  snf s  snf )zDeserialize the DocBin's annotations from a bytestring.

bytes_data (bytes): The data to load from.
RETURNS (DocBin): The loaded DocBin.

DOCS: https://spacy.io/api/docbin#from_bytes
r   r3   r   r   rC   r-   r,   uint64r   r.   r/   rA   r1   r0   Nr   )rW   rq   r   
decompresserrorr|   r   E1014r   r2   r3   rJ   
frombufferrL   sizer>   rH   r   	unflattenr,   r-   r.   rl   r/   r1   r   r0   rG   )	r5   r   r   r   flat_spacesflat_tokensrG   _r,   s	            r:   ro   DocBin.from_bytes   s   	+%%dooj&ABC \
3y>*""3y>A&&s8}DA&&s8}HE!!S_4c$**oF!))%0!));+;+;Q*?@j**;@j**;@K	77=2H132HIWWW7&;7ar7&;<
#!#k"23DN"Vc$i/DNkkFv||$)76<<7) "+ zz 	+V\\**	+ 3I&;s   *G= H.
8H3
=.H+pathc                     [        U5      nUR                  S5       n UR                  U R                  5       5         SSS5        g! [         a    [	        [
        R                  5      ef = f! , (       d  f       g= f)zSave the DocBin to a file (typically called .spacy).

path (str / Path): The file path.

DOCS: https://spacy.io/api/docbin#to_disk
wbN)r   openwriterZ   r|   r   E870r5   r   file_s      r:   to_diskDocBin.to_disk   sa     4 YYt_.DMMO, _  . --. _s   A/A$A,,A//
A=c                     [        U5      nUR                  S5       nU R                  UR                  5       5        SSS5        U $ ! , (       d  f       U $ = f)zLoad the DocBin from a file (typically called .spacy).

path (str / Path): The file path.
RETURNS (DocBin): The loaded DocBin.

DOCS: https://spacy.io/api/docbin#to_disk
rbN)r   r   ro   readr   s      r:   	from_diskDocBin.from_disk
  sD     4 YYt_OOEJJL)  _s    A
A)
r   r.   r1   r-   r/   r   r3   r,   r0   r*   )r{   r   r!   N)__name__
__module____qualname____firstlineno____doc__	ALL_ATTRSr   r   rQ   rL   r   r;   intr?   r4   r   r   rv   r   bytesrZ   ro   r
   r   r   r   __static_attributes__ rA   r:   r   r      s    B  ) %.0	"}" " sm	"
 
"H   $/s $/t $/Le  @/<7% 76U x B.E#t), . .eCI. 8 rA   r   c                     S nU  H8  nUc  M  [        SS9R                  U5      nUc  UnM'  UR                  U5        M:     Ub  UR                  5       $ g)NT)r   rA   )r   ro   r   rZ   )binsmergedbyte_stringdoc_bins       r:   
merge_binsr     sZ    F"T2==kJG~ W%    rA   c                 0    [         U R                  5       44$ N)unpickle_binrZ   )r   s    r:   
pickle_binr   '  s    7++-/00rA   c                 4    [        5       R                  U 5      $ r   )r   ro   )r   s    r:   r   r   +  s    8{++rA   )+r   pathlibr   typingr   r   r   r   r   r	   r
   rJ   rW   r   	thinc.apir   r   r   r   r   r   compatr   errorsr   utilr   r   r^   r   _dict_proxiesr   r9   r   r   r   r   r   r   r   pickleBinder__all__r   rA   r:   <module>r      su      G G G     1 1   0  % . A AH1, 	
L 1	*rA   