-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |