-
Notifications
You must be signed in to change notification settings - Fork 191
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
4 changed files
with
111 additions
and
3 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .key_value_grouper import KeyValueGrouper | ||
from .naive_grouper import NaiveGrouper | ||
from .naive_reverse_grouper import NaiveReverseGrouper | ||
|
||
__all__ = ['NaiveGrouper', 'KeyValueGrouper'] | ||
__all__ = ['KeyValueGrouper', 'NaiveGrouper', 'NaiveReverseGrouper'] |
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,26 @@ | ||
from ..base_op import OPERATORS, Grouper, convert_dict_list_to_list_dict | ||
|
||
|
||
@OPERATORS.register_module('naive_reverse_grouper') | ||
class NaiveReverseGrouper(Grouper): | ||
"""Split one batched sample to samples. """ | ||
|
||
def __init__(self, *args, **kwargs): | ||
""" | ||
Initialization method. | ||
:param args: extra args | ||
:param kwargs: extra args | ||
""" | ||
super().__init__(*args, **kwargs) | ||
|
||
def process(self, dataset): | ||
|
||
if len(dataset) == 0: | ||
return dataset | ||
|
||
samples = [] | ||
for sample in dataset: | ||
samples.append(convert_dict_list_to_list_dict(sample)) | ||
|
||
return samples |
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,83 @@ | ||
import unittest | ||
|
||
from data_juicer.core.data import NestedDataset as Dataset | ||
from data_juicer.ops.grouper.naive_reverse_grouper import NaiveReverseGrouper | ||
from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase | ||
|
||
|
||
class NaiveReverseGrouperTest(DataJuicerTestCaseBase): | ||
|
||
def _run_helper(self, op, samples, target): | ||
dataset = Dataset.from_list(samples) | ||
new_dataset = op.run(dataset) | ||
|
||
for d, t in zip(new_dataset, target): | ||
self.assertEqual(d['text'], t['text']) | ||
|
||
def test_one_batched_sample(self): | ||
|
||
source = [ | ||
{ | ||
'text':[ | ||
"Today is Sunday and it's a happy day!", | ||
"Sur la plateforme MT4, plusieurs manières d'accéder à \n" | ||
'ces fonctionnalités sont conçues simultanément.', | ||
'欢迎来到阿里巴巴!' | ||
] | ||
} | ||
] | ||
|
||
target = [ | ||
{ | ||
'text': "Today is Sunday and it's a happy day!" | ||
}, | ||
{ | ||
'text': | ||
"Sur la plateforme MT4, plusieurs manières d'accéder à \n" | ||
'ces fonctionnalités sont conçues simultanément.' | ||
}, | ||
{ | ||
'text': '欢迎来到阿里巴巴!' | ||
}, | ||
] | ||
|
||
op = NaiveReverseGrouper() | ||
self._run_helper(op, source, target) | ||
|
||
|
||
def test_two_batch_sample(self): | ||
|
||
source = [ | ||
{ | ||
'text':[ | ||
"Today is Sunday and it's a happy day!", | ||
"Sur la plateforme MT4, plusieurs manières d'accéder à \n" | ||
'ces fonctionnalités sont conçues simultanément.' | ||
] | ||
}, | ||
{ | ||
'text':[ | ||
'欢迎来到阿里巴巴!' | ||
] | ||
} | ||
] | ||
|
||
target = [ | ||
{ | ||
'text': "Today is Sunday and it's a happy day!" | ||
}, | ||
{ | ||
'text': | ||
"Sur la plateforme MT4, plusieurs manières d'accéder à \n" | ||
'ces fonctionnalités sont conçues simultanément.' | ||
}, | ||
{ | ||
'text': '欢迎来到阿里巴巴!' | ||
}, | ||
] | ||
|
||
op = NaiveReverseGrouper() | ||
self._run_helper(op, source, target) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |