-
Notifications
You must be signed in to change notification settings - Fork 10
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
10 changed files
with
233 additions
and
4 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
30 changes: 30 additions & 0 deletions
30
frame_semantic_transformer/data/augmentations/DataAugmentation.py
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,30 @@ | ||
from __future__ import annotations | ||
from abc import ABC, abstractmethod | ||
from random import uniform | ||
|
||
|
||
class DataAugmentation(ABC): | ||
""" | ||
Base class for data augmentations on training data | ||
""" | ||
|
||
probability: float | ||
|
||
def __init__(self, probability: float): | ||
self.probability = probability | ||
|
||
def __call__(self, input: str, output: str) -> tuple[str, str]: | ||
""" | ||
randomly apply this augmentation in proportion to self.probability | ||
""" | ||
rand_val = uniform(0, 1.0) | ||
if rand_val > self.probability: | ||
return (input, output) | ||
return self.apply_augmentation(input, output) | ||
|
||
@abstractmethod | ||
def apply_augmentation(self, input: str, output: str) -> tuple[str, str]: | ||
""" | ||
Main logic for subclasses to implement | ||
""" | ||
pass |
14 changes: 14 additions & 0 deletions
14
frame_semantic_transformer/data/augmentations/LowercaseAugmentation.py
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,14 @@ | ||
from __future__ import annotations | ||
from .DataAugmentation import DataAugmentation | ||
|
||
|
||
class LowercaseAugmentation(DataAugmentation): | ||
def apply_augmentation(self, input: str, output: str) -> tuple[str, str]: | ||
task_def_index = input.find(":") | ||
task_def = input[:task_def_index] | ||
input_contents = input[task_def_index:] | ||
# only lowercase the content, not the task definition | ||
return ( | ||
task_def + input_contents.lower(), | ||
output.lower(), | ||
) |
25 changes: 25 additions & 0 deletions
25
frame_semantic_transformer/data/augmentations/RemoveContractionsAugmentation.py
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,25 @@ | ||
from __future__ import annotations | ||
from .DataAugmentation import DataAugmentation | ||
import re | ||
|
||
|
||
def remove_contractions(text: str) -> str: | ||
new_text = text.replace("won't", "will not") | ||
new_text = new_text.replace("can't", "cannot") | ||
new_text = re.sub(r"n't(\b)", r" not\1", new_text) | ||
new_text = re.sub(r"'ll(\b)", r" will\1", new_text) | ||
new_text = re.sub(r"'m(\b)", r" am\1", new_text) | ||
new_text = re.sub(r"'re(\b)", r" are\1", new_text) | ||
new_text = re.sub(r"'ve(\b)", r" have\1", new_text) | ||
return new_text | ||
|
||
|
||
class RemoveContractionsAugmentation(DataAugmentation): | ||
def apply_augmentation(self, input: str, output: str) -> tuple[str, str]: | ||
if "*'" in input or "*'" in output or "*n'" in input or "*n'" in output: | ||
return (input, output) | ||
|
||
return ( | ||
remove_contractions(input), | ||
remove_contractions(output), | ||
) |
13 changes: 13 additions & 0 deletions
13
frame_semantic_transformer/data/augmentations/RemoveEndPunctuationAugmentation.py
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,13 @@ | ||
from __future__ import annotations | ||
from .DataAugmentation import DataAugmentation | ||
import re | ||
|
||
REMOVE_END_PUNCT_RE = r"\s*[.?!]\s*$" | ||
|
||
|
||
class RemoveEndPunctuationAugmentation(DataAugmentation): | ||
def apply_augmentation(self, input: str, output: str) -> tuple[str, str]: | ||
return ( | ||
re.sub(REMOVE_END_PUNCT_RE, "", input), | ||
re.sub(REMOVE_END_PUNCT_RE, "", output), | ||
) |
17 changes: 17 additions & 0 deletions
17
frame_semantic_transformer/data/augmentations/chain_augmentations.py
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,17 @@ | ||
from __future__ import annotations | ||
from typing import Callable, Sequence | ||
|
||
from .DataAugmentation import DataAugmentation | ||
|
||
|
||
def chain_augmentations( | ||
augmentations: Sequence[DataAugmentation], | ||
) -> Callable[[str, str], tuple[str, str]]: | ||
def chained_augmentation(input: str, output: str) -> tuple[str, str]: | ||
chained_input = input | ||
chained_output = output | ||
for augmentation in augmentations: | ||
chained_input, chained_output = augmentation(chained_input, chained_output) | ||
return chained_input, chained_output | ||
|
||
return chained_augmentation |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import annotations | ||
import pytest | ||
|
||
from frame_semantic_transformer.data.augmentations.LowercaseAugmentation import ( | ||
LowercaseAugmentation, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,expected", | ||
[ | ||
( | ||
("TASK: I am a banana.", "I am a banana."), | ||
("TASK: i am a banana.", "i am a banana."), | ||
), | ||
( | ||
("TASK: I AM A BANANA !", "I AM A BANANA !"), | ||
("TASK: i am a banana !", "i am a banana !"), | ||
), | ||
( | ||
("TASK | Param1 | Param 2 : I AM A BANANA !", "I AM A BANANA !"), | ||
("TASK | Param1 | Param 2 : i am a banana !", "i am a banana !"), | ||
), | ||
], | ||
) | ||
def test_LowercaseAugmentation( | ||
input: tuple[str, str], expected: tuple[str, str] | ||
) -> None: | ||
augmentation = LowercaseAugmentation(1.0) | ||
assert augmentation(*input) == expected |
36 changes: 36 additions & 0 deletions
36
tests/data/augmentations/test_RemoveContractionsAugmentation.py
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,36 @@ | ||
from __future__ import annotations | ||
import pytest | ||
|
||
from frame_semantic_transformer.data.augmentations.RemoveContractionsAugmentation import ( | ||
RemoveContractionsAugmentation, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,expected", | ||
[ | ||
( | ||
("TASK: I can't go I won't go", "I can't go I won't go"), | ||
("TASK: I cannot go I will not go", "I cannot go I will not go"), | ||
), | ||
( | ||
( | ||
"TASK: shouldn't couldn't they're we'll they've", | ||
"shouldn't couldn't they're we'll they've", | ||
), | ||
( | ||
"TASK: should not could not they are we will they have", | ||
"should not could not they are we will they have", | ||
), | ||
), | ||
( | ||
("TASK | Param1 | Param 2 : We're didn*'t", "We're didn't"), | ||
("TASK | Param1 | Param 2 : We're didn*'t", "We're didn't"), | ||
), | ||
], | ||
) | ||
def test_RemoveContractionsAugmentation( | ||
input: tuple[str, str], expected: tuple[str, str] | ||
) -> None: | ||
augmentation = RemoveContractionsAugmentation(1.0) | ||
assert augmentation(*input) == expected |
30 changes: 30 additions & 0 deletions
30
tests/data/augmentations/test_RemoveEndPunctuationAugmentation.py
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,30 @@ | ||
from __future__ import annotations | ||
import pytest | ||
|
||
from frame_semantic_transformer.data.augmentations.RemoveEndPunctuationAugmentation import ( | ||
RemoveEndPunctuationAugmentation, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,expected", | ||
[ | ||
( | ||
("TASK: I am a banana.", "I am a banana."), | ||
("TASK: I am a banana", "I am a banana"), | ||
), | ||
( | ||
("TASK: I am a banana!", "I am a banana!"), | ||
("TASK: I am a banana", "I am a banana"), | ||
), | ||
( | ||
("TASK: I am a banana .", "I am a banana ."), | ||
("TASK: I am a banana", "I am a banana"), | ||
), | ||
], | ||
) | ||
def test_RemoveEndPunctuationAugmentation( | ||
input: tuple[str, str], expected: tuple[str, str] | ||
) -> None: | ||
augmentation = RemoveEndPunctuationAugmentation(1.0) | ||
assert augmentation(*input) == expected |