逻辑改进
This commit is contained in:
103
compositions.py
103
compositions.py
@@ -4,11 +4,11 @@ from textual.widgets import Collapsible, Header, Footer, Markdown, ListView, Lis
|
||||
from textual.containers import Container, Horizontal, Center
|
||||
from textual.screen import Screen
|
||||
from textual.widget import Widget
|
||||
import pathlib
|
||||
import uuid
|
||||
from typing import Tuple, Dict
|
||||
import particles as pt
|
||||
import uuid
|
||||
from functools import wraps
|
||||
import puzzles as pz
|
||||
import re
|
||||
|
||||
class Composition():
|
||||
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
||||
@@ -24,12 +24,15 @@ class Composition():
|
||||
if id_ not in self.reg.keys():
|
||||
return "None"
|
||||
return self.reg[id_]
|
||||
def recid(self, id_):
|
||||
return id_[:-36]
|
||||
def compose(self):
|
||||
yield Label("示例标签", id="testlabel")
|
||||
yield Button("示例按钮", id="testbtn")
|
||||
def handler(self, event):
|
||||
print(event.button.id)
|
||||
self.screen.query_one("#testlabel", Label).update("hi")
|
||||
def handler(self, event, type_):
|
||||
if hasattr(event, "button"):
|
||||
#print(event.button.id)
|
||||
self.screen.query_one("#testlabel", Label).update("hi")
|
||||
|
||||
class Placeholder(Composition):
|
||||
def __init__(self, screen: Screen):
|
||||
@@ -37,8 +40,8 @@ class Placeholder(Composition):
|
||||
def compose(self):
|
||||
yield Label("示例标签", id="testlabel")
|
||||
yield Button("示例按钮", id="testbtn", classes="choice")
|
||||
def handler(self, event):
|
||||
print(event.button.id)
|
||||
def handler(self, event, type_):
|
||||
#print(event.button.id)
|
||||
self.screen.query_one("#testlabel", Label).update("hi")
|
||||
|
||||
class Recognition(Composition):
|
||||
@@ -48,17 +51,32 @@ class Recognition(Composition):
|
||||
with Center():
|
||||
yield Static(f"[dim]{self.atom[1]['translation']}[/]")
|
||||
yield Label(f"")
|
||||
with Center():
|
||||
yield Label(f"[b][b]{self.atom[1]['content']}[/][/]", id=self.regid("sentence")) # 致敬传奇去重串 uuid
|
||||
s = str(self.atom[1]['content'])
|
||||
replace_dict = {
|
||||
", ": ",",
|
||||
". ": ".",
|
||||
"; ": ";",
|
||||
": ": ":",
|
||||
"/,": ",",
|
||||
"./": ".",
|
||||
"/;": ";",
|
||||
";/": ";",
|
||||
":/": ":",
|
||||
}
|
||||
for old, new in replace_dict.items():
|
||||
s = s.replace(old, new)
|
||||
result = re.split(r"(?<=[,;:|])", s.replace('/', ' '))
|
||||
for i in result:
|
||||
with Center():
|
||||
yield Label(f"[b][b]{i.replace("/", " ")}[/][/]", id=self.regid("sentence"+str(hash(i)))) # 致敬传奇去重串 uuid
|
||||
#with Collapsible(title="附加信息", collapsed=True):
|
||||
for i in self.atom[2]["testdata"]["additional_inf"]:
|
||||
if self.atom[1][i]:
|
||||
print(type(self.atom[1][i]))
|
||||
print(self.atom[1][i])
|
||||
#print(type(self.atom[1][i]))
|
||||
#print(self.atom[1][i])
|
||||
if isinstance(self.atom[1][i], list):
|
||||
for j in self.atom[1][i]:
|
||||
print(999)
|
||||
yield Markdown(f"### {self.atom[2]["keydata"][i]}: {j}")
|
||||
yield Markdown(f"### {self.atom[2]['keydata'][i]}: {j}")
|
||||
continue
|
||||
if isinstance(self.atom[1][i], Dict):
|
||||
t = ""
|
||||
@@ -67,33 +85,52 @@ class Recognition(Composition):
|
||||
yield Markdown(t, id=self.regid("tran"))
|
||||
with Center():
|
||||
yield Button("我已知晓", id=self.regid("ok"))
|
||||
def handler(self, event):
|
||||
if event.button.id == self.getid("ok"):
|
||||
print(1)
|
||||
""""""#assessment = self.reactor.report(self.reactor.current_atom, -1)
|
||||
return 0
|
||||
def handler(self, event, type_):
|
||||
##print(event)
|
||||
if type_ == "button":
|
||||
#print(event.button.id)
|
||||
if event.button.id == self.getid("ok"):
|
||||
#print(1)
|
||||
""""""#assessment = self.reactor.report(self.reactor.current_atom, -1)
|
||||
return 0
|
||||
if type_ == 1:
|
||||
pass
|
||||
return -1
|
||||
|
||||
class BasicEvaluation(Composition):
|
||||
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
||||
super().__init__(screen, reactor, atom)
|
||||
def compose(self):
|
||||
yield Label(self.atom[1]["content"], id="sentence")
|
||||
for i in self.atom[2]["testdata"]["additional_inf"]:
|
||||
yield Label(f"{self.atom[2]["keydata"][i]}: {self.atom[1][i]}", id=f"label_{i}")
|
||||
yield Button("我已知晓", id="ok")
|
||||
def handler(self, event):
|
||||
if event.button.id == "ok":
|
||||
return 1
|
||||
with Container(id="button_container"):
|
||||
btn = {}
|
||||
btn['5'] = Button("完美回想", variant="success", id=self.regid("feedback5"), classes="choice")
|
||||
btn['4'] = Button("犹豫后正确", variant="success", id=self.regid("feedback4"), classes="choice")
|
||||
btn['3'] = Button("困难地正确", variant="warning", id=self.regid("feedback3"), classes="choice")
|
||||
btn['2'] = Button("错误但熟悉", variant="warning", id=self.regid("feedback2"), classes="choice")
|
||||
btn['1'] = Button("错误且不熟", variant="error", id=self.regid("feedback1"), classes="choice")
|
||||
btn['0'] = Button("完全空白", variant="error", id=self.regid("feedback0"), classes="choice")
|
||||
yield Horizontal(btn['5'], btn['4'])
|
||||
yield Horizontal(btn['3'], btn['2'])
|
||||
yield Horizontal(btn['1'], btn['0'])
|
||||
def handler(self, event, type_):
|
||||
if "feedback" in event.button.id:
|
||||
#print(self.recid(event.button.id)[8:9])
|
||||
assess = int(self.recid(event.button.id)[8:9])
|
||||
ret = self.reactor.report(self.atom, assess)
|
||||
return ret
|
||||
|
||||
class FillBlank(Composition):
|
||||
def __init__(self, screen: Screen, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
||||
""""""#super().__init__(screen, atom)
|
||||
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
|
||||
super().__init__(screen, reactor, atom)
|
||||
def _work(self):
|
||||
self.puzzle = pz.BlankPuzzle(self.atom[1]["content"], 4)
|
||||
self.puzzle.refresh()
|
||||
def compose(self):
|
||||
yield Label(self.atom[1]["content"], id="sentence")
|
||||
for i in self.atom[2]["testdata"]["additional_inf"]:
|
||||
yield Label(f"{self.atom[2]["keydata"][i]}: {self.atom[1][i]}", id=f"label_{i}")
|
||||
self._work()
|
||||
yield Label(self.puzzle.wording, id="sentence")
|
||||
yield Button("我已知晓", id="ok")
|
||||
def handler(self, event):
|
||||
def handler(self, event, type_):
|
||||
if event.button.id == "ok":
|
||||
return 1
|
||||
|
||||
@@ -101,7 +138,7 @@ class FillBlank(Composition):
|
||||
registry = {
|
||||
"sample": Composition,
|
||||
"recognition": Recognition,
|
||||
"fill_blank_test": FillBlank,
|
||||
"fill_blank_test": BasicEvaluation,
|
||||
"draw_card_test": BasicEvaluation,
|
||||
"basic_evaluation": BasicEvaluation,
|
||||
}
|
||||
@@ -121,7 +158,7 @@ class TestScreen(Screen):
|
||||
pass
|
||||
|
||||
def on_button_pressed(self, event: Event) -> None:
|
||||
self.comp.handler(event)
|
||||
self.comp.handler(event, "button")
|
||||
|
||||
def action_quit_app(self) -> None:
|
||||
self.app.exit()
|
||||
|
Reference in New Issue
Block a user