Skip to content

Commit

Permalink
Merge pull request #583 from QData/Transformations-Docstring-update
Browse files Browse the repository at this point in the history
Update Transformation Docstrings with API usage
  • Loading branch information
qiyanjun committed Nov 19, 2021
2 parents bfc3468 + 5dd209d commit 966531c
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def __init__(self, n=3, confidence_score=0.7, **kwargs):
:param n: Number of new locations to generate
:param confidence_score: Location will only be changed if it's above the confidence score
>>> from textattack.transformations import WordSwapChangeLocation
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapChangeLocation()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am in Dallas.'
>>> augmenter.augment(s)
"""
super().__init__(**kwargs)
self.n = n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def __init__(
:param first_only: Whether to change first name only
:param last_only: Whether to change last name only
:param confidence_score: Name will only be changed when it's above confidence score
>>> from textattack.transformations import WordSwapChangeName
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapChangeName()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am John Smith.'
>>> augmenter.augment(s)
"""
super().__init__(**kwargs)
self.num_name_replacements = num_name_replacements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def __init__(self, max_change=1, n=3, **kwargs):
:param max_change: Maximum percent of change (1 being 100%)
:param n: Numbers of new numbers to generate
>>> from textattack.transformations import WordSwapChangeNumber
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapChangeNumber()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am 12 years old.'
>>> augmenter.augment(s)
"""
super().__init__(**kwargs)
self.max_change = max_change
Expand Down
10 changes: 9 additions & 1 deletion textattack/transformations/word_swaps/word_swap_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ class WordSwapContract(WordSwap):

def _get_transformations(self, current_text, indices_to_modify):
"""Return all possible transformed sentences, each with one
contraction."""
contraction.
>>> from textattack.transformations import WordSwapContract
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapContract()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am 12 years old.'
>>> augmenter.augment(s)
"""
transformed_texts = []

words = current_text.words
Expand Down
7 changes: 7 additions & 0 deletions textattack/transformations/word_swaps/word_swap_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class WordSwapEmbedding(WordSwap):
Args:
max_candidates (int): maximum number of synonyms to pick
embedding (textattack.shared.AbstractWordEmbedding): Wrapper for word embedding
>>> from textattack.transformations import WordSwapEmbedding
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapEmbedding()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(
Expand Down
10 changes: 9 additions & 1 deletion textattack/transformations/word_swaps/word_swap_extend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ class WordSwapExtend(WordSwap):

def _get_transformations(self, current_text, indices_to_modify):
"""Return all possible transformed sentences, each with one
extension."""
extension.
>>> from textattack.transformations import WordSwapExtend
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapExtend()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = '''I'm fabulous'''
>>> augmenter.augment(s)
"""
transformed_texts = []
words = current_text.words
for idx in indices_to_modify:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class WordSwapGradientBased(WordSwap):
model (nn.Module): The model to attack. Model must have a
`word_embeddings` matrix and `convert_id_to_word` function.
top_n (int): the number of top words to return at each index
>>> from textattack.transformations import WordSwapGradientBased
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapGradientBased()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(self, model_wrapper, top_n=1):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@

class WordSwapHomoglyphSwap(WordSwap):
"""Transforms an input by replacing its words with visually similar words
using homoglyph swaps."""
using homoglyph swaps.
>>> from textattack.transformations import WordSwapHomoglyphSwap
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapHomoglyphSwap()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(self, random_one=False, **kwargs):
super().__init__(**kwargs)
Expand Down
7 changes: 7 additions & 0 deletions textattack/transformations/word_swaps/word_swap_hownet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def _get_replacement_words(self, word, word_pos):
sentence or phrase.
Based on nearest neighbors selected word embeddings.
>>> from textattack.transformations import WordSwapHowNet
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapHowNet()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""
word_pos = self.pos_dict.get(word_pos, None)
if word_pos is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class WordSwapNeighboringCharacterSwap(WordSwap):
character.
skip_last_char (bool): Whether to disregard perturbing the last
character.
>>> from textattack.transformations import WordSwapNeighboringCharacterSwap
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapNeighboringCharacterSwap()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(
Expand Down
7 changes: 7 additions & 0 deletions textattack/transformations/word_swaps/word_swap_qwerty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def __init__(
:param random_one: Whether to return a single (random) swap, or all possible swaps.
:param skip_first_char: When True, do not modify the first character of each word.
:param skip_last_char: When True, do not modify the last character of each word.
>>> from textattack.transformations import WordSwapQWERTY
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapQWERT()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""
super().__init__(**kwargs)
self.random_one = random_one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class WordSwapRandomCharacterDeletion(WordSwap):
character.
skip_last_char (bool): Whether to disregard deleting the last
character.
>>> from textattack.transformations import WordSwapRandomCharacterDeletion
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapRandomCharacterDeletion()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class WordSwapRandomCharacterInsertion(WordSwap):
skip_first_char (bool): Whether to disregard inserting as the first
character. skip_last_char (bool): Whether to disregard inserting as
the last character.
>>> from textattack.transformations import WordSwapRandomCharacterInsertion
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapRandomCharacterInsertion()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class WordSwapRandomCharacterSubstitution(WordSwap):
Args:
random_one (bool): Whether to return a single word with a random
character deleted. If not set, returns all possible options.
>>> from textattack.transformations import WordSwapRandomCharacterSubstitution
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapRandomCharacterSubstitution()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(self, random_one=True, **kwargs):
Expand Down
10 changes: 9 additions & 1 deletion textattack/transformations/word_swaps/word_swap_wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@

class WordSwapWordNet(WordSwap):
"""Transforms an input by replacing its words with synonyms provided by
WordNet."""
WordNet.
>>> from textattack.transformations import WordSwapWordNet
>>> from textattack.augmentation import Augmenter
>>> transformation = WordSwapWordNet()
>>> augmenter = Augmenter(transformation=transformation)
>>> s = 'I am fabulous.'
>>> augmenter.augment(s)
"""

def __init__(self, language="eng"):
if language not in wordnet.langs():
Expand Down

0 comments on commit 966531c

Please sign in to comment.