mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-25 17:10:51 +00:00
feat: Add comprehensive unit tests for parse_string_to_complex() method
This commit is contained in:
parent
726ba114dc
commit
a92dcd4a75
2 changed files with 80 additions and 18 deletions
|
|
@ -96,37 +96,57 @@ class ComplexArithmeticDataset(ProceduralDataset):
|
|||
|
||||
@staticmethod
|
||||
def parse_string_to_complex(answer: str) -> complex:
|
||||
if answer is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Normalize the answer string by removing spaces and converting to lowercase
|
||||
answer = answer.replace(" ", "").lower()
|
||||
|
||||
# Convert mathematical notation 'i' to Python's 'j' for complex numbers
|
||||
answer = answer.replace("i", "j")
|
||||
|
||||
# Handle real numbers (no imaginary part)
|
||||
if "j" not in answer:
|
||||
student_result = complex(float(answer))
|
||||
else:
|
||||
# Handle cases like "j" or "2j" (implicit coefficient)
|
||||
if answer[0] == "j":
|
||||
# Convert "j" to "1j", "2j" remains unchanged
|
||||
answer = "1" + answer
|
||||
# Handle cases like "3j" where there's no explicit + or - before j
|
||||
elif answer[-1] == "j" and not any(c in answer[:-1] for c in "+-"):
|
||||
# Convert "3j" to "3+1j"
|
||||
answer = answer.replace("j", "+1j")
|
||||
return complex(float(answer))
|
||||
|
||||
# Ensure the string has an imaginary part, even if zero
|
||||
if "j" not in answer:
|
||||
answer += "+0j"
|
||||
# Handle pure imaginary numbers
|
||||
if answer == "j":
|
||||
return complex(0, 1)
|
||||
if answer == "-j":
|
||||
return complex(0, -1)
|
||||
|
||||
# Parse the normalized string into a complex number
|
||||
student_result = complex(answer)
|
||||
# Handle cases like "7j" or "-7j" (no real part)
|
||||
if (
|
||||
answer.endswith("j")
|
||||
and not any(c in answer[:-1] for c in "+-")
|
||||
or (answer.startswith("-") and not any(c in answer[1:-1] for c in "+-"))
|
||||
):
|
||||
# Extract coefficient
|
||||
coef = answer[:-1]
|
||||
if coef == "":
|
||||
coef = "1"
|
||||
elif coef == "-":
|
||||
coef = "-1"
|
||||
return complex(0, float(coef))
|
||||
|
||||
except ValueError:
|
||||
# Handle complex numbers with both parts
|
||||
# Make sure there's a + or - before j if it's not at the beginning
|
||||
if "j" in answer and not answer.endswith("j"):
|
||||
return None # Invalid format like "3j+2"
|
||||
|
||||
# Handle cases like "3+j" (implicit 1)
|
||||
if "+j" in answer:
|
||||
answer = answer.replace("+j", "+1j")
|
||||
if "-j" in answer:
|
||||
answer = answer.replace("-j", "-1j")
|
||||
|
||||
# Parse the normalized string into a complex number
|
||||
return complex(answer)
|
||||
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
return student_result
|
||||
|
||||
def score_answer(self, answer: Optional[str], entry: dict) -> float:
|
||||
"""Score the answer using exponential distance-based scoring."""
|
||||
if answer is None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue