Skip to content
1 change: 0 additions & 1 deletion .github/workflows/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ jsonlines
openai
langchain
evaluate
inflect
rouge_score
typing-extensions < 4.6.0
73 changes: 34 additions & 39 deletions nlptest/transform/robustness.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio
import random
import re
from inflect import engine
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, abbreviation_dict)
from ..utils.custom_types import Sample, Span, Transformation
from ..utils.number_to_word import engine
from typing import List


Expand Down Expand Up @@ -598,7 +598,7 @@ def search_contraction(text):

class NumberToWord(BaseRobustness):
alias_name = "number_to_word"
infEng = engine()
num = engine()

@staticmethod
def transform(sample_list: List[Sample]) -> List[Sample]:
Expand All @@ -610,47 +610,42 @@ def transform(sample_list: List[Sample]) -> List[Sample]:
Returns:
List of sentences that have numbers in their verbal representation.
"""

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

for match in re.finditer(regex, text):
token = match.group()
words = NumberToWord.infEng.number_to_words(
token, wantlist=True)
token_len = len(token) - 1
new_words_len = len(' '.join(words)) - 1
trans.append(text[start_offset:match.start()])
trans.append(' '.join(words))
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(
)+new_words_len, word=' '.join(words)),
ignore=False

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

for match in re.finditer(regex, text):
token = match.group()
words = NumberToWord.num.number_to_words(token, wantlist=True)
new_words_len = len(' '.join(words))
trans.append(text[start_offset:match.start()])
trans.append(' '.join(words))
start_offset = match.end()
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()+new_words_len, word=' '.join(words)),
ignore=False
)
)
)

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

return ''.join(results), trans

for idx, sample in enumerate(sample_list):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't refactor this section, this will handle when list of samples or strings in the function.

if isinstance(sample, str):
sample_list[idx] = convert_numbers(
r'(?<!\S)\d+(\.\d+)?(\.)?(?=(\s|\n|$))', sample)
else:
sample.test_case, transformations = convert_numbers(
r'(?<!\S)\d+(\.\d+)?(\.)?(?=(\s|\n|$))', sample.original)
trans.append(text[start_offset:])
results.append(''.join(trans))
if sample.task in ("ner", "text-classification"):
sample.transformations = transformations
sample.category = "robustness"
return ''.join(results)

for sample in sample_list:
if sample.task =='question-answering':
sample.perturbed_question = convert_numbers(r'(?<!\S)(\d+(\.\d+)?)(?=(\s|\n|$))', sample.original_question)
if "perturbed_context" in sample.__annotations__:
sample.perturbed_context = convert_numbers(r'(?<!\S)(\d+(\.\d+)?)(?=(\s|\n|$))', sample.original_context)
else:
sample.test_case = convert_numbers(r'(?<!\S)(\d+(\.\d+)?)(?=(\s|\n|$))', sample.original)
return sample_list


Expand Down
84 changes: 83 additions & 1 deletion nlptest/transform/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import defaultdict
from typing import Dict, List

import re
import pandas as pd

from nlptest.utils.custom_types import NERPrediction, Sample, SequenceLabel, NEROutput, SequenceClassificationOutput
Expand Down Expand Up @@ -7198,6 +7198,88 @@ def get_entity_representation_proportions(entity_representation):
"xsum": "You are an intelligent Context summarizer. Please read the following context carefully. After understanding its content, create a concise summary, capturing the essential themes and key details. Please ensure that the summary does not end abruptly and remains within the max_tokens word limit. Context: {context}\n\n Summary: "
}

nth = {
0: "th",
1: "st",
2: "nd",
3: "rd",
4: "th",
5: "th",
6: "th",
7: "th",
8: "th",
9: "th",
11: "th",
12: "th",
13: "th",}

ordinal = dict(
ty="tieth",
one="first",
two="second",
three="third",
five="fifth",
eight="eighth",
nine="ninth",
twelve="twelfth",
)

unit = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

teen = [
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
]
ten = [
"",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
]
mill = [
" ",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion",
" sextillion",
" septillion",
" octillion",
" nonillion",
" decillion",
]

nth_suff = set(nth.values())
ordinal_suff = re.compile(fr"({'|'.join(ordinal)})\Z")

NON_DIGIT = re.compile(r"\D")
WHITESPACES_COMMA = re.compile(r"\s+,")
COMMA_WORD = re.compile(r", (\S+)\s+\Z")
WHITESPACES = re.compile(r"\s+")
DIGIT_GROUP = re.compile(r"(\d)")
TWO_DIGITS = re.compile(r"(\d)(\d)")
THREE_DIGITS = re.compile(r"(\d)(\d)(\d)")
THREE_DIGITS_WORD = re.compile(r"(\d)(\d)(\d)(?=\D*\Z)")
TWO_DIGITS_WORD = re.compile(r"(\d)(\d)(?=\D*\Z)")
ONE_DIGIT_WORD = re.compile(r"(\d)(?=\D*\Z)")
FOUR_DIGIT_COMMA = re.compile(r"(\d)(\d{3}(?:,|\Z))")

qa_prompt_template ="""
You are a distinguished professor known for your expertise in meticulously grading students' answers to questions. Your extensive knowledge and experience make you the go-to authority in your field.
You have been entrusted with the evaluation of the following question:
Expand Down
Loading