feat(interface): 组件自动聚焦与键盘操作改进

This commit is contained in:
2026-04-22 22:54:25 +08:00
parent c2a1867c49
commit f50c19ba82
24 changed files with 173 additions and 12258 deletions

View File

@@ -1,4 +1,4 @@
zmq_debug = true
zmq_debug = false
_zmq_debug_desc = "[调试] ZeroMQ 调试服务器, 这会在 zmq_debug_port 上打开一个服务器\n调试工具可远程在 HeurAMS 内执行任意 python 代码, 无必要请关闭"
zmq_debug_port = 5555
_zmq_debug_port_desc = "[调试] ZeroMQ 调试服务器端口"
@@ -15,3 +15,7 @@ config = "./data/config"
_config_desc = "配置文件根目录"
repo = "./data/repo"
_repo_desc = "记忆单元集根目录"
misc = "./data/misc"
_misc_desc = "扩展程序和 whisper 等模块的数据存储根目录"
_addons = "./data/addons"
__addons_desc = "扩展程序根目录"

View File

@@ -1,2 +1,2 @@
min_denominator = "3"
min_denominator = "2"
_min_denominator_desc = "设空比例系数的倒数"

View File

@@ -1,2 +1,2 @@
_cngk-t_desc = "高考必备古诗文"
_cngk-t_desc = "高考必备古诗文-测试"
_cngk_desc = "高考必备古诗文"

BIN
data/misc/attics/ana.pkl Normal file

Binary file not shown.

View File

@@ -0,0 +1 @@
[]

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
title = "高考必背古诗文-0"
package = "cngk-0"
title = "高考必背古诗文-筛选"
package = "cngk-t"
author = "__heurams__"
desc = "高考古诗文 60 篇"

View File

@@ -1,764 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "51b89355",
"metadata": {},
"source": [
"# 演练场\n",
"此笔记本将带你了解 repomgr 与 particles 对象相关操作 \n",
"此笔记本内含的系统命令默认仅存在于 Linux 操作系统, 如果你使用 Windows, 请在安装 busybox 或 cygwin 或 WSL 的环境下执行此笔记本"
]
},
{
"cell_type": "markdown",
"id": "f5c49014",
"metadata": {},
"source": [
"# 从一个例子开始\n",
"## 了解文件结构\n",
"了解一下文件结构"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a5ed9864",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[01;34m.\u001b[0m\n",
"├── \u001b[01;34mdata\u001b[0m\n",
"│   └── \u001b[01;34mconfig\u001b[0m\n",
"│   └── \u001b[00mconfig.toml\u001b[0m\n",
"├── \u001b[00mjiebatest.py\u001b[0m\n",
"├── \u001b[00mrepo.ipynb\u001b[0m\n",
"├── \u001b[00msimplemem.py\u001b[0m\n",
"└── \u001b[01;34mtest_repo\u001b[0m\n",
" ├── \u001b[00malgodata.json\u001b[0m\n",
" ├── \u001b[00mmanifest.toml\u001b[0m\n",
" ├── \u001b[00mpayload.toml\u001b[0m\n",
" ├── \u001b[00mschedule.toml\u001b[0m\n",
" └── \u001b[00mtypedef.toml\u001b[0m\n",
"\n",
"4 directories, 9 files\n"
]
}
],
"source": [
"!tree # 了解文件结构"
]
},
{
"cell_type": "markdown",
"id": "4e10922b",
"metadata": {},
"source": [
"如果你先前运行了单元格, 请运行下面一格清理."
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "9777730e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"zsh:1: no matches found: heurams.log*\n"
]
}
],
"source": [
"!rm -rf test_new_repo\n",
"!rm -rf heurams.log*"
]
},
{
"cell_type": "markdown",
"id": "058c098f",
"metadata": {},
"source": [
"## 导入模块\n",
"导入所需模块, 你会看到欢迎信息, 标示了库所使用的配置. \n",
"HeurAMS 在基础设施也使用配置文件实现隐式的依赖注入. "
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "bf1b00c8",
"metadata": {},
"outputs": [],
"source": [
"import heurams.kernel.repolib as repolib # 这是 RepoLib 子模块, 用于管理和结构化 repo(中文含义: 仓库) 数据结构与本地文件间的联系\n",
"import heurams.kernel.particles as pt # 这是 Particles(中文含义: 粒子) 子模块, 用于运行时的记忆管理操作\n",
"from pathlib import (\n",
" Path,\n",
") # 这是 Python 的 Pathlib 模块, 用于表示文件路径, 在整个项目中, 都使用此模块表示路径"
]
},
{
"cell_type": "markdown",
"id": "ea1f68bb",
"metadata": {},
"source": [
"## 运行时检查\n",
"如你所见, repo 在文件系统内存储为一个文件夹. \n",
"因此在载入之前, 首先要检查这是否是一个合乎标准的 repo 文件夹. "
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "897b62d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"这是一个 合规 的 repo!\n"
]
}
],
"source": [
"is_vaild = repolib.Repo.check_repodir(Path(\"./test_repo\"))\n",
"print(f\"这是一个 {'合规' if is_vaild else '不合规'} 的 repo!\")"
]
},
{
"cell_type": "markdown",
"id": "24a19991",
"metadata": {},
"source": [
"## 加载仓库\n",
"接下来, 正式加载 repo."
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "708ae7e4",
"metadata": {},
"outputs": [],
"source": [
"test_repo = repolib.Repo.create_from_repodir(Path(\"./test_repo\"))"
]
},
{
"cell_type": "markdown",
"id": "474f8eb7",
"metadata": {},
"source": [
"## 导出为字典\n",
"作为一个数据容器, repo 相应地建立了导入和导出的功能. \n",
"我们刚刚从本地文件夹导入了一个 repo. \n",
"现在试试导出为一个字典."
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "a11115fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'algodata': [('秦孝公据崤函之固, 拥雍州之地,', {}), ('君臣固守以窥周室,', {})],\n",
" 'manifest': {'author': '__heurams__',\n",
" 'desc': '高考古诗文: 过秦论',\n",
" 'title': '测试单元: 过秦论'},\n",
" 'payload': [('秦孝公据崤函之固, 拥雍州之地,',\n",
" {'content': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/',\n",
" 'keyword_note': {'崤函': '崤山和函谷关', '据': '占据', '雍州': '古代九州之一'},\n",
" 'note': [],\n",
" 'translation': '秦孝公占据着崤山和函谷关的险固地势,拥有雍州的土地,'}),\n",
" ('君臣固守以窥周室,',\n",
" {'content': '君臣/固守/以窥/周室,/',\n",
" 'keyword_note': {'窥': '窥视'},\n",
" 'note': [],\n",
" 'translation': '君臣牢固地守卫着,借以窥视周王室的权力,'})],\n",
" 'schedule': {'phases': {'final_review': [['FillBlank', '0.7'],\n",
" ['SelectMeaning', '0.7'],\n",
" ['Recognition', '1.0']],\n",
" 'quick_review': [['FillBlank', '1.0'],\n",
" ['SelectMeaning', '0.5'],\n",
" ['Recognition', '1.0']],\n",
" 'recognition': [['Recognition', '1.0']]},\n",
" 'schedule': ['quick_review', 'recognition', 'final_review']},\n",
" 'source': PosixPath('test_repo'),\n",
" 'typedef': {'annotation': {'content': '内容',\n",
" 'delimiter': '分隔符',\n",
" 'keyword_note': '关键词翻译',\n",
" 'note': '笔记',\n",
" 'translation': '语句翻译',\n",
" 'tts_text': '文本转语音文本'},\n",
" 'common': {'delimiter': '/',\n",
" 'puzzles': {'FillBlank': {'__hint__': '',\n",
" '__origin__': 'cloze',\n",
" 'delimiter': \"eval:nucleon['delimiter']\",\n",
" 'min_denominator': \"eval:default['cloze']['min_denominator']\",\n",
" 'text': \"eval:payload['content']\"},\n",
" 'Recognition': {'__hint__': '',\n",
" '__origin__': 'recognition',\n",
" 'primary': \"eval:payload['content']\",\n",
" 'secondary': [\"eval:payload['keyword_note']\",\n",
" \"eval:payload['note']\"],\n",
" 'top_dim': [\"eval:payload['translation']\"]},\n",
" 'SelectMeaning': {'__hint__': \"eval:payload['content']\",\n",
" '__origin__': 'mcq',\n",
" 'jammer': \"eval:list(payload['keyword_note'].values())\",\n",
" 'mapping': \"eval:payload['keyword_note']\",\n",
" 'max_riddles_num': \"eval:default['mcq']['max_riddles_num']\",\n",
" 'prefix': '选择正确项: ',\n",
" 'primary': \"eval:payload['content']\"}},\n",
" 'tts_text': \"eval:payload['content'].replace('/', \"\n",
" \"'')\"}}}\n"
]
}
],
"source": [
"test_repo_dic = test_repo.export_to_single_dict()\n",
"from pprint import pprint\n",
"\n",
"pprint(test_repo_dic)"
]
},
{
"cell_type": "markdown",
"id": "35a2e06f",
"metadata": {},
"source": [
"## 持久化与部分保存\n",
"如你所见, 所有内容被结构化地输出了! \n",
"\n",
"现在写回到文件夹! \n",
"\n",
"我们注意到, 并非所有的内容都要被修改. \n",
"我们可以只保存接受修改的一部分, 默认情况下, 是迭代的记忆数据(algodata). \n",
"这就是为什么我们一般不使用单个 json 或 toml 来存储 repo.\n",
"\n",
"persist_to_repodir 接受两个可选参数: \n",
"- save_list: 默认为 [\"algodata\"], 是要持久化的数据.\n",
"- source: 默认为原目录, 你也可以手动指定为其他文件夹(通过 Path)\n",
"\n",
"现在做一些演练, 我们将创建一个位于 test_new_repo 的\"克隆\". \n",
"除非文件夹已经存在, Repo 对象将会为你自动创建新文件夹."
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "05eeaacc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[01;34m.\u001b[0m\n",
"├── \u001b[01;34mdata\u001b[0m\n",
"│   └── \u001b[01;34mconfig\u001b[0m\n",
"│   └── \u001b[00mconfig.toml\u001b[0m\n",
"├── \u001b[00mjiebatest.py\u001b[0m\n",
"├── \u001b[00mrepo.ipynb\u001b[0m\n",
"├── \u001b[00msimplemem.py\u001b[0m\n",
"├── \u001b[01;34mtest_new_repo\u001b[0m\n",
"│   ├── \u001b[00malgodata.json\u001b[0m\n",
"│   ├── \u001b[00mmanifest.toml\u001b[0m\n",
"│   ├── \u001b[00mpayload.toml\u001b[0m\n",
"│   ├── \u001b[00mschedule.toml\u001b[0m\n",
"│   └── \u001b[00mtypedef.toml\u001b[0m\n",
"└── \u001b[01;34mtest_repo\u001b[0m\n",
" ├── \u001b[00malgodata.json\u001b[0m\n",
" ├── \u001b[00mmanifest.toml\u001b[0m\n",
" ├── \u001b[00mpayload.toml\u001b[0m\n",
" ├── \u001b[00mschedule.toml\u001b[0m\n",
" └── \u001b[00mtypedef.toml\u001b[0m\n",
"\n",
"5 directories, 14 files\n"
]
}
],
"source": [
"test_repo.persist_to_repodir(\n",
" save_list=[\"schedule\", \"payload\", \"manifest\", \"typedef\", \"algodata\"],\n",
" source=Path(\"test_new_repo\"),\n",
")\n",
"!tree"
]
},
{
"cell_type": "markdown",
"id": "059d7bdf",
"metadata": {},
"source": [
"如你所见, test_new_repo 已被生成!"
]
},
{
"cell_type": "markdown",
"id": "4ef8925c",
"metadata": {},
"source": [
"# 数据结构\n",
"现在讲解 repo 的数据结构"
]
},
{
"cell_type": "markdown",
"id": "c19fed95",
"metadata": {},
"source": [
"## Lict 对象\n",
"Lict 对象集成了部分列表和字典的功能, 数据在这两种风格的 API 间都可用, 且修改是同步的. \n",
"Lict 默认情况下不会保存序列顺序, 而是在列表形式下, 自动按索引字符序排布, 详情请参阅源代码. \n",
"现在导入并初始化一个 Lict 对象:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "7e88bd7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('name', 'tom'), ('age', 12), ('enemy', 'jerry')]\n",
"[('name', 'tom'), ('age', 12), ('enemy', 'jerry')]\n"
]
}
],
"source": [
"from heurams.kernel.auxiliary.lict import Lict\n",
"\n",
"lct = Lict() # 空的\n",
"lct = Lict(initlist=[(\"name\", \"tom\"), (\"age\", 12), (\"enemy\", \"jerry\")]) # 基于列表\n",
"print(lct)\n",
"lct = Lict(initdict={\"name\": \"tom\", \"age\": 12, \"enemy\": \"jerry\"}) # 基于字典\n",
"print(lct)"
]
},
{
"cell_type": "markdown",
"id": "4d760bf9",
"metadata": {},
"source": [
"### 输出形式\n",
"Lict 的\"官方\"输出形式是列表形式\n",
"你也可以选择输出字典形式"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "248f6cba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'tom', 'age': 12, 'enemy': 'jerry'}\n"
]
}
],
"source": [
"print(lct.dicted_data)"
]
},
{
"cell_type": "markdown",
"id": "29dce184",
"metadata": {},
"source": [
"### dicted_data 属性与修改方式\n",
"dicted_data 属性是一个字典, 它自动同步来自 Lict 对象操作的修改.\n",
"一个注意事项: 不要直接修改 dicted_data, 这将不会触发同步 hook.\n",
"如果你一定要这样做, 请在完事后手动运行同步 hook.\n",
"推荐的修改方式是直接把 lct 当作一个字典"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "a0eb07a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('name', 'tom'), ('age', 12), ('enemy', 'jerry')]\n",
"[('name', 'tom'), ('age', 12), ('enemy', 'jerry'), ('type', 'cat')]\n",
"[('name', 'tom'), ('age', 12), ('enemy', 'jerry'), ('type', 'cat'), ('is_human', False)]\n"
]
}
],
"source": [
"# 由于 jupyter 的环境处理, 请不要重复运行此单元格, 如果想再看一遍, 请重启 jupyter 后再全部运行\n",
"\n",
"# 错误的方式\n",
"lct.dicted_data[\"type\"] = \"cat\"\n",
"print(lct) # 将不会同步修改\n",
"\n",
"# 不推荐, 但可用的方式\n",
"lct.dicted_data[\"type\"] = \"cat\"\n",
"lct._sync_based_on_dict()\n",
"print(lct)\n",
"\n",
"# 推荐方式\n",
"lct[\"is_human\"] = False\n",
"print(lct)"
]
},
{
"cell_type": "markdown",
"id": "2337d113",
"metadata": {},
"source": [
"### data 属性与修改方式\n",
"data 属性是一个列表, 它自动同步来自 Lict 对象操作的修改.\n",
"一个注意事项: 不要直接修改 data, 这将不会触发同步 hook, 并且可能破坏排序.\n",
"如果你一定要这样做, 请在完事后手动运行同步 hook 和 sort, 此处不演示.\n",
"推荐的修改方式是直接把 lct 当作一个列表, 且避免使用索引修改"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ab442d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'tom', 'age': 12, 'enemy': 'jerry', 'type': 'cat', 'is_human': False, 'enemy_2': 'spike'}\n"
]
}
],
"source": [
"# 由于 Jupyter 的环境处理(环境状态会累积), 请不要重复运行此单元格, 如果想再看一遍, 请重启 jupyter 后再全部运行\n",
"\n",
"# 唯一推荐方式\n",
"lct.append((\"enemy_2\", \"spike\"))\n",
"print(lct.dicted_data)"
]
},
{
"cell_type": "markdown",
"id": "a3383f59",
"metadata": {},
"source": [
"### 多面手\n",
"Lict 有一些很酷的功能\n",
"详情请看源文件\n",
"此处是一些例子"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "f3ca752f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('age', 12), ('enemy', 'jerry'), ('is_human', False), ('name', 'tom'), ('type', 'cat'), ('enemy_2', 'spike')]\n",
"{'age': 12, 'enemy': 'jerry', 'is_human': False, 'name': 'tom', 'type': 'cat', 'enemy_2': 'spike'}\n",
"------\n",
"('age', 12)\n",
"('enemy', 'jerry')\n",
"('is_human', False)\n",
"('name', 'tom')\n",
"('type', 'cat')\n",
"('enemy_2', 'spike')\n",
"6\n",
"('enemy_2', 'spike')\n",
"[('age', 12), ('enemy', 'jerry'), ('is_human', False), ('name', 'tom'), ('type', 'cat')]\n",
"('type', 'cat')\n",
"[('age', 12), ('enemy', 'jerry'), ('is_human', False), ('name', 'tom')]\n",
"('name', 'tom')\n",
"[('age', 12), ('enemy', 'jerry'), ('is_human', False)]\n",
"('is_human', False)\n",
"[('age', 12), ('enemy', 'jerry')]\n",
"('enemy', 'jerry')\n",
"[('age', 12)]\n",
"('age', 12)\n",
"[]\n"
]
},
{
"data": {
"text/plain": [
"Ellipsis"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lct = Lict(\n",
" initdict={\n",
" \"age\": 12,\n",
" \"enemy\": \"jerry\",\n",
" \"is_human\": False,\n",
" \"name\": \"tom\",\n",
" \"type\": \"cat\",\n",
" \"enemy_2\": \"spike\",\n",
" }\n",
")\n",
"print(lct)\n",
"print(lct.dicted_data)\n",
"print(\"------\")\n",
"for i in lct:\n",
" print(i)\n",
"print(len(lct))\n",
"while len(lct) > 0:\n",
" print(lct.pop())\n",
" print(lct)\n",
"lct = Lict(\n",
" initdict={\n",
" \"age\": 12,\n",
" \"enemy\": \"jerry\",\n",
" \"is_human\": False,\n",
" \"name\": \"tom\",\n",
" \"type\": \"cat\",\n",
" \"enemy_2\": \"spike\",\n",
" }\n",
")\n",
"..."
]
},
{
"cell_type": "markdown",
"id": "2d6d3483",
"metadata": {},
"source": [
"关爱环境 从你我做起"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "773bf99c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"zsh:1: no matches found: heurams.log*\n"
]
}
],
"source": [
"!rm -rf test_new_repo\n",
"!rm -rf heurams.log*"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "8645c5a2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{ 'content': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/',\n",
" 'delimiter': '/',\n",
" 'keyword_note': {'崤函': '崤山和函谷关', '据': '占据', '雍州': '古代九州之一'},\n",
" 'note': [],\n",
" 'puzzles': { 'FillBlank': { '__hint__': '',\n",
" '__origin__': 'cloze',\n",
" 'delimiter': '/',\n",
" 'min_denominator': 3,\n",
" 'text': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/'},\n",
" 'Recognition': { '__hint__': '',\n",
" '__origin__': 'recognition',\n",
" 'primary': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/',\n",
" 'secondary': [ { '崤函': '崤山和函谷关',\n",
" '据': '占据',\n",
" '雍州': '古代九州之一'},\n",
" []],\n",
" 'top_dim': [ '秦孝公占据着崤山和函谷关的险固地势,拥有雍州的土地,']},\n",
" 'SelectMeaning': { '__hint__': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/',\n",
" '__origin__': 'mcq',\n",
" 'jammer': ['占据', '崤山和函谷关', '古代九州之一'],\n",
" 'mapping': { '崤函': '崤山和函谷关',\n",
" '据': '占据',\n",
" '雍州': '古代九州之一'},\n",
" 'max_riddles_num': 2,\n",
" 'prefix': '选择正确项: ',\n",
" 'primary': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/'}},\n",
" 'translation': '秦孝公占据着崤山和函谷关的险固地势,拥有雍州的土地,',\n",
" 'tts_text': '秦孝公据崤函之固, 拥雍州之地,'}\n",
"{ 'SM-2': { 'efactor': 2.5,\n",
" 'interval': 1,\n",
" 'is_activated': 1,\n",
" 'last_date': 20459,\n",
" 'last_modify': 1767700296.4950516,\n",
" 'next_date': 20460,\n",
" 'real_rept': 1,\n",
" 'rept': 0}}\n",
"{ 'content': '君臣/固守/以窥/周室,/',\n",
" 'delimiter': '/',\n",
" 'keyword_note': {'窥': '窥视'},\n",
" 'note': [],\n",
" 'puzzles': { 'FillBlank': { '__hint__': '',\n",
" '__origin__': 'cloze',\n",
" 'delimiter': '/',\n",
" 'min_denominator': 3,\n",
" 'text': '君臣/固守/以窥/周室,/'},\n",
" 'Recognition': { '__hint__': '',\n",
" '__origin__': 'recognition',\n",
" 'primary': '君臣/固守/以窥/周室,/',\n",
" 'secondary': [{'窥': '窥视'}, []],\n",
" 'top_dim': ['君臣牢固地守卫着,借以窥视周王室的权力,']},\n",
" 'SelectMeaning': { '__hint__': '君臣/固守/以窥/周室,/',\n",
" '__origin__': 'mcq',\n",
" 'jammer': ['窥视'],\n",
" 'mapping': {'窥': '窥视'},\n",
" 'max_riddles_num': 2,\n",
" 'prefix': '选择正确项: ',\n",
" 'primary': '君臣/固守/以窥/周室,/'}},\n",
" 'translation': '君臣牢固地守卫着,借以窥视周王室的权力,',\n",
" 'tts_text': '君臣固守以窥周室,'}\n",
"{ 'SM-2': { 'efactor': 2.5,\n",
" 'interval': 1,\n",
" 'is_activated': 1,\n",
" 'last_date': 20459,\n",
" 'last_modify': 1767700296.4968777,\n",
" 'next_date': 20460,\n",
" 'real_rept': 1,\n",
" 'rept': 0}}\n",
"{ 'algodata': [ ( '秦孝公据崤函之固, 拥雍州之地,',\n",
" { 'SM-2': { 'efactor': 2.5,\n",
" 'interval': 1,\n",
" 'is_activated': 1,\n",
" 'last_date': 20459,\n",
" 'last_modify': 1767700296.4950516,\n",
" 'next_date': 20460,\n",
" 'real_rept': 1,\n",
" 'rept': 0}}),\n",
" ( '君臣固守以窥周室,',\n",
" { 'SM-2': { 'efactor': 2.5,\n",
" 'interval': 1,\n",
" 'is_activated': 1,\n",
" 'last_date': 20459,\n",
" 'last_modify': 1767700296.4968777,\n",
" 'next_date': 20460,\n",
" 'real_rept': 1,\n",
" 'rept': 0}})],\n",
" 'manifest': { 'author': '__heurams__',\n",
" 'desc': '高考古诗文: 过秦论',\n",
" 'title': '测试单元: 过秦论'},\n",
" 'payload': [ ( '秦孝公据崤函之固, 拥雍州之地,',\n",
" { 'content': '秦孝公/据/崤函/之固/, 拥/雍州/之地,/',\n",
" 'keyword_note': { '崤函': '崤山和函谷关',\n",
" '据': '占据',\n",
" '雍州': '古代九州之一'},\n",
" 'note': [],\n",
" 'translation': '秦孝公占据着崤山和函谷关的险固地势,拥有雍州的土地,'}),\n",
" ( '君臣固守以窥周室,',\n",
" { 'content': '君臣/固守/以窥/周室,/',\n",
" 'keyword_note': {'窥': '窥视'},\n",
" 'note': [],\n",
" 'translation': '君臣牢固地守卫着,借以窥视周王室的权力,'})],\n",
" 'schedule': { 'phases': { 'final_review': [ ['FillBlank', '0.7'],\n",
" ['SelectMeaning', '0.7'],\n",
" ['Recognition', '1.0']],\n",
" 'quick_review': [ ['FillBlank', '1.0'],\n",
" ['SelectMeaning', '0.5'],\n",
" ['Recognition', '1.0']],\n",
" 'recognition': [['Recognition', '1.0']]},\n",
" 'schedule': [ 'quick_review',\n",
" 'recognition',\n",
" 'final_review']},\n",
" 'source': PosixPath('test_repo'),\n",
" 'typedef': { 'annotation': { 'content': '内容',\n",
" 'delimiter': '分隔符',\n",
" 'keyword_note': '关键词翻译',\n",
" 'note': '笔记',\n",
" 'translation': '语句翻译',\n",
" 'tts_text': '文本转语音文本'},\n",
" 'common': { 'delimiter': '/',\n",
" 'puzzles': { 'FillBlank': { '__hint__': '',\n",
" '__origin__': 'cloze',\n",
" 'delimiter': \"eval:nucleon['delimiter']\",\n",
" 'min_denominator': \"eval:default['cloze']['min_denominator']\",\n",
" 'text': \"eval:payload['content']\"},\n",
" 'Recognition': { '__hint__': '',\n",
" '__origin__': 'recognition',\n",
" 'primary': \"eval:payload['content']\",\n",
" 'secondary': [ \"eval:payload['keyword_note']\",\n",
" \"eval:payload['note']\"],\n",
" 'top_dim': [ \"eval:payload['translation']\"]},\n",
" 'SelectMeaning': { '__hint__': \"eval:payload['content']\",\n",
" '__origin__': 'mcq',\n",
" 'jammer': \"eval:list(payload['keyword_note'].values())\",\n",
" 'mapping': \"eval:payload['keyword_note']\",\n",
" 'max_riddles_num': \"eval:default['mcq']['max_riddles_num']\",\n",
" 'prefix': '选择正确项: ',\n",
" 'primary': \"eval:payload['content']\"}},\n",
" 'tts_text': \"eval:payload['content'].replace('/', \"\n",
" \"'')\"}}}\n"
]
}
],
"source": [
"repo = repolib.Repo.create_from_repodir(Path(\"./test_repo\"))\n",
"for i in repo.ident_index:\n",
" n = pt.Nucleon.create_on_nucleonic_data(\n",
" nucleonic_data=repo.nucleonic_data_lict.get_itemic_unit(i)\n",
" )\n",
" e = pt.Electron.create_on_electonic_data(\n",
" electronic_data=repo.electronic_data_lict.get_itemic_unit(i)\n",
" )\n",
" e.activate()\n",
" e.revisor(5, True)\n",
" print(repr(n))\n",
" print(repr(e))\n",
"print(repo)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -1,56 +0,0 @@
import time
from pathlib import Path
import heurams.kernel.particles as pt
import heurams.kernel.repolib as repolib
from heurams.services.textproc import truncate
repo = repolib.Repo.from_repodir(Path("./test_repo"))
alist = list()
print(repo.ident_index)
for i in repo.ident_index:
n = pt.Nucleon.from_data(nucleonic_data=repo.nucleonic_data_lict.get_itemic_unit(i))
e = pt.Electron.from_data(
electronic_data=repo.electronic_data_lict.get_itemic_unit(i),
algo_name=repo.config["algorithm"],
)
print(n)
input()
a = pt.Atom(n, e, repo.orbitic_data)
alist.append(a)
# e.activate()
# e.revisor(5, True)
print(repr(a))
# print(repr(e))
print(repo)
input()
import heurams.kernel.reactor as rt
ph: rt.Phaser = rt.Phaser(alist)
print(ph)
pr: rt.Procession = ph.current_procession() # type: ignore
print(pr)
pr.forward()
print(pr)
pr.forward() # 如果过界了?
print(pr) # 静默设置状态 无报错
pr.forward()
print(pr)
pr = ph.current_procession() # type: ignore # 下一个队列
print(pr)
pr.forward()
print(pr)
pr.append() # 如果记忆失败了?
print(pr)
pr.forward()
pr.append() # 如果记忆失败了?
pr.append() # 如果记忆失败了?
pr.append() # 如果记忆失败了?
pr.append() # 如果记忆失败了?
pr.append() # 如果记忆失败了?
# 重复项目只会占据一个车尾
print(pr)
pr.forward()
print(pr)
pr = ph.current_procession() # type: ignore
print(pr)

View File

@@ -51,4 +51,4 @@ class ConfigContext:
return self
def __exit__(self, exc_type, exc_val, exc_tb):
config_var.reset(self._token) # type: ignore
config_var.reset(self._token) # type: ignore

View File

@@ -6,6 +6,11 @@
height: 3;
}
#analysis {
margin: 0;
padding: 0;
}
.repo-list-item {
layout: grid;
grid-size: 1;

View File

@@ -19,8 +19,9 @@ import sys
class AboutScreen(Screen):
BINDINGS = [
("q", "go_back", "返回"),
("z", "go_back", "关于"),
]
SUB_TITLE = "关于"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@@ -6,7 +6,7 @@ from pathlib import Path
from textual.app import ComposeResult
from textual.containers import ScrollableContainer, Horizontal, Vertical
from textual.screen import Screen
from textual.widgets import Button, Footer, Header, Label, ListItem, ListView, Static
from textual.widgets import Button, Footer, Header, Label, ListItem, ListView, Static, Markdown
from textual import events, on
from textual.reactive import reactive
@@ -74,10 +74,11 @@ class DashboardScreen(Screen):
),
id="header",
)
yield ListView(id="repo_list", classes="repo-list") # 单元集选择
from heurams.services.attic import Attic
a = Attic('ana', {'totaltime': 0, 'openpuzzles': 0, 'puzzles_err': 0})
yield Label(f"版本 {version.ver} {version.stage.capitalize()}") # 版本信息
yield Label(f"{round(a.data['totaltime'], 2)} 秒内处理了 {a.data['openpuzzles']} 个谜题, 正确率{'无法求解' if not a.data['openpuzzles'] else ' ' + str(round(100 * (1 - a.data['puzzles_err']/a.data['openpuzzles']), 2)) + '%'}, 平均速度{'无法求解' if not a.data['totaltime'] else ' ' + str(round(a.data['openpuzzles']/a.data['totaltime'], 2)) + '个/s'}", id='analysis') # 版本信息
yield Footer()
@on(events.ScreenResume)
@@ -201,6 +202,5 @@ class DashboardScreen(Screen):
logger.debug(f"event.button.id: {event.button.id}")
if event.button.id.startswith("slaunch_repo_"): # type: ignore
from .preparation import launch
launch(repo=self.repolink[event.button.id.lstrip("slaunch_repo_")], app=self.app, scheduled_num=-1) # type: ignore
launch(repo=self.repolink[event.button.id.removeprefix("slaunch_repo_")], app=self.app, scheduled_num=-1) # type: ignore
# TODO: 这样启动的记忆实例的状态机无法绑定到 PreparationScreen 中

View File

@@ -16,12 +16,10 @@ from heurams.kernel.reactor import *
from heurams.services.favorite_service import favorite_manager
from heurams.services.logger import get_logger
from .. import shim
logger = get_logger(__name__)
class MemScreen(Screen):
BINDINGS = [
("q", "go_back", "返回"),
@@ -29,6 +27,7 @@ class MemScreen(Screen):
("d", "toggle_dark", ""),
("v", "play_voice", "朗读"),
("*", "toggle_favorite", "收藏"),
("r", "resume_mark"),
("n", "block_prompt"),
("s", "block_prompt"),
("z", "block_prompt"),
@@ -60,10 +59,13 @@ class MemScreen(Screen):
@on(events.ScreenResume)
def post_active(self, event):
from heurams.interface import shim
shim.set_term_title(f"{self.app.TITLE} - {self.SUB_TITLE}")
def compose(self) -> ComposeResult:
import time
from heurams.services.attic import Attic
a = Attic('ana', {'openqueue': 0})
a.data['openqueue'] += 1
if config_var.get()['interface']['global']['show_header']:
yield Header(show_clock=config_var.get()['interface']['global']['clock_on_header'])
with ScrollableContainer():
@@ -78,6 +80,10 @@ class MemScreen(Screen):
def on_mount(self):
self.expander = self.procession.get_expander()
from heurams.services.attic import Attic
import time
a = Attic('ana', {'last': time.time()})
a.data['last'] = time.time()
self.mount_puzzle()
self.update_display()
@@ -107,6 +113,7 @@ class MemScreen(Screen):
def mount_puzzle(self):
"""挂载当前谜题组件"""
from heurams.services.attic import Attic
if self.procession.route == RouterState.FINISHED:
self.mount_finished_widget()
return
@@ -117,6 +124,8 @@ class MemScreen(Screen):
def mount_finished_widget(self):
"""挂载已完成组件"""
a = Attic('ana', {'finished': 0})
a.data['finished'] += 1
container = self.query_one("#puzzle_container")
for i in container.children:
i.remove()
@@ -168,6 +177,13 @@ class MemScreen(Screen):
if self.expander.state == "retronly":
self.forward_atom(self.expander.get_quality())
self.update_state()
from heurams.services.attic import Attic
a = Attic('ana', {'openpuzzles': 0})
a = Attic('ana', {'totaltime': 0})
a.data['openpuzzles'] += 1
import time
a.data['totaltime'] += time.time() - a.data['last']
a.data['last'] = time.time()
self.mount_puzzle()
self.update_display()
@@ -187,6 +203,8 @@ class MemScreen(Screen):
logger.debug(f"Quality: {quality}")
self.atom_reporter(quality)
if quality <= 3:
a = Attic('ana', {'puzzles_err': 0})
a.data['puzzles_err'] += 1
self.procession.append()
self.update_state() # 刷新状态
self.procession.forward(1)
@@ -241,3 +259,11 @@ class MemScreen(Screen):
def action_block_prompt(self):
self.app.notify("功能在记忆界面中不可用, 完成或返回后再试", severity="error")
def action_resume_mark(self):
from heurams.services.attic import Attic
import time
a = Attic('ana')
l = a.data['last']
a.data['last'] = time.time()
self.app.notify(f"时间恢复已修正: {l} -> {a.data['last']}")

View File

@@ -51,7 +51,9 @@ class PreparationScreen(Screen):
shim.set_term_title(f"{self.app.TITLE} - {self.SUB_TITLE}")
def compose(self) -> ComposeResult:
from heurams.services.attic import Attic
a = Attic('ana', {'openpre': 0})
a.data['openpre'] += 1
if config_var.get()['interface']['global']['show_header']:
yield Header(show_clock=config_var.get()['interface']['global']['clock_on_header'])
with ScrollableContainer(id="main_container"):

View File

@@ -32,6 +32,7 @@ class SettingScreen(Screen):
SUB_TITLE = "设置"
BINDINGS = [
("q", "go_back", "返回"),
("s", "go_back", "设置"),
]
CSS_PATH = rootdir / "interface" / "css" / "screens" / "setting.tcss"

View File

@@ -5,6 +5,7 @@ from typing import TypedDict
from textual.containers import ScrollableContainer
from textual.widget import Widget
from textual.widgets import Button, Label, Markdown
from textual.events import Key
import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
@@ -50,6 +51,7 @@ class ClozePuzzle(BasePuzzleWidget):
self.hashtable = {}
self.alia = alia
self._load()
self.btn_shortcuts = {}
self.hashmap = dict()
def _load(self):
@@ -67,15 +69,24 @@ class ClozePuzzle(BasePuzzleWidget):
yield Label(self.puzzle.wording, id="sentence")
yield Markdown(f"> {self.listprint(self.inputlist)}", id="inputpreview")
# 渲染当前问题的选项
with ScrollableContainer(id="btn-container"):
with ScrollableContainer(id="btn-container") as s:
c = 0
for i in self.ans:
h = str(hash(i))
if hash(i) in self.hashmap.keys():
continue
c += 1
self.hashmap[h] = i
btnid = f"sel000-{h}"
logger.debug(f"建立按钮 {btnid}")
yield Button(i, id=f"{btnid}")
self.btn_shortcuts[f'{c}'] = btnid
yield Button(f'[{c}] {i}', id=f"{btnid}")
s.focus()
yield Button("退格", id="delete")
self.btn_shortcuts[f'0'] = 'delete'
self.btn_shortcuts[f'backspace'] = 'delete'
self.btn_shortcuts[f'delete'] = 'delete'
def listprint(self, lst):
s = ""
@@ -117,3 +128,10 @@ class ClozePuzzle(BasePuzzleWidget):
pass
else:
self.atom.minimize(rating)
def on_key(self, event: Key) -> None:
self.notify(event.key)
if event.key in self.btn_shortcuts:
btn_id = self.btn_shortcuts.get(event.key)
btn_id = '#' + btn_id
self.query_one(btn_id, Button).press()

View File

@@ -9,7 +9,7 @@ import heurams.kernel.particles as pt
import heurams.kernel.puzzles as pz
from heurams.services.hasher import hash
from heurams.services.logger import get_logger
from textual.events import Key
from .base_puzzle_widget import BasePuzzleWidget
logger = get_logger(__name__)
@@ -51,6 +51,7 @@ class MCQPuzzle(BasePuzzleWidget):
self.hashmap = dict()
self.cursor = 0
self.atom = atom
self.btn_shortcuts = {}
self._load()
def _load(self):
@@ -75,14 +76,24 @@ class MCQPuzzle(BasePuzzleWidget):
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
# 渲染当前问题的选项
with ScrollableContainer(id="btn-container"):
c = 0
with ScrollableContainer(id="btn-container") as s:
for i in current_options:
self.hashmap[str(hash(i))] = i
btnid = f"sel{str(self.cursor).zfill(3)}-{hash(i)}"
if i in [' ', '']:
continue
c += 1
h = str(hash(i))
self.hashmap[h] = i
btnid = f"sel{str(self.cursor).zfill(3)}-{h}"
logger.debug(f"建立按钮 {btnid}")
yield Button(i, id=f"{btnid}")
self.btn_shortcuts[f'{c}'] = f'{btnid}'
yield Button(f'[{c}] ' + i, id=f"{btnid}")
s.focus()
yield Button("退格", id="delete")
yield Button("退格", id="delete")
self.btn_shortcuts['0'] = f'delete'
self.btn_shortcuts['delete'] = f'delete'
self.btn_shortcuts['backspace'] = f'delete'
def update_display(self, error=0):
# 更新预览标签
@@ -140,20 +151,25 @@ class MCQPuzzle(BasePuzzleWidget):
for child in container.children
if hasattr(child, "id") and child.id and child.id.startswith("sel")
]
container.focus()
for button in buttons_to_remove:
logger.info(button)
container.remove_children("#" + button.id) # type: ignore
# 添加当前题目的选项按钮
c = 0
current_question_index = len(self.inputlist)
if current_question_index < len(self.puzzle.options):
current_options = self.puzzle.options[current_question_index]
for option in current_options:
if option in ['', ' ']:
continue
c += 1
button_id = f"sel{str(self.cursor).zfill(3)}-{hash(option)}"
if button_id not in self.hashmap:
self.hashmap[button_id[7:]] = option
new_button = Button(option, id=button_id)
new_button = Button(f"[{c}] " + option, id=button_id)
self.btn_shortcuts[f"{c}"] = button_id
container.mount(new_button)
def handler(self, rating):
@@ -161,3 +177,10 @@ class MCQPuzzle(BasePuzzleWidget):
pass
else:
self.atom.minimize(rating)
def on_key(self, event: Key) -> None:
self.notify(event.key)
if event.key in self.btn_shortcuts:
btn_id = self.btn_shortcuts.get(event.key)
btn_id = '#' + btn_id
self.query_one(btn_id, Button).press()

View File

@@ -49,7 +49,7 @@ class Recognition(BasePuzzleWidget):
def compose(self):
from heurams.context import config_var
autovoice = config_var.get()["interface"]["widgets"]["autovoice"]
autovoice = config_var.get()["interface"]["widgets"]['recognition']["autovoice"]
if autovoice:
self.screen.action_play_voice() # type: ignore
cfg: RecognitionConfig = self.atom.registry["nucleon"]["puzzles"][self.alia]
@@ -96,8 +96,9 @@ class Recognition(BasePuzzleWidget):
if isinstance(item, str):
yield Markdown(item)
with Center():
yield Button("我已知晓", id="ok")
with Center() as c:
with Button("我已知晓", id="ok") as b:
b.focus()
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "ok":

View File

@@ -72,7 +72,7 @@ class MCQPuzzle(BasePuzzle):
# 确保至少有4个干扰项
while len(self.jammer) < 4:
self.jammer.append(" " * (4 - len(self.jammer)))
self.jammer.append("")
unique_jammers = set(jammer + list(self.mapping.values()))

View File

@@ -0,0 +1,54 @@
# Attic 服务
import pickle as pkl
from heurams.services.logger import get_logger
from heurams.context import config_var
from pathlib import Path
from heurams.services.hasher import get_md5
import atexit
from heurams.services import timer
from heurams.services.exceptions import WTFException
logger = get_logger(__name__)
def singleton(cls):
instances = {}
def get_instance(ident, default):
key = ident
if key not in instances:
instances[key] = cls(ident)
instances[key].patch_dict(default)
return instances[key]
return get_instance
atticdir = Path(config_var.get()['global']['paths']['misc']) / 'attics'
atticdir.mkdir(parents=True, exist_ok=True)
@singleton
class Attic:
def __init__(self, ident, default:dict={}):
self.ident = ident
self.ident = self.ident.replace('<DAYSTAMP>', str(timer.get_daystamp()))
self.ident = self.ident.replace('<TIMESTAMP>', str(timer.get_timestamp()))
if '<' in ident or '>' in ident:
raise WTFException
#self.ident = get_md5(self.ident)
self.pklpath = atticdir / f'{self.ident}.pkl'
atexit.register(self.save)
self.data = default
if self.pklpath.exists():
try:
self.load()
return
except:
self.pklpath.unlink(missing_ok=True)
self.pklpath.touch(exist_ok=True)
def patch_dict(self, dct):
self.data.update({k: v for k, v in dct.items() if k not in self.data})
def save(self):
with open(atticdir / f'{self.ident}.pkl', 'wb') as f:
pkl.dump(self.data, f)
def load(self):
with open(atticdir / f'{self.ident}.pkl', 'rb') as f:
self.data.update(dict(pkl.load(f)))

View File

@@ -62,8 +62,7 @@ class FavoriteManager:
def _get_file_path(self) -> Path:
"""获取收藏文件路径"""
config_path = Path(config_var.get()["global"]["paths"]["data"])
fav_path = config_path / "global" / "favorites.json"
fav_path = Path(config_var.get()["global"]["paths"]["misc"]) / "favorites.json5"
fav_path.parent.mkdir(parents=True, exist_ok=True)
return fav_path

View File

@@ -14,7 +14,8 @@ def get_md5(text):
def hash(text):
logger.debug(f"计算MD5-时间复合哈希, 输入`{text}`")
result = hashlib.md5(f"{text}{random.randint(0,1000)}".encode("utf-8")).hexdigest()
logger.debug("哈希结果: %s...", result[:8])
return result
#logger.debug(f"计算MD5-时间复合哈希, 输入`{text}`")
#result = hashlib.md5(f"{text}{random.randint(0,1000)}".encode("utf-8")).hexdigest()
#logger.debug("哈希结果: %s...", result[:8])
#return result
return get_md5(text)