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

Added num2words #74

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions transformations/num2words/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Numbers2Words Transformation 🦎 + ⌨️ → 🐍
This transformation converts the numbers/floats in the given sentence/paragraph to word format.

Author name: Viswanatha Reddy Gajjala
Author email: [email protected]

## What type of a transformation is this?
This transformation acts like a perturbation to test robustness.
Input: 2 times 2 is 4.
Output: two times two is four.

## What tasks does it intend to benefit?
This perturbation would benefit all tasks which have a sentence/paragraph/document as input like text classification,
text generation, etc.

This transformation can be used to augment the dataset that contains numerical values. It helps to analyze models performance on questions which require numerical understanding.

## What are the limitations of this transformation?
The transformation's outputs are too simple to be used for data augmentation. Unlike a paraphraser, it is not capable of generating linguistically diverse text.
1 change: 1 addition & 0 deletions transformations/num2words/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .transformation import *
50 changes: 50 additions & 0 deletions transformations/num2words/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "num2words",
"test_cases": [
{
"class": "num2words",
"inputs": {
"sentence": "He ate 0.25 of the pizza within the first 5 minutes."
},
"outputs": [{
"sentence": "He ate zero and 25/100 of the pizza within the first 5 minutes."
}]
},
{
"class": "num2words",
"inputs": {
"sentence": "He has returned from his office."
},
"outputs": [{
"sentence": "He has returned from his office."
}]
},
{
"class": "num2words",
"inputs": {
"sentence": "She has bought 100 apples."
},
"outputs": [{
"sentence": "She has bought one hundred apples."
}]
},
{
"class": "num2words",
"inputs": {
"sentence": "2 times 2 is 4."
},
"outputs": [{
"sentence": "two times two is four."
}]
},
{
"class": "num2words",
"inputs": {
"sentence": "Neuroplasticity is a continuous processing allowing short-term, medium-term, and long-term remodeling of the neuronosynaptic organization."
},
"outputs": [{
"sentence": "Neuroplasticity is a continuous processing allowing short-term, medium-term, and long-term remodeling of the neuronosynaptic organization."
}]
}
]
}
66 changes: 66 additions & 0 deletions transformations/num2words/transformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import re
import spacy
from interfaces.SentenceOperation import SentenceOperation
from tasks.TaskTypes import TaskType
import inflect


class Numbers2Words:
nlp = None

def __init__(self):
self.nlp = spacy.load("en_core_web_sm")

@staticmethod
def int2words(n, p=inflect.engine()):
return ' '.join(p.number_to_words(n, wantlist=True, andword=' '))

def float2words(self, float_value):
float_value = str(round(float(float_value), 2))
integer, dot, decimal = float_value.partition('.')
return "{integer}{decimal}".format(
integer=self.int2words(int(integer)),
decimal=" and {}/100".format(decimal) if decimal and int(decimal) else '')

def __call__(self, input_text: str):
doc = self.nlp(input_text)

for entity in doc.ents:
new_value = None

if entity.label_ == "CARDINAL" and not re.search(
"[_]|[-]|[:]|[/]|[(]|[)]", entity.text
):

cardinal_value = entity.text

cardinal_value = cardinal_value.replace(",", "")

if cardinal_value.isdigit() or '.' in cardinal_value:
cardinal_value = self.float2words(cardinal_value)
input_text = input_text.replace(entity.text, str(cardinal_value))

return input_text


class Num2Words(SentenceOperation):
tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION]
languages = ["en"]

def __init__(self, verbose=False):
super().__init__(verbose=verbose)
self.transform = Numbers2Words()

def generate(self, sentence: str):
result = self.transform(sentence)
if self.verbose:
print(f"Perturbed Input from {self.name()} : {result}")
return [result]

"""
# Sample code to demonstrate usage. Can also assist in adding test cases.
if __name__ == '__main__':
Num2Words(verbose=True).generate('she has bought hundred apples.')
Num2Words(verbose=True).generate('she has bought 100 apples.')
Num2Words(verbose=True).generate('she has bought 100.55 apples.')
"""