-
Notifications
You must be signed in to change notification settings - Fork 2.2k
refactor: split segmenter into its own base class #1632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36d135a
refactor: split segmenter into its own base class
0b1f7c2
Merge branch 'master' of github.com:jina-ai/jina into refactor-split-…
eeac9d2
Merge branch 'master' of github.com:jina-ai/jina into refactor-split-…
865904b
Merge branch 'master' of github.com:jina-ai/jina into refactor-split-…
f129593
fix: level depth test should not depend on hub executor
713becf
fix: fix integration mime tests
f41974b
fix: skip hub list test
2e24c3c
Merge branch 'master' into refactor-split-segmenter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,39 @@ | ||
__copyright__ = "Copyright (c) 2020 Jina AI Limited. All rights reserved." | ||
__license__ = "Apache-2.0" | ||
|
||
from typing import Tuple, Optional | ||
|
||
from . import BaseExecutableDriver | ||
from ..types.document import Document | ||
|
||
if False: | ||
from .. import DocumentSet | ||
|
||
|
||
class SegmentDriver(BaseExecutableDriver): | ||
"""Drivers inherited from this Driver will bind :meth:`segment` by default """ | ||
|
||
def __init__( | ||
self, | ||
executor: Optional[str] = None, | ||
method: str = 'segment', | ||
traversal_paths: Tuple[str] = ('r',), | ||
*args, | ||
**kwargs | ||
): | ||
super().__init__(executor, method, traversal_paths=traversal_paths, *args, **kwargs) | ||
|
||
def _apply_all(self, docs: 'DocumentSet', *args, **kwargs): | ||
for doc in docs: | ||
_args_dict = doc.get_attrs(*self.exec.required_keys) | ||
ret = self.exec_fn(**_args_dict) | ||
if ret: | ||
self.update(doc, ret) | ||
|
||
@staticmethod | ||
def update(doc, ret): | ||
for r in ret: | ||
with Document(length=len(ret), **r) as c: | ||
if not c.mime_type: | ||
c.mime_type = doc.mime_type | ||
doc.chunks.append(c) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,26 @@ | ||
import inspect | ||
from typing import Dict, List | ||
|
||
from .. import BaseExecutor | ||
from ...helper import typename | ||
|
||
|
||
class BaseSegmenter(BaseExecutor): | ||
""":class:`BaseSegmenter` works on doc-level, | ||
it chunks Documents into set of Chunks """ | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.required_keys = {k for k in inspect.getfullargspec(self.segment).args if k != 'self'} | ||
if not self.required_keys: | ||
self.logger.warning(f'{typename(self)} works on keys, but no keys are specified') | ||
|
||
def segment(self, *args, **kwargs) -> List[Dict]: | ||
"""The apply function of this executor. | ||
Unlike :class:`BaseCrafter`, the :func:`craft` here works on doc-level info and the output is defined on | ||
chunk-level. Therefore the name of the arguments should be always valid keys defined | ||
in the doc-level protobuf whereas the output dict keys should always be valid keys defined in the chunk-level | ||
protobuf. | ||
:return: a list of chunks-level info represented by a dict | ||
""" | ||
raise NotImplementedError |
This file contains hidden or 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from jina.executors.crafters import BaseSegmenter | ||
from jina.executors.crafters import BaseCrafter | ||
from .helper import helper_function | ||
|
||
|
||
class CustomCrafter1(BaseSegmenter): | ||
class CustomCrafter1(BaseCrafter): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
print(helper_function) |
This file contains hidden or 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from .helper import helper_function | ||
|
||
from jina.executors.crafters import BaseSegmenter | ||
from jina.executors.crafters import BaseCrafter | ||
|
||
|
||
class CustomCrafter2(BaseSegmenter): | ||
class CustomCrafter2(BaseCrafter): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
print(helper_function) |
This file contains hidden or 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from jinahub.helper import helper_function | ||
|
||
from jina.executors.crafters import BaseSegmenter | ||
from jina.executors.crafters import BaseCrafter | ||
|
||
|
||
class CustomCrafter3(BaseSegmenter): | ||
class CustomCrafter3(BaseCrafter): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
print(helper_function) |
This file contains hidden or 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from .helper import helper_function | ||
|
||
from jina.executors.crafters import BaseSegmenter | ||
from jina.executors.crafters import BaseCrafter | ||
|
||
|
||
class CustomCrafter4(BaseSegmenter): | ||
class CustomCrafter4(BaseCrafter): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
print(helper_function) |
This file contains hidden or 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
This file contains hidden or 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,37 @@ | ||
import re | ||
import string | ||
from typing import Dict, List | ||
|
||
from jina.executors.segmenters import BaseSegmenter | ||
|
||
|
||
class DummySentencizer(BaseSegmenter): | ||
|
||
def __init__(self, | ||
*args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
punct_chars = [','] | ||
self._slit_pat = re.compile('\s*([^{0}]+)(?<!\s)[{0}]*'.format(''.join(set(punct_chars)))) | ||
|
||
def segment(self, text: str, *args, **kwargs) -> List[Dict]: | ||
""" | ||
Split the text into sentences. | ||
|
||
:param text: the raw text | ||
:return: a list of chunk dicts with the cropped images | ||
""" | ||
results = [] | ||
ret = [(m.group(0), m.start(), m.end()) for m in | ||
re.finditer(self._slit_pat, text)] | ||
if not ret: | ||
ret = [(text, 0, len(text))] | ||
for ci, (r, s, e) in enumerate(ret): | ||
f = ''.join(filter(lambda x: x in string.printable, r)) | ||
f = re.sub('\n+', ' ', f).strip() | ||
f = f[:100] | ||
if len(f) > 3: | ||
results.append(dict( | ||
text=f | ||
)) | ||
return results |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -6,4 +6,4 @@ requests: | |
[SearchRequest, TrainRequest, IndexRequest]: | ||
- !SegmentDriver | ||
with: | ||
method: craft | ||
method: segment |
This file contains hidden or 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 |
---|---|---|
|
@@ -6,4 +6,4 @@ requests: | |
[SearchRequest, TrainRequest, IndexRequest]: | ||
- !SegmentDriver | ||
with: | ||
method: craft | ||
method: segment |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont see the need of changing
github_1546
test case, but changing also does no harmThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was failing the test, I think is because Segmenter does not have craft anymore as method