formatting

This commit is contained in:
Andreas Koepf 2025-01-25 18:51:28 +01:00
parent 5fc0b1bdc3
commit 31a5b5cb76
6 changed files with 51 additions and 60 deletions

View file

@ -7,6 +7,7 @@ from string import ascii_uppercase
from typing import List, Optional
from reasoning_gym.data import read_data_file
from ..factory import ProceduralDataset, register_dataset
@ -37,10 +38,10 @@ class CaesarCipherDataset(ProceduralDataset):
# Load and preprocess text
text = read_data_file("in_the_year_2889.txt")
# Split into sentences and filter
sentences = [s.strip() for s in text.split(config.delimiter) if s.strip()]
# Process each sentence
self.valid_sentences = []
for sentence in sentences:
@ -55,7 +56,7 @@ class CaesarCipherDataset(ProceduralDataset):
for char in text:
if char.isalpha():
# Convert to 0-25 range, rotate, convert back to ASCII
base = ord('A')
base = ord("A")
rotated = (ord(char) - base + rotation) % 26
result.append(chr(base + rotated))
else:
@ -65,22 +66,18 @@ class CaesarCipherDataset(ProceduralDataset):
def __getitem__(self, idx: int) -> dict:
"""Generate a single Caesar cipher task"""
rng = Random(self.seed + idx)
# Select random sentence and rotation
sentence = rng.choice(self.valid_sentences)
rotation = rng.randint(self.config.min_rotation, self.config.max_rotation)
# Generate cipher text
cipher_text = self._caesar_encrypt(sentence, rotation)
return {
"question": f"Decrypt this Caesar cipher text: {cipher_text}",
"answer": sentence,
"metadata": {
"rotation": rotation,
"cipher_text": cipher_text,
"clear_text": sentence
}
"metadata": {"rotation": rotation, "cipher_text": cipher_text, "clear_text": sentence},
}

View file

@ -6,6 +6,7 @@ from random import Random
from typing import List, Optional
from reasoning_gym.data import read_data_file
from ..factory import ProceduralDataset, register_dataset
@ -31,7 +32,9 @@ class LetterJumbleConfig:
assert self.max_words >= self.min_words, "max_words must be >= min_words"
assert 0 <= self.min_corruption_level <= 1, "min_corruption_level must be in [0,1]"
assert 0 <= self.max_corruption_level <= 1, "max_corruption_level must be in [0,1]"
assert self.max_corruption_level >= self.min_corruption_level, "max_corruption_level must be >= min_corruption_level"
assert (
self.max_corruption_level >= self.min_corruption_level
), "max_corruption_level must be >= min_corruption_level"
class LetterJumbleDataset(ProceduralDataset):
@ -44,50 +47,47 @@ class LetterJumbleDataset(ProceduralDataset):
text = read_data_file("in_the_year_2889.txt")
# Extract words and filter by length
self.words = [
word for word in re.findall(r"\b\w+\b", text)
if self.config.min_word_len <= len(word) <= self.config.max_word_len
and word.isalpha()
word
for word in re.findall(r"\b\w+\b", text)
if self.config.min_word_len <= len(word) <= self.config.max_word_len and word.isalpha()
]
def _scramble_word(self, word: str, corruption_level: float, rng: Random) -> str:
"""Scramble a word by swapping random pairs of characters"""
if len(word) < 2: # Can't scramble 1-character words
return word
word = list(word)
num_swaps = max(1, int(len(word) * corruption_level)) # Ensure at least one swap
for _ in range(num_swaps):
# Pick two different random positions
pos1, pos2 = rng.sample(range(len(word)), 2)
# Swap characters
word[pos1], word[pos2] = word[pos2], word[pos1]
return "".join(word)
def __getitem__(self, idx: int) -> dict:
"""Generate a single word jumbling task"""
rng = Random(self.seed + idx)
# Select number of words and corruption level
num_words = rng.randint(self.config.min_words, self.config.max_words)
corruption_level = rng.uniform(self.config.min_corruption_level, self.config.max_corruption_level)
# Select words based on configuration
if self.config.consecutive_words:
# Select consecutive words from a random starting position
start_idx = rng.randint(0, len(self.words) - num_words)
selected_words = self.words[start_idx:start_idx + num_words]
selected_words = self.words[start_idx : start_idx + num_words]
else:
# Select random words
selected_words = rng.sample(self.words, num_words)
# Scramble each word
scrambled_words = [
self._scramble_word(word, corruption_level, rng)
for word in selected_words
]
scrambled_words = [self._scramble_word(word, corruption_level, rng) for word in selected_words]
return {
"question": f"Unscramble these words: {' '.join(scrambled_words)}",
"answer": " ".join(selected_words),
@ -95,8 +95,8 @@ class LetterJumbleDataset(ProceduralDataset):
"num_words": num_words,
"corruption_level": corruption_level,
"scrambled_words": scrambled_words,
"original_words": selected_words
}
"original_words": selected_words,
},
}