Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions _patch_api5.py

This file was deleted.

14 changes: 12 additions & 2 deletions backend/classroom/test_wave_e_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ class EffortAndHeatmapTest(ReportsBase):
"""Items 43/44: the analytics derivations."""

def test_effort_board_ranks_grinders(self):
# st attempts 3 times (2 active days); lz a single AC.
# Widen the window so the pinned instants always fall inside,
# regardless of the current time of day.
Assignment.objects.filter(pk=self.assignment.pk).update(
available_from=timezone.now() - timedelta(days=3),
due_at=timezone.now() + timedelta(days=3),
)

# st attempts 4 times (2 active days); lz a single AC.
# The two "old day" submissions are pinned to fixed clock times on
# explicit dates (10:00 and 10:30 two days ago, 10:00 yesterday):
# minutes_ago offsets straddle midnight depending on the wall clock
Expand All @@ -107,10 +114,13 @@ def pinned(days_ago: int, minute: int = 0):
self._submit_at(self.student, WA, 10, pinned(2, 0))
self._submit_at(self.student, WA, 20, pinned(2, 30))
self._submit_at(self.student, WA, 30, pinned(1, 0))
# The third distinct day is pinned too: a minutes_ago offset can
# land on either side of midnight UTC depending on the wall clock.
self._submit_at(self.student, WA, 40, pinned(1, 30))
self._submit(self.lazy, AC, 100)
board = effort_board(self.classroom)
rows = {r["username"]: r for r in board["results"]}
self.assertEqual(rows["st"]["attempts"], 3)
self.assertEqual(rows["st"]["attempts"], 4)
self.assertEqual(rows["st"]["active_days"], 2)
self.assertEqual(rows["st"]["problems_solved"], 0)
self.assertEqual(rows["lz"]["problems_solved"], 1)
Expand Down
229 changes: 229 additions & 0 deletions backend/problems/statement_quality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
"""Statement quality tooling (wave F items 123/124/134).

- ``statement_lint`` (item 123) — rule-based checks on a problem's
statements: missing Input/Output/Constraints sections, bilingual parity
drift (samples mentioned in one locale only), unbalanced math
delimiters, wall-of-text sentences, sample-reference mismatches.
- ``readability`` (item 134) — Flesch reading-ease adapted per language
(RO syllable counting by vowel groups, EN classic formula) plus a grade
band and bilingual consistency check.
- ``translation_gaps`` (item 124) — the EN+translation sidecar report:
which localized fields are missing/present per locale, powering the
dual-pane authoring UI.
"""

from __future__ import annotations

import re

SECTION_KEYS = ("input", "output", "constraints", "examples")
#: A statement should mention examples; RO uses both "exemplu" and "example".
EXAMPLE_WORDS = ("example", "exemplu", "sample", "Date de intrare")
MAX_SENTENCE_WORDS = 60


def _statement_text(problem, locale: str) -> str:
value = (
problem.statement_i18n.get(locale, "")
if isinstance(problem.statement_i18n, dict)
else ""
)
return value or ""


def statement_lint(problem) -> list[dict]:
"""Item 123 — bilingual statement lint; one warning dict per finding."""
warnings: list[dict] = []
locales = []
for locale in ("en", "ro"):
text = _statement_text(problem, locale)
title = (
problem.title_i18n.get(locale, "")
if isinstance(problem.title_i18n, dict)
else ""
)
if text or title:
locales.append(locale)
lowered = text.lower()
for key in SECTION_KEYS:
if key == "examples":
if text and not any(word.lower() in lowered for word in EXAMPLE_WORDS):
warnings.append(
{
"code": f"missing_examples_{locale}",
"locale": locale,
"message": (
"The statement never mentions examples — add a "
"worked Example section."
),
Comment on lines +48 to +58

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Statement quality checks lack Romanian equivalents for section headings, causing false-positive lint warnings on valid Romanian statements.
Severity: LOW

Suggested Fix

Extend the heading checks for input, output, and constraints to accept their localized Romanian equivalents (e.g., 'date de intrare', 'date de ieșire', 'restricții').

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: backend/problems/statement_quality.py#L48-L58

Potential issue: The statement quality checker evaluates `input`, `output`, and
`constraints` headings only against English terms across all locales. Romanian
statements following standard Romanian headings (such as 'Date de intrare', 'Date de
ieșire', and 'Restricții') fail substring matching, generating false-positive lint
warnings (`missing_input_ro`, `missing_output_ro`, `missing_constraints_ro`).

Did we get this right? 👍 / 👎 to inform future reviews.

}
)
continue
if text and key not in lowered:
warnings.append(
{
"code": f"missing_{key}_{locale}",
Comment on lines +62 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recognize Romanian section headings in Romanian statements

For a Romanian statement with normal headings such as Date de intrare, Date de ieșire, and Restricții, this check emits all three missing-section warnings because it searches exclusively for the English keys. Consequently, a correctly translated statement cannot get a clean lint report without inserting English headings. Use locale-specific heading aliases, as the examples check already attempts to do, and cover an actual Romanian statement in the clean-statement test.

Useful? React with 👍 / 👎.

"locale": locale,
"message": (
f"No '{key}' section found — statements should name "
"Input, Output and Constraints explicitly."
),
}
)
# Unbalanced inline math delimiters ($...$ needs an even count).
dollars = text.count("$")
if dollars % 2 == 1:
warnings.append(
{
"code": f"unbalanced_math_{locale}",
"locale": locale,
"message": "Odd number of '$' delimiters — inline math is unbalanced.",
}
)
# Wall-of-text: any single sentence over MAX_SENTENCE_WORDS words.
for sentence in re.split(r"[.!?]", text):
if len(sentence.split()) > MAX_SENTENCE_WORDS:
warnings.append(
{
"code": f"runaway_sentence_{locale}",
"locale": locale,
"message": (
f"A sentence exceeds {MAX_SENTENCE_WORDS} words — "
"split it for learner readability."
),
}
)
break
# Parity drift: a locale present in the title but missing its statement.
for locale in ("en", "ro"):
title = (
problem.title_i18n.get(locale, "")
if isinstance(problem.title_i18n, dict)
else ""
)
statement = _statement_text(problem, locale)
if title and not statement:
warnings.append(
{
"code": f"statement_missing_{locale}",
"locale": locale,
"message": (
f"The {locale.upper()} title exists but the statement is "
"empty — finish the translation before submitting."
),
}
)
return warnings


# ---------------------------------------------------------------- item 134


def _syllables_ro(word: str) -> int:
"""Romanian syllables ≈ vowel groups (a e i o u ă â î), minimum 1."""
groups = re.findall(r"[aeiouăâî]+", word.lower())
return max(1, len(groups))


def _syllables_en(word: str) -> int:
"""Classic English heuristic; silent trailing e discounted."""
word = word.lower().strip(".,;:!?")
vowels = re.findall(r"[aeiouy]+", word)
count = len(vowels)
if word.endswith("e") and count > 1:
count -= 1
return max(1, count)


def _sentences(text: str) -> int:
return max(1, len(re.findall(r"[.!?](?:\s|$)", text)))


def readability_score(text: str, locale: str = "en") -> dict:
"""Flesch reading ease adapted per language.

EN: 206.835 − 1.015·(words/sentence) − 84.6·(syllables/word).
RO: same skeleton with the vowel-group syllable counter (the standard
adaptation for Romance orthographies).
"""
words = re.findall(r"[A-Za-zĂÂÎȘȚăâîșț]+", text)
word_count = len(words)
if word_count == 0:
return {"score": None, "band": None, "words": 0, "sentences": 0}
counter = _syllables_ro if locale == "ro" else _syllables_en
syllables = sum(counter(word) for word in words)
sentence_count = _sentences(text)
score = (
206.835
- 1.015 * (word_count / sentence_count)
- 84.6 * (syllables / word_count)
)
score = round(max(0.0, min(100.0, score)), 1)
if score >= 80:
band = "very_easy"
elif score >= 60:
band = "plain"
elif score >= 40:
band = "standard"
elif score >= 20:
band = "difficult"
else:
band = "very_difficult"
return {
"score": score,
"band": band,
"words": word_count,
"sentences": sentence_count,
}


def problem_readability(problem) -> dict:
"""Item 134 — per-locale readability of the statement + a consistency
verdict (locales within 15 points read consistently)."""
report: dict = {"locales": {}, "consistent": None}
scores = []
for locale in ("en", "ro"):
text = _statement_text(problem, locale)
if not text:
continue
entry = readability_score(text, locale)
report["locales"][locale] = entry
scores.append(entry["score"])
if len(scores) == 2:
report["consistent"] = abs(scores[0] - scores[1]) <= 15
Comment on lines +186 to +193

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Comparing readability scores across two locales raises a TypeError if one locale contains non-letter placeholders resulting in a None score.
Severity: MEDIUM

Suggested Fix

Filter out None values from scores or verify both scores are not None before calculating their difference in problem_readability.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: backend/problems/statement_quality.py#L186-L193

Potential issue: In `problem_readability`, non-empty statement strings containing no
alphabetical characters (such as placeholders, punctuation, or bare formulas) pass the
`if not text:` guard. `readability_score` subsequently returns `None` for the score and
appends `None` to `scores`. When both locales are present, `len(scores) == 2` evaluates
`abs(scores[0] - scores[1])`, raising a `TypeError: unsupported operand type(s) for -:
'NoneType' and 'float'` and resulting in an unhandled 500 error when fetching
readability metrics.

Did we get this right? 👍 / 👎 to inform future reviews.

Comment on lines +191 to +193

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude unscorable locales from the consistency calculation

When both locale statements are nonempty but either contains no characters matched by the word regex, readability_score returns score: None, which is appended here and then subtracted. For example, statements containing 123 + 456 in both locales reproduce a TypeError, turning the readability endpoint into a 500. Such nonempty strings pass the existing localized-field validator. Calculate consistency only when both scores are numeric, leaving it null otherwise.

Useful? React with 👍 / 👎.

return report


# ---------------------------------------------------------------- item 124


SIDECAR_FIELDS = ("title", "statement", "hints")


def translation_gaps(problem) -> dict:
"""Item 124 — the EN+translation sidecar report: per-locale presence
of every localizable field, so the dual-pane authoring UI can show
exactly what remains to translate."""

def field(locale: str, name: str) -> bool:
if name == "title":
value = (
problem.title_i18n.get(locale, "")
if isinstance(problem.title_i18n, dict)
else ""
)
elif name == "hints":
hints = problem.hints_ro if locale == "ro" else problem.hints_en
value = "\n".join(hints or [])
else:
value = _statement_text(problem, locale)
return bool((value or "").strip())

locales = {}
for locale in ("en", "ro"):
locales[locale] = {name: field(locale, name) for name in SIDECAR_FIELDS}
return {
"fields": list(SIDECAR_FIELDS),
"locales": locales,
"complete": all(all(present.values()) for present in locales.values()),
}
Loading
Loading