/mhb pddlZddlmZddlmZmZddlmZm Z ddl m Z GddeZ Gd d e Z y) N)NDArray)OptionalList)ABCabstractmethod) split_textceZdZdZdeddfdZeedefdZ ed dZ ededefd Z edd e e fd Zedeefd Zy)BaseContextPartitionera A base class for partitioning a context into sources. Attributes: context (str): The context to partition. Methods: num_sources(self) -> int: Property. The number of sources within the context. split_context(self) -> None: Split the context into sources. get_source(self, index: int) -> str: Get a represention of the source corresponding to a given index. get_context(self, mask: Optional[NDArray] = None) -> str: Get a version of the context ablated according to the given mask. sources(self) -> List[str]: Property. A list of all sources within the context. contextreturnNc||_yN)r )selfr s I/Users/shenjiajun/Desktop/2code/2code/context_cite/context_partitioner.py__init__zBaseContextPartitioner.__init__s  cy)zThe number of sources.Nrs r num_sourcesz"BaseContextPartitioner.num_sources rcy)zSplit the context into sources.Nrrs r split_contextz$BaseContextPartitioner.split_context%rrindexcy)z@Get a represention of the source corresponding to a given index.Nrrrs r get_sourcez!BaseContextPartitioner.get_source)rrmaskcy)zAGet a version of the context ablated according to the given mask.Nr)rrs r get_contextz"BaseContextPartitioner.get_context-rrcpt|jDcgc]}|j|c}Scc}w)zA list of all sources.)rangerr)ris rsourceszBaseContextPartitioner.sources1s,-2$2B2B,CDq"DDDs3)r Nr)__name__ __module__ __qualname____doc__strrpropertyrintrrrrrr!rr%rrrr r s(%S%%..OOOOP 1PPEcEErr ceZdZdZddededdffd ZdZedZed Z ede fd Z d e defd Z dd e efdZxZS)SimpleContextPartitionerze A simple context partitioner that splits the context into sources based on a separator. r source_typer Nc@t||||_i|_yr)superrr/_cache)rr r/ __class__s rrz!SimpleContextPartitioner.__init__=s !& rct|j|j\}}}||jd<||jd<y)z9Split text into parts and cache the parts and separators.parts separatorsN)r r r/r2)rr5r6_s rrz&SimpleContextPartitioner.split_contextBs;)$,,8H8HIz1$ G$. L!rcv|jjd|j|jdS)Nr5r2getrrs rr5zSimpleContextPartitioner.partsHs0 ;;??7 # +    {{7##rcv|jjd|j|jdS)Nr6r9rs rr6z#SimpleContextPartitioner.separatorsNs0 ;;??< ( 0    {{<((rc,t|jSr)lenr5rs rrz$SimpleContextPartitioner.num_sourcesTs4::rrc |j|Sr)r5rs rrz#SimpleContextPartitioner.get_sourceXszz%  rrc>|%tj|jt}tj|j |}tj|j |}d}tt||D]\}\}}|dkDr||z }||z }|S)N)dtyper) nponesrboolarrayr6r5 enumeratezip)rrr6r5r r$ separatorparts rr!z$SimpleContextPartitioner.get_context[s <774++48DXXdoo.t4 $T*$-c*e.D$E  A 41u9$ tOG r)sentencer)r&r'r(r)r*rrr+r5r6r,rrrrr! __classcell__)r3s@rr.r.7s #t / $$ )) S!!!  1 rr.)numpyrB numpy.typingrtypingrrabcrrutilsr r r.rrrrQs0 !#,ES,E^.5.r