mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-27 17:23:19 +00:00
rename word_reversal.py -> word_sequence_reversal.py
This commit is contained in:
parent
e9ac50a6fc
commit
cdf08d9d5b
5 changed files with 67 additions and 64 deletions
|
|
@ -31,8 +31,9 @@ class SpellBackwardDataset(ProceduralDataset):
|
||||||
# Load and preprocess text
|
# Load and preprocess text
|
||||||
text = read_data_file("in_the_year_2889.txt")
|
text = read_data_file("in_the_year_2889.txt")
|
||||||
# Extract words and clean them to contain only alphanumeric characters
|
# Extract words and clean them to contain only alphanumeric characters
|
||||||
self.words = [word for word in re.findall(r"\b\w+\b", text)
|
self.words = [
|
||||||
if word.isalnum() and len(word) >= config.min_word_len]
|
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:
|
def __getitem__(self, idx: int) -> dict:
|
||||||
"""Generate a single spell backward task"""
|
"""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
|
import pytest
|
||||||
|
|
||||||
from reasoning_gym.algorithmic.spell_backward import SpellBackwardConfig, SpellBackwardDataset
|
from reasoning_gym.algorithmic.word_sequence_reversal import WordSequenceReversalConfig, WordSequenceReversalDataset
|
||||||
from reasoning_gym.algorithmic.word_reversal import WordSequenceReversalConfig, WordSequenceReversalDataset
|
|
||||||
|
|
||||||
|
|
||||||
def test_word_sequence_reversal_config_validation():
|
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))
|
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():
|
def test_word_sequence_reversal_dataset_iteration():
|
||||||
"""Test that iteration respects dataset size"""
|
"""Test that iteration respects dataset size"""
|
||||||
config = WordSequenceReversalConfig(size=5, seed=42)
|
config = WordSequenceReversalConfig(size=5, seed=42)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue