mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-19 12:58:07 +00:00
rename word_reversal.py -> word_sequence_reversal.py
This commit is contained in:
parent
cf864d523a
commit
7977895275
5 changed files with 67 additions and 64 deletions
|
|
@ -104,7 +104,7 @@ Available dataset names (which can be used with `create_dataset()`):
|
|||
- `LetterCountingDataset`: Count letter occurrences in text spans
|
||||
- `NumberFilteringDataset`: Filter numbers based on comparison with threshold
|
||||
- `NumberSortingDataset`: Sort lists of numbers in ascending or descending order
|
||||
- `LetterJumbleDataset`: Unscramble words that have had their letters randomly jumbled
|
||||
- `LetterJumbleDataset`: Unscramble words that have had their letters randomly jumbled
|
||||
- `SpellBackwardDataset`: Spell individual words backward (e.g. "sun" -> "nus")
|
||||
- `WordSequenceReversalDataset`: Reverse word order in text spans
|
||||
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ from ..data import read_data_file
|
|||
from ..factory import ProceduralDataset, register_dataset
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass
|
||||
class SpellBackwardConfig:
|
||||
"""Configuration for spelling words backward task generation"""
|
||||
|
||||
|
||||
min_word_len: int = 3 # Minimum word length
|
||||
seed: Optional[int] = None
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self) -> None:
|
||||
|
|
@ -31,8 +31,9 @@ class SpellBackwardDataset(ProceduralDataset):
|
|||
# Load and preprocess text
|
||||
text = read_data_file("in_the_year_2889.txt")
|
||||
# Extract words and clean them to contain only alphanumeric characters
|
||||
self.words = [word for word in re.findall(r"\b\w+\b", text)
|
||||
if word.isalnum() and len(word) >= config.min_word_len]
|
||||
self.words = [
|
||||
word for word in re.findall(r"\b\w+\b", text) if word.isalnum() and len(word) >= config.min_word_len
|
||||
]
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single spell backward task"""
|
||||
|
|
|
|||
59
tests/test_spell_backward.py
Normal file
59
tests/test_spell_backward.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
"""Tests for spell backward task generation"""
|
||||
|
||||
import pytest
|
||||
|
||||
from reasoning_gym.algorithmic.spell_backward import SpellBackwardConfig, SpellBackwardDataset
|
||||
|
||||
|
||||
def test_spell_backward_config_validation():
|
||||
"""Test that invalid configs raise appropriate errors"""
|
||||
with pytest.raises(AssertionError):
|
||||
config = SpellBackwardConfig(min_word_len=0)
|
||||
config.validate()
|
||||
|
||||
|
||||
def test_spell_backward_dataset_deterministic():
|
||||
"""Test that dataset generates same items with same seed"""
|
||||
config = SpellBackwardConfig(seed=42, size=10)
|
||||
dataset1 = SpellBackwardDataset(config)
|
||||
dataset2 = SpellBackwardDataset(config)
|
||||
|
||||
for i in range(len(dataset1)):
|
||||
assert dataset1[i] == dataset2[i]
|
||||
|
||||
|
||||
def test_spell_backward_dataset_items():
|
||||
"""Test basic properties of generated items"""
|
||||
config = SpellBackwardConfig(min_word_len=3, size=10, seed=42)
|
||||
dataset = SpellBackwardDataset(config)
|
||||
|
||||
for i in range(len(dataset)):
|
||||
item = dataset[i]
|
||||
# Check item structure
|
||||
assert isinstance(item, dict)
|
||||
assert "question" in item
|
||||
assert "answer" in item
|
||||
assert "metadata" in item
|
||||
|
||||
# Check metadata
|
||||
assert "word" in item["metadata"]
|
||||
assert "word_len" in item["metadata"]
|
||||
|
||||
# Verify word length constraint
|
||||
word = item["metadata"]["word"]
|
||||
assert len(word) >= config.min_word_len
|
||||
|
||||
# Verify answer is correct
|
||||
assert item["answer"] == word[::-1]
|
||||
|
||||
|
||||
def test_spell_backward_dataset_iteration():
|
||||
"""Test that iteration respects dataset size"""
|
||||
config = SpellBackwardConfig(size=5, seed=42)
|
||||
dataset = SpellBackwardDataset(config)
|
||||
|
||||
items = list(dataset)
|
||||
assert len(items) == config.size
|
||||
|
||||
# Test multiple iterations yield same items
|
||||
assert items == list(dataset)
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
"""Tests for word reversal task generation"""
|
||||
|
||||
import pytest
|
||||
|
||||
from reasoning_gym.algorithmic.spell_backward import SpellBackwardConfig, SpellBackwardDataset
|
||||
from reasoning_gym.algorithmic.word_reversal import WordSequenceReversalConfig, WordSequenceReversalDataset
|
||||
from reasoning_gym.algorithmic.word_sequence_reversal import WordSequenceReversalConfig, WordSequenceReversalDataset
|
||||
|
||||
|
||||
def test_word_sequence_reversal_config_validation():
|
||||
|
|
@ -55,60 +52,6 @@ def test_word_sequence_reversal_dataset_items():
|
|||
assert answer_words == list(reversed(question_words))
|
||||
|
||||
|
||||
def test_spell_backward_config_validation():
|
||||
"""Test that invalid configs raise appropriate errors"""
|
||||
with pytest.raises(AssertionError):
|
||||
config = SpellBackwardConfig(min_word_len=0)
|
||||
config.validate()
|
||||
|
||||
|
||||
def test_spell_backward_dataset_deterministic():
|
||||
"""Test that dataset generates same items with same seed"""
|
||||
config = SpellBackwardConfig(seed=42, size=10)
|
||||
dataset1 = SpellBackwardDataset(config)
|
||||
dataset2 = SpellBackwardDataset(config)
|
||||
|
||||
for i in range(len(dataset1)):
|
||||
assert dataset1[i] == dataset2[i]
|
||||
|
||||
|
||||
def test_spell_backward_dataset_items():
|
||||
"""Test basic properties of generated items"""
|
||||
config = SpellBackwardConfig(min_word_len=3, size=10, seed=42)
|
||||
dataset = SpellBackwardDataset(config)
|
||||
|
||||
for i in range(len(dataset)):
|
||||
item = dataset[i]
|
||||
# Check item structure
|
||||
assert isinstance(item, dict)
|
||||
assert "question" in item
|
||||
assert "answer" in item
|
||||
assert "metadata" in item
|
||||
|
||||
# Check metadata
|
||||
assert "word" in item["metadata"]
|
||||
assert "word_len" in item["metadata"]
|
||||
|
||||
# Verify word length constraint
|
||||
word = item["metadata"]["word"]
|
||||
assert len(word) >= config.min_word_len
|
||||
|
||||
# Verify answer is correct
|
||||
assert item["answer"] == word[::-1]
|
||||
|
||||
|
||||
def test_spell_backward_dataset_iteration():
|
||||
"""Test that iteration respects dataset size"""
|
||||
config = SpellBackwardConfig(size=5, seed=42)
|
||||
dataset = SpellBackwardDataset(config)
|
||||
|
||||
items = list(dataset)
|
||||
assert len(items) == config.size
|
||||
|
||||
# Test multiple iterations yield same items
|
||||
assert items == list(dataset)
|
||||
|
||||
|
||||
def test_word_sequence_reversal_dataset_iteration():
|
||||
"""Test that iteration respects dataset size"""
|
||||
config = WordSequenceReversalConfig(size=5, seed=42)
|
||||
Loading…
Add table
Add a link
Reference in a new issue