mirror of
https://github.com/QData/TextAttack.git
synced 2021-10-13 00:05:06 +03:00
update tests to include numbers in words
This commit is contained in:
4
.github/workflows/check-formatting.yml
vendored
4
.github/workflows/check-formatting.yml
vendored
@@ -1,7 +1,7 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Github Check Formatting with Black & isort
|
||||
name: Formatting with black & isort
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -15,7 +15,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.6, 3.7, 3.8]
|
||||
python-version: [3.8]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
4
.github/workflows/make-docs.yml
vendored
4
.github/workflows/make-docs.yml
vendored
@@ -1,7 +1,7 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Github Check Sphinx Build
|
||||
name: Build documentation with Sphinx
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -15,7 +15,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.6, 3.7, 3.8]
|
||||
python-version: [3.8]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
9
.github/workflows/run-pytest.yml
vendored
9
.github/workflows/run-pytest.yml
vendored
@@ -1,7 +1,7 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Github Run Tests with PyTest
|
||||
name: Test with PyTest
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -29,13 +29,6 @@ jobs:
|
||||
pip install pytest pytest-xdist # Testing packages
|
||||
python setup.py install_egg_info # Workaround https://github.com/pypa/pip/issues/4537
|
||||
pip install -e .
|
||||
- name: Check code format with black and isort
|
||||
run: |
|
||||
black . --check
|
||||
isort --check-only --recursive tests textattack
|
||||
- name: Build docs with Sphinx and check for errors
|
||||
run: |
|
||||
sphinx-build -b html docs docs/_build/html -W
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
pytest tests -vx --dist=loadfile -n auto
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
)
|
||||
(is_black_box): True
|
||||
)
|
||||
|
||||
/.*/
|
||||
Running in interactive mode
|
||||
----------------------------
|
||||
Enter a sentence to attack or "q" to quit:
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
| Original accuracy: | 100.0% |
|
||||
| Accuracy under attack: | 0.0% |
|
||||
| Attack success rate: | 100.0% |
|
||||
| Average perturbed word %: | 45.39% |
|
||||
| Average num. words per input: | 11.5 |
|
||||
| Avg num queries: | 26.5 |
|
||||
| Average perturbed word %: | 45.0% |
|
||||
| Average num. words per input: | 12.0 |
|
||||
| Avg num queries: | 27.0 |
|
||||
+-------------------------------+--------+
|
||||
|
||||
@@ -34,7 +34,7 @@ I went into "Night of the Hunted" not knowing what to expect at all. I was reall
|
||||
| Original accuracy: | 100.0% |
|
||||
| Accuracy under attack: | 0.0% |
|
||||
| Attack success rate: | 100.0% |
|
||||
| Average perturbed word %: | 0.63% |
|
||||
| Average num. words per input: | 162.5 |
|
||||
| Avg num queries: | 164.5 |
|
||||
| Average perturbed word %: | 0.62% |
|
||||
| Average num. words per input: | 164.0 |
|
||||
| Avg num queries: | 166.0 |
|
||||
+-------------------------------+--------+
|
||||
|
||||
@@ -12,13 +12,20 @@ def attacked_text():
|
||||
return textattack.shared.AttackedText(raw_text)
|
||||
|
||||
|
||||
raw_pokemon_text = "the threat implied in the title pokémon 4ever is terrifying like locusts in a horde these things will keep coming ."
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pokemon_attacked_text():
|
||||
return textattack.shared.AttackedText(raw_pokemon_text)
|
||||
|
||||
|
||||
premise = "Among these are the red brick Royal Palace, which now houses the Patan Museum (Nepal's finest and most modern museum), and, facing the palace across the narrow brick plaza, eight temples of different styles and sizes."
|
||||
hypothesis = "The Patan Museum is down the street from the red brick Royal Palace."
|
||||
raw_text_pair = collections.OrderedDict(
|
||||
[("premise", premise), ("hypothesis", hypothesis)]
|
||||
)
|
||||
|
||||
|
||||
raw_hyphenated_text = "It's a run-of-the-mill kind of farmer's tan."
|
||||
|
||||
|
||||
@@ -33,27 +40,13 @@ def attacked_text_pair():
|
||||
|
||||
|
||||
class TestAttackedText:
|
||||
def test_words(self, attacked_text):
|
||||
def test_words(self, attacked_text, pokemon_attacked_text):
|
||||
# fmt: off
|
||||
assert attacked_text.words == [
|
||||
"A",
|
||||
"person",
|
||||
"walks",
|
||||
"up",
|
||||
"stairs",
|
||||
"into",
|
||||
"a",
|
||||
"room",
|
||||
"and",
|
||||
"sees",
|
||||
"beer",
|
||||
"poured",
|
||||
"from",
|
||||
"a",
|
||||
"keg",
|
||||
"and",
|
||||
"people",
|
||||
"talking",
|
||||
"A", "person", "walks", "up", "stairs", "into", "a", "room", "and", "sees", "beer", "poured", "from", "a", "keg", "and", "people", "talking",
|
||||
]
|
||||
assert pokemon_attacked_text.words == ['the', 'threat', 'implied', 'in', 'the', 'title', 'pokémon', '4ever', 'is', 'terrifying', 'like', 'locusts', 'in', 'a', 'horde', 'these', 'things', 'will', 'keep', 'coming']
|
||||
# fmt: on
|
||||
|
||||
def test_window_around_index(self, attacked_text):
|
||||
assert attacked_text.text_window_around_index(5, 1) == "into"
|
||||
@@ -77,8 +70,9 @@ class TestAttackedText:
|
||||
def test_window_around_index_end(self, attacked_text):
|
||||
assert attacked_text.text_window_around_index(17, 3) == "and people talking"
|
||||
|
||||
def test_text(self, attacked_text, attacked_text_pair):
|
||||
def test_text(self, attacked_text, pokemon_attacked_text, attacked_text_pair):
|
||||
assert attacked_text.text == raw_text
|
||||
assert pokemon_attacked_text.text == raw_pokemon_text
|
||||
assert attacked_text_pair.text == "\n".join(raw_text_pair.values())
|
||||
|
||||
def test_printable_text(self, attacked_text, attacked_text_pair):
|
||||
|
||||
@@ -37,3 +37,5 @@ def test_command_line_augmentation(name, command, outfile, sample_output_file):
|
||||
# Ensure CSV file exists, then delete it.
|
||||
assert os.path.exists(outfile)
|
||||
os.remove(outfile)
|
||||
|
||||
assert result.returncode == 0
|
||||
|
||||
@@ -27,3 +27,5 @@ def test_command_line_list(name, command, sample_output_file):
|
||||
print("stderr =>", stderr)
|
||||
|
||||
assert stdout == desired_text
|
||||
|
||||
assert result.returncode == 0
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
def has_letter(word):
|
||||
""" Returns true if `word` contains at least one character in [A-Za-z]. """
|
||||
""" Returns true if `word` contains at least one character in [A-Za-z].
|
||||
"""
|
||||
# TODO implement w regex
|
||||
for c in word:
|
||||
if c.isalpha():
|
||||
return True
|
||||
@@ -21,10 +23,11 @@ def add_indent(s_, numSpaces):
|
||||
def words_from_text(s, words_to_ignore=[]):
|
||||
""" Lowercases a string, removes all non-alphanumeric characters,
|
||||
and splits into words. """
|
||||
# TODO implement w regex
|
||||
words = []
|
||||
word = ""
|
||||
for c in " ".join(s.split()):
|
||||
if c.isalpha():
|
||||
if c.isalnum():
|
||||
word += c
|
||||
elif c in "'-" and len(word) > 0:
|
||||
# Allow apostrophes and hyphens as long as they don't begin the
|
||||
|
||||
Reference in New Issue
Block a user