实现 Recognition 部件

This commit is contained in:
2025-11-08 13:23:15 +08:00
parent 92f9903307
commit 330846a4a5
8 changed files with 108 additions and 54 deletions

View File

@@ -25,7 +25,7 @@ class BasicEvaluation(BasePuzzleWidget):
}
def compose(self):
yield Label(self.atom.register["nucleon"]["content"], id="main")
yield Label(self.atom.registry["nucleon"]["content"], id="main")
with Container(id="button_container"):
btn = {}
btn["5"] = Button(

View File

@@ -23,7 +23,7 @@ class ClozePuzzle(BasePuzzleWidget):
self._work()
def _work(self):
self.puzzle = pz.ClozePuzzle(text=self.atom.register["nucleon"]["content"], min_denominator=2)
self.puzzle = pz.ClozePuzzle(text=self.atom.registry["nucleon"]["content"], min_denominator=2)
self.puzzle.refresh()
self.ans = copy.copy(self.puzzle.answer)
random.shuffle(self.ans)

View File

@@ -24,56 +24,72 @@ import copy
from .base_puzzle_widget import BasePuzzleWidget
from heurams.context import config_var
from .. import shim
from typing import TypedDict, List
class RecognitionConfig(TypedDict):
__origin__: str
__hint__: str
primary: str
secondary: List[str]
top_dim: List[str]
class Recognition(BasePuzzleWidget):
def __init__(self, *children: Widget, atom: pt.Atom, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
rate_mapping = {
"ok": 5
}
def __init__(self, *children: Widget, atom: pt.Atom, alia: str = "", name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None:
super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup)
if alia == "":
alia = "Recognition"
self.alia = alia
def compose(self):
with Center():
yield Static(f"[dim]{self.atom.register['nucleon']['translation']}[/]")
yield Label(f"")
s = str(self.atom.register['nucleon']["content"])
cfg: RecognitionConfig = self.atom.registry["orbital"]["puzzles"][self.alia]
delim = self.atom.registry['nucleon'].metadata["formation"]["delimiter"]
replace_dict = {
", ": ",",
". ": ".",
"; ": ";",
": ": ":",
"/,": ",",
"./": ".",
"/;": ";",
";/": ";",
":/": ":",
f"{delim},": ",",
f".{delim}": ".",
f"{delim};": ";",
f";{delim}": ";",
f":{delim}": ":",
}
nucleon = self.atom.registry['nucleon']
metadata = self.atom.registry['nucleon'].metadata
primary = nucleon[cfg["primary"]]
with Center():
yield Static(f"[dim]{nucleon[cfg["top_dim"]]}[/]")
yield Label(f"")
for old, new in replace_dict.items():
s = s.replace(old, new)
result = re.split(r"(?<=[,;:|])", s.replace("/", " "))
for i in result:
primary = primary.replace(old, new)
primary_splited = re.split(r"(?<=[,;:|])", cfg['primary'])
for item in primary_splited:
with Center():
yield Label(
f"[b][b]{i.replace('/', ' ')}[/][/]",
id="sentence" + str(hash(i)),
f"[b][b]{item.replace(delim, ' ')}[/][/]",
id="sentence" + str(hash(item)),
)
# 处理orbital/展示配置
for i in
# eval 环境设置
nucleon = self.atom.register['nucleon']
default = config_var.get()["puzzles"]
metadata = nucleon.metadata
for item in nucleon[cfg["secondary"]]:
if isinstance(item, list):
for j in item:
yield Markdown(f"### {metadata["annotation"][item]}: {j}")
continue
if isinstance(item, Dict):
total = ""
for j, k in item.items(): # type: ignore
total += f"> **{j}**: {k} \n"
yield Markdown(total)
if isinstance(item, str):
yield Markdown(item)
for i in self.atom.register["orbital"] ["testdata"]["additional_inf"]:
if self.atom.register['nucleon'][i]:
if isinstance(self.atom.register['nucleon'][i], list):
for j in self.atom.register['nucleon'][i]:
yield Markdown(f"### {self.atom.register["orbital"] ['keydata'][i]}: {j}")
continue
if isinstance(self.atom.register['nucleon'][i], Dict):
t = ""
for j, k in self.atom.register['nucleon'][i].items(): # type: ignore
t += f"> **{j}**: {k} \n"
yield Markdown(t, id="tran")
with Center():
yield Button("我已知晓", id="ok")