Skip to content

Commit

Permalink
#542 added universal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Jan 7, 2024
1 parent 91a23e5 commit f0fe4a9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions arekit/common/text/partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from collections import Iterable

from arekit.common.bound import Bound
from arekit.common.text.partitioning.base import BasePartitioning


class Partitioning(BasePartitioning):
""" NOTE: considering that provided parts
has no intersections between each other
"""

list_reg_types = {
"str": lambda p, item: p.append(item),
"list": lambda p, item: p.extend(item)
}

def __init__(self, text_fmt):
assert(isinstance(text_fmt, str) and text_fmt in self.list_reg_types)
self.__reg_part = self.list_reg_types[text_fmt]

def provide(self, text, parts_it):
assert(isinstance(parts_it, Iterable))

parts = []
start = 0

for value, bound in parts_it:
assert(isinstance(bound, Bound))
assert(bound.Position >= start)

# Release everything till the current value position.
self.__reg_part(p=parts, item=text[start:bound.Position])

# Release the entity value.
parts.extend([value])

start = bound.Position + bound.Length

# Release everything after the last entity.
self.__reg_part(p=parts, item=text[start:len(text)])

return parts

0 comments on commit f0fe4a9

Please sign in to comment.