Skip to content
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

feature/add_ocr_typo_for_QA_and_Summarization #436

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions nlptest/transform/robustness.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,14 @@ def transform(sample_list: List[Sample]) -> List[Sample]:
Returns:
List[Sample]: The transformed list of samples.
"""
for sample in sample_list:

def ocr_typo(regex, text):
results = []
trans = []
transformations = []
start_offset = 0

for match in re.finditer(r'[^,\s.!?]+', sample.original):
for match in re.finditer(regex, text):
token = match.group()
corrected_token = None

Expand All @@ -799,26 +800,39 @@ def transform(sample_list: List[Sample]) -> List[Sample]:
corrected_token = token

if corrected_token != token:
trans.append(sample.original[start_offset:match.start()])
trans.append(text[start_offset:match.start()])
trans.append(corrected_token)
start_offset = match.end()

transformations.append(
Transformation(
original_span=Span(start=match.start(), end=match.end() - 1, word=token),
new_span=Span(start=match.start(), end=match.start() + len(corrected_token), word=corrected_token),
ignore=False
if sample.task in ("ner", "text-classification"):
transformations.append(
Transformation(
original_span=Span(start=match.start(), end=match.end(), word=token),
new_span=Span(start=match.start(), end=match.start() + len(corrected_token),
word=corrected_token),
ignore=False
)
)
)
else:
trans.append(sample.original[start_offset:match.end()])
trans.append(text[start_offset:match.end()])
start_offset = match.end()

trans.append(sample.original[start_offset:])
trans.append(text[start_offset:])
results.append(''.join(trans))

sample.test_case = ''.join(results)
perturbed_text = ''.join(results)
sample.category = "robustness"
sample.transformations = transformations
if sample.task in ("ner", "text-classification"):
sample.transformations = transformations

return perturbed_text

for sample in sample_list:
if sample.task == 'question-answering':
sample.perturbed_question = ocr_typo(r'[^,\s.!?]+', sample.original_question)

if "perturbed_context" in sample.__annotations__:
sample.perturbed_context = ocr_typo(r'[^,\s.!?]+', sample.original_context)

else:
sample.test_case = ocr_typo(r'[^,\s.!?]+', sample.original)

return sample_list