Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 58 additions & 2 deletions nlptest/transform/robustness.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from abc import ABC, abstractmethod
from typing import Dict, List, Optional
from nlptest.modelhandler.modelhandler import ModelFactory
from .utils import (CONTRACTION_MAP, TYPO_FREQUENCY, default_user_prompt ,ocr_typo_dict)
from .utils import (CONTRACTION_MAP, TYPO_FREQUENCY, default_user_prompt ,ocr_typo_dict, abbreviation_dict)
from ..utils.custom_types import Sample, Span, Transformation
from typing import List

Expand Down Expand Up @@ -835,4 +835,60 @@ def ocr_typo(regex, text):
else:
sample.test_case = ocr_typo(r'[^,\s.!?]+', sample.original)

return sample_list
return sample_list

class AbbreviationInsertion(BaseRobustness):
alias_name = "add_abbreviation"

@staticmethod
def transform(sample_list: List[Sample]) -> List[Sample]:
"""
Transforms the given sample list by inserting abbreviations.

Args:
sample_list (List[Sample]): The list of samples to transform.

Returns:
List[Sample]: The transformed list of samples.
"""

def insert_abbreviation(text):
perturbed_text = text
transformations = []

for abbreviation, expansions in abbreviation_dict.items():
for expansion in expansions:
pattern = r"(?i)\b" + re.escape(expansion) + r"\b"
corrected_token = abbreviation
perturbed_text = re.sub(pattern, corrected_token, perturbed_text)
matches = re.finditer(pattern, text)
for match in matches:
start = match.start()
end = match.end()
token = text[start:end]
if corrected_token != token:
if sample.task in ("ner", "text-classification"):
transformations.append(
Transformation(
original_span=Span(start=start, end=end, word=token),
new_span=Span(start=start, end=start + len(corrected_token), word=corrected_token),
ignore=False
)
)
sample.category = "robustness"
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 = insert_abbreviation(sample.original_question)

if "perturbed_context" in sample.__annotations__:
sample.perturbed_context = insert_abbreviation(sample.original_context)

else:
sample.test_case = insert_abbreviation(sample.original)
sample.category = "robustness"

return sample_list
Loading