Compare commits
22 Commits
709e15663b
..
0.4.3
| Author | SHA1 | Date | |
|---|---|---|---|
| b3bc5b1a09 | |||
| e562e02227 | |||
| c10a18bafd | |||
| 6a5120920a | |||
| 56038afefe | |||
| d4dce68449 | |||
| 8dd323a789 | |||
| 531b0756bf | |||
| 6adf1c62f6 | |||
| 058cc1e4d8 | |||
| 7e28e0cd6b | |||
| a3612bd3b3 | |||
| 77441e2d40 | |||
| 72697f652f | |||
| c5fd625c87 | |||
| abd94d9a21 | |||
| 91d3c86871 | |||
| 66ad50c44d | |||
| 2527daa923 | |||
| 1883ca2387 | |||
| dde6f872f0 | |||
| 16c22cf207 |
+8
-4
@@ -5,18 +5,22 @@
|
||||
__pycache__/
|
||||
.idea/
|
||||
cache/
|
||||
data/repo/cngk
|
||||
data/repo/eotgk
|
||||
data/repo/evtgk
|
||||
#nucleon/test.toml
|
||||
electron/test.toml
|
||||
*.egg-info/
|
||||
build/
|
||||
dist/
|
||||
old/
|
||||
# config/
|
||||
data/cache/
|
||||
data/electron/
|
||||
data/nucleon/
|
||||
data/global/
|
||||
!data/nucleon/TEST*
|
||||
data/orbital/
|
||||
config/config_dev.toml
|
||||
AGENTS.md
|
||||
*.log.*
|
||||
*.log.1
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
||||
+7
-8
@@ -1,25 +1,24 @@
|
||||
# 贡献指南
|
||||
|
||||
欢迎为此项目做出贡献!\
|
||||
欢迎为此项目做出贡献!
|
||||
本项目是一个开源项目, 我们鼓励社区成员参与改进.
|
||||
|
||||
## 开发规范
|
||||
## 开发流程
|
||||
|
||||
1. 分支划分:
|
||||
1. **分支划分**:
|
||||
- `main` 分支: 稳定版本
|
||||
- `dev` 分支: 开发版本
|
||||
- 功能分支: 从 `dev` 分支创建, 命名格式为 `feature/描述` 或 `fix/描述` 或 `refactor/描述`
|
||||
1. 代码风格:
|
||||
- 请使用 Black 格式化代码
|
||||
2. **代码风格**:
|
||||
- 请使用 Black 与 isort 格式化代码
|
||||
- 遵循 PEP 8 规范
|
||||
- 添加适当的文档字符串
|
||||
1. 提交消息:
|
||||
3. **提交消息**:
|
||||
- 使用简体中文或英文撰写清晰的提交消息
|
||||
- 格式: 遵循 Conventional Commits 规范
|
||||
1. 合并方式:
|
||||
4. **合并方式**:
|
||||
- 不使用 Fast-forward 合并
|
||||
- 可以设置 `git config merge.ff false`
|
||||
|
||||
## 设置开发环境
|
||||
|
||||
```bash
|
||||
|
||||
@@ -1,78 +1,109 @@
|
||||
# 潜进 (HeurAMS) - 启发式辅助记忆程序
|
||||
|
||||
## 概述
|
||||
|
||||
"潜进" (HeurAMS: Heuristic Auxiliary Memorizing Scheduler, 启发式记忆辅助调度器) 是为习题册, 古诗词, 及其他问答/记忆/理解型知识设计的开放源代码多用途辅助记忆软件, 提供动态规划的优化记忆方案
|
||||
"潜进" (HeurAMS: Heuristic Auxiliary Memorizing Scheduler, 启发式记忆辅助调度器) 是为习题册, 古诗词, 及其他问答/记忆/理解型知识设计的多用途辅助记忆软件, 提供动态规划的优化记忆方案
|
||||
|
||||
## 关于此仓库
|
||||
"潜进" 软件组项目包含多个子项目
|
||||
此仓库包含了 "潜进" 项目的核心和基于 Textual 的基本用户界面的实现
|
||||
|
||||
本仓库为 "潜进" 软件组项目的核心部分, 包含核心功能模块以及基于 Textual 框架的基础用户界面(heurams.interface)实现\
|
||||
除了通过用户界面进行学习外, 你也可以在 Python 中导入 `heurams` 库, 使用其中实现的状态机, 算法迭代器和数据模型构建辅助记忆功能
|
||||
## 开发进程
|
||||
- 0.0.x: 简易调度器实现与最小原型.
|
||||
- 0.1.x: 命令行操作的调度器.
|
||||
- 0.2.x: 使用 Textual 构建富文本终端用户界面, 项目可行性验证, 采用 SM-2 原始算法, 评估方式为用户自评估的原型.
|
||||
- 0.3.x: 简单的多文件项目, 创建了记忆内容/算法数据结构, 基于 SM-2 改进算法的自动复习测评评估. 重点设计古诗文记忆理解功能, 以及 TUI 界面实现, 简单的 TTS 集成.
|
||||
- 0.4.x: 使用模块管理解耦设计, 增加文档与类型标注, 采用上下文设计模式的隐式依赖注入与遵从 IoC, 注册器设计的算法与功能实现, 支持其他调度算法模块 (SM-2, FSRS) 与谜题模块, 采用日志调试, 更新文件格式, 引入动态数据模式(宏驱动的动态内容生成), 与基于文件的策略调控, 更佳的用户数据处理, 加入模块化扩展集成, 将算法数据格式换为 json 提高性能, 采用 provider-service 抽象架构, 支持切换服务提供者, 整体兼容性改进.
|
||||
> 下一步?
|
||||
> 使用 Flutter 构建酷酷的现代化前端, 增加云同步/文档源服务...
|
||||
|
||||
## 特性
|
||||
|
||||
### 间隔迭代算法
|
||||
|
||||
> 许多出版物都广泛讨论了不同重复间隔对学习效果的影响. 特别是, 间隔效应被认为是一种普遍现象. 间隔效应是指, 如果重复的间隔是分散/稀疏的, 而不是集中重复, 那么学习任务的表现会更好. 因此, 有观点提出, 学习中使用的最佳重复间隔是**最长的, 但不会导致遗忘的间隔**.
|
||||
|
||||
> 许多出版物都广泛讨论了不同重复间隔对学习效果的影响. 特别是, 间隔效应被认为是一种普遍现象. 间隔效应是指, 如果重复的间隔是分散/稀疏的, 而不是集中重复, 那么学习任务的表现会更好. 因此, 有观点提出, 学习中使用的最佳重复间隔是**最长的、但不会导致遗忘的间隔**.
|
||||
- 采用经实证的 SM-2 间隔迭代算法, 此算法亦用作 Anki 闪卡记忆软件的默认闪卡调度器
|
||||
- 动态规划每个记忆单元的记忆间隔时间表
|
||||
- 动态跟踪记忆反馈数据, 优化长期记忆保留率与稳定性
|
||||
|
||||
### 学习进程优化
|
||||
|
||||
- 元数据配置: 支持配置详细附加数据
|
||||
- 自然语音: 集成文本转语音 (TTS) 功能, 支持"电台"回顾功能
|
||||
- 多种谜题类型: 选择题 (MCQ), 填空题 (Cloze), 识别题 (Recognition)
|
||||
- 逐字解析: 支持逐字详细释义解析
|
||||
- 语法分析: 接入生成式人工智能, 支持古文结构交互式解析
|
||||
- 自然语音: 集成微软神经网络文本转语音 (TTS) 技术
|
||||
- 多种谜题类型: 选择题 (MCQ)、填空题 (Cloze)、识别题 (Recognition)
|
||||
- 动态内容生成: 支持宏驱动的模板系统, 根据上下文动态生成题目
|
||||
- 云同步支持: 通过多种协议同步数据到远程服务器
|
||||
- 云同步支持: 通过 WebDAV 协议同步数据到远程服务器
|
||||
|
||||
### 实用用户界面
|
||||
|
||||
- 响应式 Textual 框架构建的跨平台 TUI 界面
|
||||
- 支持触屏/鼠标/键盘多操作模式
|
||||
- 简洁直观的复习流程设计
|
||||
|
||||
### 架构特性
|
||||
- 模块化设计: 算法、谜题、服务提供者可插拔替换
|
||||
- 上下文管理: 使用 ContextVar 实现隐式依赖注入
|
||||
- 数据持久化: TOML 配置与内容, JSON 算法状态
|
||||
- 服务抽象: 音频播放、TTS、LLM 通过 provider 架构支持多种后端
|
||||
- 完整日志系统: 带轮转的日志记录, 便于调试
|
||||
|
||||
## 安装
|
||||
|
||||
### 从源码安装
|
||||
|
||||
1. 克隆仓库:
|
||||
|
||||
```bash
|
||||
git clone https://gitea.imwangzhiyu.xyz/ajax/HeurAMS
|
||||
cd HeurAMS
|
||||
```
|
||||
|
||||
1. 安装依赖:
|
||||
|
||||
2. 安装依赖:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
1. 以开发模式安装包:
|
||||
|
||||
3. 以开发模式安装包:
|
||||
```bash
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
### 从包管理器安装
|
||||
|
||||
暂时还没有:(
|
||||
|
||||
## 启动应用
|
||||
## 使用
|
||||
|
||||
### 启动应用
|
||||
```bash
|
||||
# 在任一目录(建议是空目录或者包根目录, 将被用作存放数据)下运行
|
||||
python -m heurams.interface
|
||||
```
|
||||
|
||||
配置文件位于 `./data/config/config.toml`(相对于工作目录).\
|
||||
如果不存在, 会使用内置的默认配置.
|
||||
### 数据目录结构
|
||||
应用会在工作目录下创建以下数据目录:
|
||||
- `data/nucleon/`: 记忆内容 (TOML 格式)
|
||||
- `data/electron/`: 算法状态 (JSON 格式)
|
||||
- `data/orbital/`: 策略配置 (TOML 格式)
|
||||
- `data/cache/`: 音频缓存文件
|
||||
- `data/template/`: 内容模板
|
||||
|
||||
首次运行时会自动创建这些目录.
|
||||
|
||||
## 配置
|
||||
|
||||
配置文件位于 `config/config.toml`(相对于工作目录). 如果不存在, 会使用内置的默认配置.
|
||||
|
||||
### 同步配置
|
||||
同步功能支持 WebDAV 协议,可在配置文件的 `[sync.webdav]` 段进行配置:
|
||||
```toml
|
||||
[sync.webdav]
|
||||
enabled = false
|
||||
url = "" # WebDAV 服务器地址
|
||||
username = "" # 用户名
|
||||
password = "" # 密码
|
||||
remote_path = "/heurams/" # 远程路径
|
||||
sync_mode = "bidirectional" # 同步模式: bidirectional/upload_only/download_only
|
||||
conflict_strategy = "newer" # 冲突策略: newer/ask/keep_both
|
||||
verify_ssl = true # SSL 证书验证
|
||||
```
|
||||
|
||||
启用同步后,可通过应用内的同步工具进行数据备份和恢复。
|
||||
|
||||
## 项目结构
|
||||
|
||||
### 架构图(待更新 0.5.0)
|
||||
### 架构图
|
||||
|
||||
以下 Mermaid 图展示了 HeurAMS 的主要组件及其关系:
|
||||
|
||||
@@ -133,26 +164,38 @@ graph TB
|
||||
Algorithms --> Files
|
||||
```
|
||||
|
||||
### 目录结构
|
||||
```
|
||||
src/heurams/
|
||||
├── __init__.py # 包入口点
|
||||
├── context.py # 全局上下文、路径、配置上下文管理器
|
||||
├── services/ # 核心服务
|
||||
│ ├── config.py # 配置管理
|
||||
│ ├── logger.py # 日志系统
|
||||
│ ├── timer.py # 时间服务
|
||||
│ ├── audio_service.py # 音频播放抽象
|
||||
│ ├── tts_service.py # 文本转语音抽象
|
||||
│ └── sync_service.py # WebDAV 同步服务
|
||||
├── kernel/ # 核心业务逻辑
|
||||
│ ├── algorithms/ # 间隔重复算法 (FSRS, SM2)
|
||||
│ ├── particles/ # 数据模型 (Atom, Electron, Nucleon, Orbital)
|
||||
│ ├── puzzles/ # 谜题类型 (MCQ, cloze, recognition)
|
||||
│ └── reactor/ # 调度和处理逻辑
|
||||
├── providers/ # 外部服务提供者
|
||||
│ ├── audio/ # 音频播放实现
|
||||
│ ├── tts/ # 文本转语音实现
|
||||
│ └── llm/ # LLM 集成
|
||||
├── interface/ # Textual TUI 界面
|
||||
│ ├── widgets/ # UI 组件
|
||||
│ ├── screens/ # 应用屏幕
|
||||
│ └── __main__.py # 应用入口点
|
||||
└── default/ # 默认配置和数据模板
|
||||
```
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎贡献! 请参阅 [CONTRIBUTING.md](CONTRIBUTING.md) 了解贡献指南.
|
||||
欢迎贡献!请参阅 [CONTRIBUTING.md](CONTRIBUTING.md) 了解贡献指南.
|
||||
|
||||
## 许可证
|
||||
|
||||
### 项目本身
|
||||
|
||||
本项目基于 AGPL-3.0 许可证开源. 详见根目录下 [LICENSE](LICENSE) 文件.
|
||||
|
||||
### 第三方代码
|
||||
|
||||
项目在 `src/heurams/vendor/` 目录下嵌入了以下第三方代码(可能有修改):
|
||||
|
||||
#### py-fsrs
|
||||
|
||||
- 上游版本: 6.3.1
|
||||
- 位置: `src/heurams/vendor/pyfsrs/`
|
||||
- 原项目: [py-fsrs](https://github.com/open-spaced-repetition/py-fsrs)
|
||||
- 原许可证: Copyright (c) 2026 Open Spaced Repetition Contributors
|
||||
- 原许可证: MIT License, 详见 `src/heurams/vendor/pyfsrs/LICENSE`
|
||||
|
||||
本项目受益于他们无私且优秀的工作
|
||||
本项目基于 AGPL-3.0 许可证开源. 详见 [LICENSE](LICENSE) 文件.
|
||||
@@ -0,0 +1,61 @@
|
||||
# [调试] 将更改保存到文件
|
||||
persist_to_file = 1
|
||||
|
||||
# [调试] 覆写时间, 设为 -1 以禁用
|
||||
daystamp_override = -1
|
||||
timestamp_override = -1
|
||||
|
||||
# [调试] 一键通过
|
||||
quick_pass = 1
|
||||
|
||||
# 对于每个项目的默认新记忆原子数量
|
||||
scheduled_num = 8
|
||||
|
||||
# UTC 时间戳修正 仅用于 UNIX 日时间戳的生成修正, 单位为秒
|
||||
timezone_offset = +28800 # 中国标准时间 (UTC+8)
|
||||
|
||||
[interface]
|
||||
|
||||
[interface.memorizor]
|
||||
autovoice = true # 自动语音播放, 仅限于 recognition 组件
|
||||
|
||||
[algorithm]
|
||||
default = "SM-2" # 主要算法; 可选项: SM-2, SM-15M, FSRS
|
||||
|
||||
[puzzles] # 谜题默认配置
|
||||
|
||||
[puzzles.mcq]
|
||||
max_riddles_num = 2
|
||||
|
||||
[puzzles.cloze]
|
||||
min_denominator = 3
|
||||
|
||||
[paths] # 相对于配置文件的 ".." (即工作目录) 而言 或绝对路径
|
||||
nucleon_dir = "./data/nucleon"
|
||||
electron_dir = "./data/electron"
|
||||
global_dir = "./data/global" # 全局数据路径, SM-15 等算法需要
|
||||
orbital_dir = "./data/orbital"
|
||||
cache_dir = "./data/cache"
|
||||
template_dir = "./data/template"
|
||||
|
||||
[services] # 定义服务到提供者的映射
|
||||
audio = "playsound" # 可选项: playsound(通用), termux(仅用于支持 Android Termux), mpg123(TODO)
|
||||
tts = "edgetts" # 可选项: edgetts
|
||||
llm = "openai" # 可选项: openai
|
||||
sync = "webdav" # 可选项: 留空, webdav
|
||||
|
||||
[providers.tts.edgetts] # EdgeTTS 设置
|
||||
voice = "zh-CN-XiaoxiaoNeural" # 可选项: zh-CN-YunjianNeural (男声), zh-CN-XiaoxiaoNeural (女声)
|
||||
|
||||
[providers.llm.openai] # 与 OpenAI 相容的语言模型接口服务设置
|
||||
url = ""
|
||||
key = ""
|
||||
|
||||
[providers.sync.webdav] # WebDAV 同步设置
|
||||
url = ""
|
||||
username = ""
|
||||
password = ""
|
||||
remote_path = "/heurams/"
|
||||
verify_ssl = true
|
||||
|
||||
[sync]
|
||||
@@ -1,6 +0,0 @@
|
||||
_services_desc = '服务模块(services)设置'
|
||||
_providers_desc = '驱动模块(providers)设置'
|
||||
_repo_desc = '单元集独立设置'
|
||||
_interface_desc = '基本用户界面设置'
|
||||
_default_desc = '默认设置'
|
||||
_global_desc = '底层设置'
|
||||
@@ -1,17 +0,0 @@
|
||||
zmq_debug = false
|
||||
_zmq_debug_desc = "[调试] ZMQ 调试服务器, 这会在 zmq_debug_port 上打开调试服务器\n可作为 HeurAMS 执行任意 python 代码, 如无必要请关闭"
|
||||
zmq_debug_port = 5555
|
||||
_zmq_debug_port_desc = "ZMQ 调试服务器端口"
|
||||
enable_built_in_interface = false
|
||||
_enable_built_in_interface_desc = "启用内置基本用户界面\n(当且仅当 HeurAMS 作为程序库时禁用, 以跳过用户界面逻辑)"
|
||||
_paths_desc = "用户数据路径定义"
|
||||
|
||||
[paths]
|
||||
data = "./data"
|
||||
_data_desc = "用户数据根目录"
|
||||
cache = "./data/cache"
|
||||
_cache_desc = "缓存根目录\n(如音频缓存在 voice 子目录)"
|
||||
config = "./data/config"
|
||||
_config_desc = "配置文件根目录"
|
||||
repo = "./data/repo"
|
||||
_repo_desc = "记忆单元集根目录"
|
||||
@@ -1,4 +0,0 @@
|
||||
_global_desc = "用户界面通用设置"
|
||||
_widgets_desc = "各组件设置"
|
||||
_screens_desc = "各界面设置"
|
||||
_puzzles_desc = "谜题生成器设置"
|
||||
@@ -1,17 +0,0 @@
|
||||
persist_to_file = true
|
||||
_persist_to_file_desc = "[调试] 将记忆更改保存到文件"
|
||||
quick_pass = true
|
||||
_quick_pass_desc = "[调试] 启用快速应答功能(跳过测验)"
|
||||
auto_pass = false
|
||||
_auto_pass_desc = "[调试] 自动通过测试模式"
|
||||
scheduled_num = "420"
|
||||
_scheduled_num_desc = "默认记忆单元数量(可被单元集设置覆盖)"
|
||||
algorithm = "NSP-0"
|
||||
_algorithm_desc = "默认记忆调度算法(可被单元集设置覆盖)"
|
||||
|
||||
[_algorithm_candidate]
|
||||
NSP-0 = "筛选用非间隔重复调度器"
|
||||
none = "不设置默认调度器"
|
||||
SM-2 = "第二代 SuperMemo 简单间隔重复调度器"
|
||||
SM-15M = "第15代 SuperMemo 复杂间隔重复调度器 (不稳定且逆向工程)"
|
||||
FSRS = "先进开放间隔重复调度器"
|
||||
@@ -1,2 +0,0 @@
|
||||
_cloze_desc = "填空题"
|
||||
_mcq_desc = "选择题"
|
||||
@@ -1,2 +0,0 @@
|
||||
min_denominator = "3"
|
||||
_min_denominator_desc = "设空比例系数的倒数"
|
||||
@@ -1,2 +0,0 @@
|
||||
max_riddles_num = "2"
|
||||
_max_riddles_num_desc = "单次生成的最大谜题数量"
|
||||
@@ -1 +0,0 @@
|
||||
_recognition_desc = "用于 '辨识' 组件的设置"
|
||||
@@ -1,2 +0,0 @@
|
||||
autovoice = true
|
||||
_autovoice_desc = "自动语音播放"
|
||||
@@ -1 +0,0 @@
|
||||
_tts_desc = '文本转语音驱动'
|
||||
@@ -1 +0,0 @@
|
||||
_edgetts_desc = "微软文本转语音驱动"
|
||||
@@ -1,25 +0,0 @@
|
||||
voice = "zh-CN-XiaoxiaoNeural"
|
||||
_voice_desc = "音色"
|
||||
|
||||
[_voice_candidate]
|
||||
zh-CN-XiaoxiaoNeural = "晓晓: 中文温柔女声"
|
||||
zh-CN-XiaoyiNeural = "晓伊: 中文甜美女声"
|
||||
zh-CN-XiaochenNeural = "晓辰: 中文知性女声"
|
||||
zh-CN-XiaohanNeural = "晓涵: 中文优雅女声"
|
||||
zh-CN-XiaomengNeural = "晓梦: 中文梦幻女声"
|
||||
zh-CN-XiaomoNeural = "晓墨: 中文文艺女声"
|
||||
zh-CN-XiaoqiuNeural = "晓秋: 中文成熟女声"
|
||||
zh-CN-XiaoruiNeural = "晓睿: 中文智慧女声"
|
||||
zh-CN-XiaoshuangNeural = "晓双: 中文活泼女声"
|
||||
zh-CN-XiaoxuanNeural = "晓萱: 中文清新女声"
|
||||
zh-CN-XiaoyanNeural = "晓颜: 中文柔美女声"
|
||||
zh-CN-XiaoyouNeural = "晓悠: 中文悠扬女声"
|
||||
zh-CN-XiaozhenNeural = "晓甄: 中文端庄女声"
|
||||
zh-CN-YunxiNeural = "云希: 中文清朗男声"
|
||||
zh-CN-YunyangNeural = "云扬: 中文阳光男声"
|
||||
zh-CN-YunjianNeural = "云健: 中文稳重男声"
|
||||
zh-CN-YunfengNeural = "云枫: 中文磁性男声"
|
||||
zh-CN-YunhaoNeural = "云皓: 中文豪迈男声"
|
||||
zh-CN-YunxiaNeural = "云夏: 中文热情男声"
|
||||
zh-CN-YunyeNeural = "云野: 中文野性男声"
|
||||
zh-CN-YunzeNeural = "云泽: 中文深沉男声"
|
||||
@@ -1,5 +0,0 @@
|
||||
_audio_desc = '音频服务'
|
||||
_llm_desc = '语言模型服务'
|
||||
_sync_desc = '数据同步服务'
|
||||
_timer_desc = '时间服务'
|
||||
_tts_desc = '文本转语音服务'
|
||||
@@ -1,9 +0,0 @@
|
||||
provider = "playsound"
|
||||
_provider_desc = "音频驱动类型"
|
||||
|
||||
[_provider_candidate]
|
||||
playsound = "python 跨平台音频系统"
|
||||
termux = "Android Termux 音频系统"
|
||||
mpg123 = "通用音频系统, 依赖系统 mpg123"
|
||||
pulseaudio = "高级音频路由系统"
|
||||
none = "不使用音频"
|
||||
@@ -1,6 +0,0 @@
|
||||
provider = "openai"
|
||||
_provider_desc = "模型接口类型"
|
||||
|
||||
[_provider_candidate]
|
||||
openai = "OpenAI 风格 API, 同时支持与其相容的模型服务 (如 deepseek)"
|
||||
none = "不使用语言大模型"
|
||||
@@ -1,7 +0,0 @@
|
||||
provider = "webdav"
|
||||
_provider_desc = "同步服务驱动类型"
|
||||
|
||||
[_provider_candidate]
|
||||
webdav = "WebDAV 兼容网络文件系统 (包括 webdavs)"
|
||||
official = "官方同步服务器"
|
||||
none = "不使用同步服务器"
|
||||
@@ -1,6 +0,0 @@
|
||||
daystamp_override = -1
|
||||
_daystamp_override_desc = "[调试] 覆写 UNIX 日时间戳, 单位为日\n(设为 -1 禁用)"
|
||||
timestamp_override = -1
|
||||
_timestamp_override_desc = "[调试] 覆写 UNIX 时间戳, 单位为秒\n(设为 -1 禁用)"
|
||||
timezone_offset = 28800
|
||||
_timezone_offset_desc = "时区偏移设置, 用于取消跨天时区误差, 单位为秒\n(如 28800 为 UTC+8.0, 中国标准时间)"
|
||||
@@ -1,7 +0,0 @@
|
||||
provider = "edgetts"
|
||||
_provider_desc = "文本转语音驱动类型"
|
||||
|
||||
[_provider_candidate]
|
||||
edgetts = "微软神经网络语音合成, 依赖微软网络服务"
|
||||
espeak = "低保真度本地语音合成"
|
||||
none = "不使用文本转语音"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,3 +0,0 @@
|
||||
title = "高考必背古诗文-0"
|
||||
author = "__heurams__"
|
||||
desc = "高考古诗文 60 篇"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,11 +0,0 @@
|
||||
schedule = ["quick_review", "recognition", "final_review"]
|
||||
|
||||
[phases]
|
||||
quick_review = [["FillBlank", "1.0"], ["Recognition", "1.0"]]
|
||||
recognition = [["FillBlank", "1.0"]]
|
||||
final_review = [["FillBlank", "1.0"], ["Recognition", "1.0"]]
|
||||
|
||||
[annotation]
|
||||
"quick_review" = "复习旧知"
|
||||
"recognition" = "新知识"
|
||||
"final_review" = "总复习"
|
||||
@@ -1,17 +0,0 @@
|
||||
[annotation]
|
||||
note = "笔记"
|
||||
keyword_note = "关键词翻译"
|
||||
translation = "语句翻译"
|
||||
delimiter = "分隔符"
|
||||
content = "内容"
|
||||
tts_text = "文本转语音文本"
|
||||
|
||||
[common]
|
||||
delimiter = "/"
|
||||
tts_text = "eval:payload['content'].replace('/', '')"
|
||||
|
||||
[common.puzzles] # 谜题定义
|
||||
# 我们称 "Recognition" 为 recognition 谜题的 alia
|
||||
"Recognition" = { __origin__ = "recognition", __hint__ = "", primary = "eval:payload['content']", secondary = ["eval:payload['keyword_note']", "eval:payload['note']"], top_dim = ["eval:payload['translation']"] }
|
||||
"SelectMeaning" = { __origin__ = "mcq", __hint__ = "eval:payload['content']", primary = "eval:payload['content']", mapping = "eval:payload['keyword_note']", jammer = "eval:list(payload['keyword_note'].values())", max_riddles_num = "eval:default['mcq']['max_riddles_num']", prefix = "选择正确项: " }
|
||||
"FillBlank" = { __origin__ = "cloze", __hint__ = "", text = "eval:payload['content']", delimiter = "eval:nucleon['delimiter']", min_denominator = "eval:default['cloze']['min_denominator']"}
|
||||
@@ -0,0 +1,23 @@
|
||||
# Nucleon 是 HeurAMS 软件项目使用的基于 TOML 的专有源文件格式, 版本 5
|
||||
# 建议使用的 MIME 类型: application/vnd.xyz.imwangzhiyu.heurams-nucleon.v5+toml
|
||||
|
||||
[__metadata__]
|
||||
[__metadata__.attribution] # 元信息
|
||||
desc = "带有宏支持的空白模板"
|
||||
|
||||
[__metadata__.annotation] # 键批注
|
||||
|
||||
[__metadata__.formation] # 文件配置
|
||||
#delimiter = "/"
|
||||
#tts_text = "eval:nucleon['content'].replace('/', '')"
|
||||
|
||||
[__metadata__.orbital.puzzles] # 谜题定义
|
||||
# 我们称 "Recognition" 为 recognition 谜题的 alia
|
||||
#"Recognition" = { __origin__ = "recognition", __hint__ = "", primary = "eval:nucleon['content']", secondary = ["eval:nucleon['keyword_note']", "eval:nucleon['note']"], top_dim = ["eval:nucleon['translation']"] }
|
||||
#"SelectMeaning" = { __origin__ = "mcq", __hint__ = "eval:nucleon['content']", mapping = "eval:nucleon['keyword_note']", jammer = "eval:nucleon['keyword_note']", max_riddles_num = "eval:default['mcq']['max_riddles_num']", prefix = "选择正确项: " }
|
||||
#"FillBlank" = { __origin__ = "cloze", __hint__ = "", text = "eval:nucleon['content']", delimiter = "eval:metadata['formation']['delimiter']", min_denominator = "eval:default['cloze']['min_denominator']"}
|
||||
|
||||
[__metadata__.orbital.schedule] # 内置的推荐学习方案
|
||||
#quick_review = [["FillBlank", "1.0"], ["SelectMeaning", "0.5"], ["recognition", "1.0"]]
|
||||
#recognition = [["Recognition", "1.0"]]
|
||||
#final_review = [["FillBlank", "0.7"], ["SelectMeaning", "0.7"], ["recognition", "1.0"]]
|
||||
@@ -1,14 +0,0 @@
|
||||
# encoding=utf-8
|
||||
import jieba
|
||||
|
||||
# jieba.enable_paddle()# 启动paddle模式。 0.40版之后开始支持,早期版本不支持
|
||||
strs = ["我来到北京清华大学", "乒乓球拍卖完了", "中国科学技术大学"]
|
||||
# for str in strs:
|
||||
# seg_list = jieba.cut(str,use_paddle=True) # 使用paddle模式
|
||||
# print("Paddle Mode: " + '/'.join(list(seg_list)))
|
||||
|
||||
seg_list = jieba.cut("秦孝公据崤函之固, 拥雍州之地", cut_all=False)
|
||||
print("Default Mode: " + "/ ".join(seg_list)) # 精确模式
|
||||
|
||||
seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
|
||||
print(", ".join(seg_list))
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,57 +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.create_from_repodir(Path("./test_repo"))
|
||||
alist = list()
|
||||
print(repo.ident_index)
|
||||
for i in repo.ident_index:
|
||||
n = pt.Nucleon.create_on_nucleonic_data(
|
||||
nucleonic_data=repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
e = pt.Electron.create_on_electonic_data(
|
||||
electronic_data=repo.electronic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
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)
|
||||
@@ -1 +0,0 @@
|
||||
{}
|
||||
@@ -1,3 +0,0 @@
|
||||
title = "测试单元: 过秦论"
|
||||
author = "__heurams__"
|
||||
desc = "高考古诗文: 过秦论"
|
||||
@@ -1,11 +0,0 @@
|
||||
["秦孝公据崤函之固, 拥雍州之地,"]
|
||||
note = []
|
||||
content = "秦孝公/据/崤函/之固/, 拥/雍州/之地,/"
|
||||
translation = "秦孝公占据着崤山和函谷关的险固地势,拥有雍州的土地,"
|
||||
keyword_note = {"据"="占据", "崤函"="崤山和函谷关", "雍州"="古代九州之一"}
|
||||
|
||||
["君臣固守以窥周室,"]
|
||||
note = []
|
||||
content = "君臣/固守/以窥/周室,/"
|
||||
translation = "君臣牢固地守卫着,借以窥视周王室的权力,"
|
||||
keyword_note = {"窥"="窥视"}
|
||||
@@ -1,5 +0,0 @@
|
||||
schedule = ["quick_review", "recognition", "final_review"]
|
||||
[phases]
|
||||
quick_review = [["FillBlank", "1.0"], ["SelectMeaning", "0.5"], ["Recognition", "1.0"]]
|
||||
recognition = [["Recognition", "1.0"]]
|
||||
final_review = [["FillBlank", "0.7"], ["SelectMeaning", "0.7"], ["Recognition", "1.0"]]
|
||||
@@ -1,17 +0,0 @@
|
||||
[annotation]
|
||||
note = "笔记"
|
||||
keyword_note = "关键词翻译"
|
||||
translation = "语句翻译"
|
||||
delimiter = "分隔符"
|
||||
content = "内容"
|
||||
tts_text = "文本转语音文本"
|
||||
|
||||
[common]
|
||||
delimiter = "/"
|
||||
tts_text = "eval:payload['content'].replace('/', '')"
|
||||
|
||||
[common.puzzles] # 谜题定义
|
||||
# 我们称 "Recognition" 为 recognition 谜题的 alia
|
||||
"Recognition" = { __origin__ = "recognition", __hint__ = "", primary = "eval:payload['content']", secondary = ["eval:payload['keyword_note']", "eval:payload['note']"], top_dim = ["eval:payload['translation']"] }
|
||||
"SelectMeaning" = { __origin__ = "mcq", __hint__ = "eval:payload['content']", primary = "eval:payload['content']", mapping = "eval:payload['keyword_note']", jammer = "eval:list(payload['keyword_note'].values())", max_riddles_num = "eval:default['mcq']['max_riddles_num']", prefix = "选择正确项: " }
|
||||
"FillBlank" = { __origin__ = "cloze", __hint__ = "", text = "eval:payload['content']", delimiter = "eval:nucleon['delimiter']", min_denominator = "eval:default['cloze']['min_denominator']"}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
# 运行时对象
|
||||
|
||||
Atom: 原子, 由核子, 电子, 轨道对象一并构成, 用于处理记忆所需一系列对象
|
||||
Nucleon: 核子, 负责解析文件动态内容, 并储存记忆材料内容与谜题定义, 是静态只读但可临时覆盖内容的\
|
||||
Electron: 电子, 负责处理记忆算法数据\
|
||||
Orbital: 轨道, 储存记忆阶段信息与谜题阶段内出现配置
|
||||
|
||||
# 状态机对象
|
||||
|
||||
Transitions: 一种状态机框架库
|
||||
Reactor: 状态机库
|
||||
Phaser...
|
||||
|
||||
rating: 用户评估生成的值
|
||||
quality: 用于单元反馈的值
|
||||
+23
-23
@@ -1,28 +1,28 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=45", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "heurams"
|
||||
version = "0.5.0"
|
||||
description = "Heuristic Auxiliary Memory Scheduler"
|
||||
readme = "README.md"
|
||||
authors = [{ name = "pluvium27", email = "pluvium27@outlook.com" }]
|
||||
requires-python = "==3.14.*"
|
||||
dependencies = [
|
||||
"edge-tts>=7.2.8",
|
||||
"jieba>=0.42.1",
|
||||
"openai>=2.32.0",
|
||||
"playsound==1.2.2",
|
||||
"psutil>=7.2.2",
|
||||
"pygobject>=3.56.2",
|
||||
"tabulate>=0.10.0",
|
||||
"textual>=8.2.3",
|
||||
"toml>=0.10.2",
|
||||
"transitions>=0.9.3",
|
||||
"zmq>=0.0.0",
|
||||
version = "0.4.3"
|
||||
description = "Heuristic Assisted Memory Scheduler"
|
||||
license = {file = "LICENSE"}
|
||||
classifiers = [
|
||||
"License :: OSI Approved :: GNU Affero General Public License v3",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Operating System :: OS Independent",
|
||||
"Topic :: Education",
|
||||
"Intended Audience :: Education",
|
||||
]
|
||||
keywords = ["spaced-repetition", "memory", "learning", "tui", "textual", "flashcards", "education"]
|
||||
dependencies = [
|
||||
"bidict==0.23.1",
|
||||
"playsound==1.2.2",
|
||||
"textual==5.3.0",
|
||||
"toml==0.10.2",
|
||||
]
|
||||
readme = "README.md"
|
||||
|
||||
[project.scripts]
|
||||
heurams = "heurams.__main__:main"
|
||||
tui = "heurams.interface.__main__:main"
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["src"]
|
||||
|
||||
[build-system]
|
||||
requires = ["uv_build>=0.9.22,<0.10.0"]
|
||||
build-backend = "uv_build"
|
||||
|
||||
+4
-6
@@ -1,8 +1,6 @@
|
||||
edge-tts==7.0.2
|
||||
jieba==0.42.1
|
||||
openai==1.0.0
|
||||
bidict==0.23.1
|
||||
playsound==1.2.2
|
||||
tabulate>=0.9.0
|
||||
textual==7.0.0
|
||||
textual==5.3.0
|
||||
toml==0.10.2
|
||||
transitions==0.9.3
|
||||
requests>=2.31.0
|
||||
webdavclient3>=3.0.0
|
||||
|
||||
+4
-18
@@ -1,21 +1,7 @@
|
||||
import heurams.services.version as ver
|
||||
|
||||
# __main__.py
|
||||
def main():
|
||||
prompt = f"""HeurAMS {ver.ver} 已经被成功地安装在系统中.
|
||||
HeurAMS 被设计为一个带有辅助记忆调度器功能的软件包, 无法直接被执行, 但可被其他 Python 程序调用.
|
||||
若您想启动内置的基本用户界面:
|
||||
prompt = """HeurAMS 已经被成功地安装在系统中.
|
||||
但 HeurAMS 被设计为一个带有辅助记忆调度器功能的软件包, 无法直接被执行, 但可被其他 Python 程序调用.
|
||||
若您想启动内置的基本用户界面,
|
||||
请运行 python -m heurams.interface,
|
||||
或者 python -m heurams.interface.__main__
|
||||
python 代指您使用的解释器, 在某些发行版中可能是 python3, 而 python 命令被指向了 python2.
|
||||
尽管项目保留了 requirements.txt, 我们仍不推荐使用系统 python 和原始 venv 进行开发.
|
||||
项目的推荐开发环境工具是 uv.
|
||||
如果你的环境已经安装了 uv:
|
||||
先运行 uv sync 同步环境, 此命令只需要执行一遍, uv 会自动处理依赖.
|
||||
然后通过运行 uv run tui 启动内置基本用户界面.
|
||||
此时您的解释器在项目目录里的 .venv/bin 中, 使用 IDE 开发前, 务必切换解释器!
|
||||
注意: 一个常见的误区是, 执行 interface 下的 __main__.py 运行基本用户界面, 这会导致 Python 上下文环境异常, 请不要这样做."""
|
||||
print(prompt)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
print(prompt)
|
||||
|
||||
+30
-19
@@ -6,34 +6,45 @@
|
||||
import pathlib
|
||||
from contextvars import ContextVar
|
||||
|
||||
from heurams.services.config import ConfigDict
|
||||
from heurams.services.config import ConfigFile
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
# 默认数据目录, 以包目录下的 data 为准
|
||||
# 用户数据目录, 以运行目录下的 data 为准
|
||||
|
||||
rootdir: pathlib.Path = pathlib.Path(__file__).parent
|
||||
"""包目录路径, 也就是 heurams 目录."""
|
||||
|
||||
workdir = pathlib.Path.cwd()
|
||||
"""工作目录路径."""
|
||||
# 默认配置文件路径规定: 以包目录为准
|
||||
# 用户配置文件路径规定: 以运行目录为准
|
||||
# 数据文件路径规定: 以运行目录为准
|
||||
|
||||
rootdir = pathlib.Path(__file__).parent
|
||||
print(f"rootdir: {rootdir}")
|
||||
logger = get_logger(__name__)
|
||||
logger.debug(f"包目录: {rootdir}")
|
||||
logger.debug(f"项目根目录: {rootdir}")
|
||||
workdir = pathlib.Path.cwd()
|
||||
print(f"workdir: {workdir}")
|
||||
logger.debug(f"工作目录: {workdir}")
|
||||
|
||||
(workdir / "data" / "config").mkdir(parents=True, exist_ok=True)
|
||||
|
||||
config_var: ContextVar[ConfigDict].get = ContextVar(
|
||||
"config_var",
|
||||
default=ConfigDict(workdir / "data" / "config"),
|
||||
config_var: ContextVar[ConfigFile] = ContextVar(
|
||||
"config_var", default=ConfigFile(rootdir / "default" / "config" / "config.toml")
|
||||
)
|
||||
"""配置对象的全局引用对象."""
|
||||
try:
|
||||
config_var: ContextVar[ConfigFile] = ContextVar(
|
||||
"config_var", default=ConfigFile(workdir / "config" / "config.toml")
|
||||
) # 配置文件
|
||||
print("已加载自定义用户配置")
|
||||
logger.info("已加载自定义用户配置, 路径: %s", workdir / "config" / "config.toml")
|
||||
except Exception as e:
|
||||
print("未能加载自定义用户配置")
|
||||
logger.warning("未能加载自定义用户配置, 错误: %s", e)
|
||||
if pathlib.Path(workdir / "config" / "config_dev.toml").exists():
|
||||
print("使用开发设置")
|
||||
logger.debug("使用开发设置")
|
||||
config_var: ContextVar[ConfigFile] = ContextVar(
|
||||
"config_var", default=ConfigFile(workdir / "config" / "config_dev.toml")
|
||||
)
|
||||
# runtime_var: ContextVar = ContextVar('runtime_var', default=dict()) # 运行时共享数据
|
||||
|
||||
|
||||
class ConfigContext:
|
||||
"""
|
||||
功能完备的上下文管理器
|
||||
用于临时切换配置引用对象的作用域, 支持嵌套使用
|
||||
用于临时切换配置的作用域, 支持嵌套使用
|
||||
|
||||
Example:
|
||||
>>> with ConfigContext(test_config):
|
||||
@@ -41,7 +52,7 @@ class ConfigContext:
|
||||
>>> get_daystamp() # 恢复原配置
|
||||
"""
|
||||
|
||||
def __init__(self, config_provider: ConfigDict):
|
||||
def __init__(self, config_provider: ConfigFile):
|
||||
self.config_provider = config_provider
|
||||
self._token = None
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# [调试] 将更改保存到文件
|
||||
persist_to_file = 1
|
||||
|
||||
# [调试] 覆写时间, 设为 -1 以禁用
|
||||
daystamp_override = -1
|
||||
timestamp_override = -1
|
||||
|
||||
# [调试] 一键通过
|
||||
quick_pass = 1
|
||||
|
||||
# 对于每个项目的默认新记忆原子数量
|
||||
scheduled_num = 8
|
||||
|
||||
# UTC 时间戳修正 仅用于 UNIX 日时间戳的生成修正, 单位为秒
|
||||
timezone_offset = +28800 # 中国标准时间 (UTC+8)
|
||||
|
||||
[interface]
|
||||
|
||||
[interface.memorizor]
|
||||
autovoice = true # 自动语音播放, 仅限于 recognition 组件
|
||||
|
||||
[algorithm]
|
||||
default = "SM-2" # 主要算法; 可选项: SM-2, SM-15M, FSRS
|
||||
|
||||
[puzzles] # 谜题默认配置
|
||||
|
||||
[puzzles.mcq]
|
||||
max_riddles_num = 2
|
||||
|
||||
[puzzles.cloze]
|
||||
min_denominator = 3
|
||||
|
||||
[paths] # 相对于配置文件的 ".." (即工作目录) 而言 或绝对路径
|
||||
nucleon_dir = "./data/nucleon"
|
||||
electron_dir = "./data/electron"
|
||||
global_dir = "./data/global" # 全局数据路径, SM-15 等算法需要
|
||||
orbital_dir = "./data/orbital"
|
||||
cache_dir = "./data/cache"
|
||||
template_dir = "./data/template"
|
||||
|
||||
[services] # 定义服务到提供者的映射
|
||||
audio = "playsound" # 可选项: playsound(通用), termux(仅用于支持 Android Termux), mpg123(TODO)
|
||||
tts = "edgetts" # 可选项: edgetts
|
||||
llm = "openai" # 可选项: openai
|
||||
sync = "webdav" # 可选项: 留空, webdav
|
||||
|
||||
[providers.tts.edgetts] # EdgeTTS 设置
|
||||
voice = "zh-CN-XiaoxiaoNeural" # 可选项: zh-CN-YunjianNeural (男声), zh-CN-XiaoxiaoNeural (女声)
|
||||
|
||||
[providers.llm.openai] # 与 OpenAI 相容的语言模型接口服务设置
|
||||
url = ""
|
||||
key = ""
|
||||
|
||||
[providers.sync.webdav] # WebDAV 同步设置
|
||||
url = ""
|
||||
username = ""
|
||||
password = ""
|
||||
remote_path = "/heurams/"
|
||||
verify_ssl = true
|
||||
|
||||
[sync]
|
||||
@@ -1,3 +1,2 @@
|
||||
# Interface - 用户界面
|
||||
|
||||
与界面系统**强绑定**的相关代码文件, "界面系统" 在此处是基本界面实现相关的 Textual 框架
|
||||
|
||||
@@ -1,74 +1,63 @@
|
||||
from time import sleep, perf_counter
|
||||
print("欢迎使用基本用户界面!")
|
||||
print("加载配置与上下文... ", end="", flush=True)
|
||||
_start1 = perf_counter()
|
||||
_start = perf_counter()
|
||||
from heurams.context import *
|
||||
_end = perf_counter()
|
||||
print(f"已完成! (耗时: {round(1000 * (_end - _start))}ms)")
|
||||
|
||||
print("加载用户界面框架... ", end="", flush=True)
|
||||
_start = perf_counter()
|
||||
from textual.app import App
|
||||
from textual.widgets import Button
|
||||
_end = perf_counter()
|
||||
print(f"已完成! (耗时: {round(1000 * (_end - _start))}ms)")
|
||||
|
||||
print("加载用户界面布局... ", end="", flush=True)
|
||||
_start = perf_counter()
|
||||
from heurams.context import config_var
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .screens.about import AboutScreen
|
||||
from .screens.dashboard import DashboardScreen
|
||||
from .screens.navigator import NavigatorScreen
|
||||
from .screens.nucreator import NucleonCreatorScreen
|
||||
from .screens.precache import PrecachingScreen
|
||||
from .screens.setting import SettingScreen
|
||||
from .screens.synctool import SyncScreen
|
||||
_end = perf_counter()
|
||||
print(f"已完成! (耗时: {round(1000 * (_end - _start))}ms)")
|
||||
|
||||
print(f"组件目录: {rootdir}")
|
||||
print(f"工作目录: {workdir}")
|
||||
_end1 = perf_counter()
|
||||
print(f"前置工作共计耗时: {round(1000 * (_end1 - _start1))}ms")
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def environment_check():
|
||||
from pathlib import Path
|
||||
|
||||
logger.debug("检查环境路径")
|
||||
|
||||
for i in config_var.get()["paths"].values():
|
||||
i = Path(i)
|
||||
if not i.exists():
|
||||
logger.info("创建目录: %s", i)
|
||||
print(f"创建 {i}")
|
||||
i.mkdir(exist_ok=True, parents=True)
|
||||
else:
|
||||
logger.debug("目录已存在: %s", i)
|
||||
print(f"找到 {i}")
|
||||
logger.debug("环境检查完成")
|
||||
|
||||
|
||||
class HeurAMSApp(App):
|
||||
TITLE = "潜进"
|
||||
CSS_PATH = "css/main.tcss"
|
||||
SUB_TITLE = "启发式辅助记忆调度器"
|
||||
BINDINGS = [
|
||||
("q", "go_back", "退出"),
|
||||
("d", "toggle_dark", "主题"),
|
||||
("n", "app.push_screen('navigator')", "导航"),
|
||||
("z", "app.push_screen('about')", "关于"),
|
||||
("q", "quit", "退出"),
|
||||
("d", "toggle_dark", "切换色调"),
|
||||
("1", "app.push_screen('dashboard')", "仪表盘"),
|
||||
("2", "app.push_screen('precache_all')", "缓存管理器"),
|
||||
("3", "app.push_screen('nucleon_creator')", "创建新单元"),
|
||||
# ("4", "app.push_screen('synctool')", "同步工具"),
|
||||
("0", "app.push_screen('about')", "版本信息"),
|
||||
]
|
||||
SCREENS = {
|
||||
"dashboard": DashboardScreen,
|
||||
"nucleon_creator": NucleonCreatorScreen,
|
||||
"precache_all": PrecachingScreen,
|
||||
"synctool": SyncScreen,
|
||||
"about": AboutScreen,
|
||||
"navigator": NavigatorScreen,
|
||||
"setting": SettingScreen,
|
||||
}
|
||||
|
||||
def on_mount(self) -> None:
|
||||
environment_check()
|
||||
self.push_screen("dashboard")
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
pass
|
||||
# self.exit(event.button.id)
|
||||
|
||||
def action_go_back(self) -> None:
|
||||
quit()
|
||||
self.exit(event.button.id)
|
||||
|
||||
def action_do_nothing(self):
|
||||
print("DO NOTHING")
|
||||
self.refresh()
|
||||
|
||||
# 移除烦人的 "rich traceback"
|
||||
# Textual 官方不会管这破事, 写 Rich 写入脑了导致的
|
||||
# 不知道哪来的自信改标准库的 traceback
|
||||
# https://github.com/Textualize/textual/discussions/6255
|
||||
def _fatal_error(self):
|
||||
self._close_messages_no_wait()
|
||||
raise self._exception
|
||||
|
||||
def panic(self, *args):
|
||||
self._close_messages_no_wait()
|
||||
raise self._exception
|
||||
@@ -1,66 +1,18 @@
|
||||
from heurams.interface import *
|
||||
from textual.app import App
|
||||
from textual.widgets import Button
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.interface import HeurAMSApp
|
||||
from heurams.services.logger import get_logger
|
||||
import threading
|
||||
import zmq
|
||||
import pickle
|
||||
|
||||
from .screens.about import AboutScreen
|
||||
from .screens.dashboard import DashboardScreen
|
||||
from .screens.nucreator import NucleonCreatorScreen
|
||||
from .screens.precache import PrecachingScreen
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
def environment_check():
|
||||
from pathlib import Path
|
||||
|
||||
logger.debug("检查环境路径")
|
||||
subdir = ["cache/voice", "repo", "global", "config"]
|
||||
for i in subdir:
|
||||
i = Path(config_var.get()["global"]["paths"]["data"]) / i
|
||||
if not i.exists():
|
||||
logger.info("创建目录: %s", i)
|
||||
print(f"创建 {i}")
|
||||
i.mkdir(exist_ok=True, parents=True)
|
||||
else:
|
||||
logger.debug("目录已存在: %s", i)
|
||||
print(f"找到 {i}")
|
||||
logger.debug("环境检查完成")
|
||||
|
||||
def start_debug_server(app):
|
||||
logger = get_logger("zmq_debug")
|
||||
context = zmq.Context()
|
||||
socket = context.socket(zmq.REP)
|
||||
port = config_var.get()['global'].get('zmq_debug_port', 5555)
|
||||
socket.bind(f"tcp://*:{port}")
|
||||
logger.info(f"ZMQ Debug server started on port {port}")
|
||||
first = 1
|
||||
while True:
|
||||
msg = socket.recv()
|
||||
code = pickle.loads(msg)
|
||||
namespace = {"app": app, "logger": logger, "config_var": config_var}
|
||||
if first:
|
||||
app.title += ' [调试已连接]'
|
||||
first = 0
|
||||
try:
|
||||
# 先尝试 eval
|
||||
result = eval(code, namespace)
|
||||
socket.send(pickle.dumps(f"成功: {result}"))
|
||||
except SyntaxError:
|
||||
# 再尝试 exec
|
||||
try:
|
||||
exec(code, namespace)
|
||||
socket.send(pickle.dumps(f"成功: 执行完成"))
|
||||
except Exception as e:
|
||||
socket.send(pickle.dumps(f"错误: {e}"))
|
||||
except Exception as e:
|
||||
socket.send(pickle.dumps(f"错误: {e}"))
|
||||
|
||||
def main():
|
||||
environment_check()
|
||||
|
||||
app = HeurAMSApp()
|
||||
|
||||
if config_var.get()['global'].get('zmq_debug', False):
|
||||
threading.Thread(target=start_debug_server, args=(app,), daemon=True).start()
|
||||
|
||||
app.run(inline=False)
|
||||
app = HeurAMSApp()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
app.run()
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
NavigatorScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
.infview {
|
||||
width: 5fr
|
||||
}
|
||||
.dataview {
|
||||
width: 3fr
|
||||
}
|
||||
|
||||
.repo_listitem {
|
||||
layout: grid;
|
||||
grid-size: 2;
|
||||
}
|
||||
|
||||
.repo_listitem_btn {
|
||||
dock: right;
|
||||
offset: -5% 0
|
||||
}
|
||||
|
||||
#dialog {
|
||||
grid-size: 2;
|
||||
grid-gutter: 1 1;
|
||||
grid-rows: 1fr 3;
|
||||
padding: 0 1;
|
||||
width: 46;
|
||||
height: 12;
|
||||
border: thick $background 80%;
|
||||
background: $surface;
|
||||
}
|
||||
|
||||
#dashboardtop {
|
||||
height: 4
|
||||
}
|
||||
|
||||
#input-container {
|
||||
height: 3;
|
||||
margin-top: 1;
|
||||
align: center middle;
|
||||
}
|
||||
.container {
|
||||
height: auto;
|
||||
padding: 0 0 1 0;
|
||||
}
|
||||
#message-input {
|
||||
width: 1fr;
|
||||
margin-right: 1;
|
||||
}
|
||||
|
||||
#status-bar {
|
||||
height: 1;
|
||||
margin-top: 1;
|
||||
text-style: italic;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
.session-label {
|
||||
color: $primary;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
"""关于界面"""
|
||||
|
||||
#!/usr/bin/env python3
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
@@ -7,77 +6,69 @@ from textual.widgets import Button, Footer, Header, Label, Markdown, Static
|
||||
|
||||
import heurams.services.version as version
|
||||
from heurams.context import *
|
||||
import platform
|
||||
import shutil
|
||||
import psutil
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
class AboutScreen(Screen):
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
]
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer(id="about_container"):
|
||||
yield Label("[b]关于与版本信息[/b]")
|
||||
|
||||
# 获取系统信息
|
||||
textual_version = self._get_textual_version()
|
||||
terminal_info = self._get_terminal_info()
|
||||
python_version = self._get_python_version()
|
||||
os_version = self._get_os_version()
|
||||
disk_usage = self._get_disk_usage()
|
||||
memory_info = self._get_memory_info()
|
||||
|
||||
about_text = f"""
|
||||
# 关于 "潜进"
|
||||
|
||||
主程序库版本: `{version.ver}-python`
|
||||
用户界面分支: `Textual TUI (基本用户界面)`
|
||||
用户界面版本: `{version.ver}`
|
||||
API 版本代号: `{version.codename.capitalize()}`
|
||||
版本 {version.ver} {version.stage.capitalize()}
|
||||
|
||||
一个基于启发式算法与认知科学理论的辅助记忆调度器, 旨在帮助用户更高效地进行记忆工作与学习规划.
|
||||
开发代号: {version.codename.capitalize()}
|
||||
|
||||
以 AGPL-3.0 开放源代码, 这直接意味着任何个体直接基于此代码对外或内部提供的应用和服务, 无论本地或网络, 必须向所有用户公开完整修改后的源代码, 且继续沿用 AGPL-3.0 协议.
|
||||
一个基于启发式算法的开放源代码记忆调度器, 旨在帮助用户更高效地进行记忆工作与学习规划.
|
||||
|
||||
您正使用的 TUI 用户界面是 python 版本程序库自带的基本用户界面, 作为基本的全功能前端实现与程序库测试, 如果您想去除它, 请移除程序库根目录中的 interface 文件夹.
|
||||
以 AGPL-3.0 开放源代码
|
||||
|
||||
您可在项目主页 https://ams.pluv27.top 获取用户指南, 开发文档与软件更新.
|
||||
开发人员:
|
||||
|
||||
如果您觉得这个软件有用, 可以在它的源代码仓库给它添加一个星标 :)
|
||||
- Wang Zhiyu([@pluvium27](https://github.com/pluvium27)): 项目作者
|
||||
|
||||
> 潜进(HeurAMS), 以及它作为一个"程序库"是自由且免费的, 但是开发工作必须投入大量精力.
|
||||
> 您可以加入各种语言的翻译团队来翻译软件的界面, 您还可以制作图像、主题、音效, 或者改进软件配套的文档……
|
||||
> 不管您来自何方, 我们都欢迎您加入社区并做出贡献.
|
||||
> 我们的共同目标是为人人带来高品质的辅助记忆 & 学习软件.
|
||||
> 您的慷慨支持, 我们必当涌泉相报.
|
||||
|
||||
开发人员列表:
|
||||
|
||||
- Wang Zhiyu([@pluvium27](https://github.com/pluvium27)): 发起项目与主要维护者
|
||||
|
||||
特别感谢以下人士, 他们的算法与理论构成了此软件算法的基石:
|
||||
特别感谢:
|
||||
|
||||
- [Piotr A. Woźniak](https://supermemo.guru/wiki/Piotr_Wozniak): SM-2 算法与 SM-15 算法理论
|
||||
- [Kazuaki Tanida](https://github.com/slaypni): SM-15 算法的 CoffeeScript 实现
|
||||
- [Thoughts Memo](https://www.zhihu.com/people/L.M.Sherlock): 文献参考
|
||||
|
||||
# 参与贡献
|
||||
|
||||
# 运行环境信息
|
||||
我们是一个年轻且包容的社区, 由技术人员, 设计师, 文书工作者, 以及创意人员共同构成,
|
||||
|
||||
Python 解释器版本: {python_version}
|
||||
Textual 框架版本: {textual_version}
|
||||
终端模拟器: {terminal_info}
|
||||
操作系统版本: {os_version}
|
||||
存储余量: {disk_usage}
|
||||
内存大小: {memory_info}
|
||||
通过我们协力开发的软件为所有人谋取福祉.
|
||||
|
||||
报告问题时, 请复制这些信息到问题描述, 并上传软件日志 `heurams.log` 作为附件, 以协助开发者定位错误
|
||||
上述工作不可避免地让我们确立了下列价值观 (取自 KDE 宣言):
|
||||
|
||||
- 开放治理 确保更多人能参与我们的领导和决策进程;
|
||||
|
||||
- 自由软件 确保我们的工作成果随时能为所有人所用;
|
||||
|
||||
- 多样包容 确保所有人都能加入社区并参加工作;
|
||||
|
||||
- 创新精神 确保新思路能不断涌现并服务于所有人;
|
||||
|
||||
- 共同产权 确保我们能团结一致;
|
||||
|
||||
- 迎合用户 确保我们的成果对所有人有用.
|
||||
|
||||
综上所述, 在为我们共同目标奋斗的过程中, 我们认为上述价值观反映了我们社区的本质, 是我们始终如一地保持初心的关键所在.
|
||||
|
||||
这是一项立足于协作精神的事业, 它的运作和产出不受任何单一个人或者机构的操纵.
|
||||
|
||||
我们的共同目标是为人人带来高品质的辅助记忆 & 学习软件.
|
||||
|
||||
不管您来自何方, 我们都欢迎您加入社区并做出贡献.
|
||||
"""
|
||||
|
||||
# """
|
||||
# 学术数据
|
||||
|
||||
# "潜进" 的用户数据可用于科学方面的研究, 我们将在未来版本添加学术数据的收集和展示平台
|
||||
# """
|
||||
yield Markdown(about_text, classes="about-markdown")
|
||||
|
||||
yield Button(
|
||||
@@ -95,68 +86,3 @@ Textual 框架版本: {textual_version}
|
||||
event.stop()
|
||||
if event.button.id == "back_button":
|
||||
self.action_go_back()
|
||||
|
||||
def _get_textual_version(self) -> str:
|
||||
"""获取 Textual 框架版本"""
|
||||
try:
|
||||
import textual
|
||||
return textual.__version__
|
||||
except (ImportError, AttributeError):
|
||||
return "未知"
|
||||
|
||||
def _get_terminal_info(self) -> str:
|
||||
"""获取终端模拟器信息"""
|
||||
terminal = shutil.which("terminal")
|
||||
if terminal:
|
||||
return terminal
|
||||
# 尝试从环境变量获取
|
||||
terminal_env = os.environ.get('TERM_PROGRAM') or os.environ.get('TERM')
|
||||
return terminal_env or "未知"
|
||||
|
||||
def _get_python_version(self) -> str:
|
||||
"""获取 Python 解释器版本"""
|
||||
return platform.python_version()
|
||||
|
||||
def _get_os_version(self) -> str:
|
||||
"""获取操作系统版本"""
|
||||
try:
|
||||
if platform.system() == "Darwin":
|
||||
# macOS
|
||||
import subprocess
|
||||
result = subprocess.run(['sw_vers', '-productVersion'],
|
||||
capture_output=True, text=True)
|
||||
return f"macOS {result.stdout.strip()}"
|
||||
elif platform.system() == "Windows":
|
||||
# Windows
|
||||
return f"Windows {platform.release()}"
|
||||
elif platform.system() == "Linux":
|
||||
# Linux - 尝试获取发行版信息
|
||||
try:
|
||||
import distro
|
||||
return f"{distro.name()} {distro.version()}"
|
||||
except (ImportError, AttributeError):
|
||||
return platform.platform()
|
||||
else:
|
||||
return platform.platform()
|
||||
except Exception:
|
||||
return platform.platform()
|
||||
|
||||
def _get_disk_usage(self) -> str:
|
||||
"""获取磁盘使用情况"""
|
||||
try:
|
||||
usage = psutil.disk_usage('/')
|
||||
free_gb = usage.free / (1024 ** 3)
|
||||
total_gb = usage.total / (1024 ** 3)
|
||||
percent_free = (free_gb / total_gb) * 100
|
||||
return f"{free_gb:.1f} GB ({percent_free:.1f}%)"
|
||||
except Exception:
|
||||
return "未知"
|
||||
|
||||
def _get_memory_info(self) -> str:
|
||||
"""获取内存信息"""
|
||||
try:
|
||||
memory = psutil.virtual_memory()
|
||||
total_gb = memory.total / (1024 ** 3)
|
||||
return f"{total_gb:.1f} GB"
|
||||
except Exception:
|
||||
return "未知"
|
||||
@@ -1,27 +1,19 @@
|
||||
"""仪表盘界面"""
|
||||
|
||||
from functools import reduce
|
||||
#!/usr/bin/env python3
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer, Container, Horizontal, Vertical
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button, Footer, Header, Label, ListItem, ListView, Static
|
||||
from textual.layouts import horizontal
|
||||
from textual.widgets import (Button, Footer, Header, Label, ListItem, ListView,
|
||||
Static)
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.services.timer as timer
|
||||
import heurams.services.version as version
|
||||
from heurams.context import *
|
||||
from heurams.kernel.particles import *
|
||||
from heurams.kernel.repolib import *
|
||||
from heurams.kernel.algorithms import algorithms
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .about import AboutScreen
|
||||
from .navigator import NavigatorScreen
|
||||
from .preparation import PreparationScreen
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -31,9 +23,6 @@ class DashboardScreen(Screen):
|
||||
"""主仪表盘屏幕"""
|
||||
|
||||
SUB_TITLE = "仪表盘"
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -42,119 +31,130 @@ class DashboardScreen(Screen):
|
||||
classes: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
self.repostat = {}
|
||||
self.title2dirname = {}
|
||||
self.title2repo = {}
|
||||
self.dirname2repo = {}
|
||||
self._load_data()
|
||||
self.nextdates = {}
|
||||
self.texts = {}
|
||||
self.stay_enabled = {}
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""组合界面组件"""
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer():
|
||||
yield Horizontal(
|
||||
Vertical(
|
||||
Label(f'欢迎使用 "潜进" 版本 {version.ver}'),
|
||||
yield ScrollableContainer(
|
||||
Label('欢迎使用 "潜进" 启发式辅助记忆调度器', classes="title-label"),
|
||||
Label(f"当前 UNIX 日时间戳: {timer.get_daystamp()}"),
|
||||
Label(f'时区修正: UTC+{config_var.get()["timezone_offset"] / 3600}'),
|
||||
Label(f"使用算法: {config_var.get()['algorithm']['default']}"),
|
||||
Label("选择待学习或待修改的记忆单元集:", classes="title-label"),
|
||||
ListView(id="union-list", classes="union-list-view"),
|
||||
Label(
|
||||
f"当前 UNIX 日时间戳: {timer.get_daystamp()}"
|
||||
f'"潜进" 启发式辅助记忆调度器 | 版本 {version.ver} '
|
||||
f"{version.codename.capitalize()} 2025"
|
||||
),
|
||||
Label(f"应用时区修正: UTC+{config_var.get()['services']['timer']['timezone_offset'] / 3600}"),
|
||||
Label(f"全局算法设置: {config_var.get()['interface']['global']['algorithm']}: {algorithms[config_var.get()['interface']['global']['algorithm']].desc}"),
|
||||
classes="column infview",
|
||||
),
|
||||
Vertical(
|
||||
Label(f"已加载 {len(self.repostat)} 个单元集", classes='dataview'),
|
||||
Label(f"共计 {reduce(lambda x, y: x + y, map(lambda x: x.get('unit_sum'), self.repostat.values()))} 个单元", classes='dataview'),
|
||||
Label(f"已激活 {reduce(lambda x, y: x + y, map(lambda x: x.get('activated_sum'), self.repostat.values()))} 个单元", classes='dataview'),
|
||||
Label(f""),
|
||||
classes="column dataview",
|
||||
),
|
||||
id="dashboardtop"
|
||||
)
|
||||
|
||||
yield ListView(id="repo-list", classes="repo-list-view")
|
||||
yield Label(f'"潜进" 启发式辅助记忆调度器 版本 {version.ver} {version.stage.capitalize()}')
|
||||
yield Footer()
|
||||
|
||||
def _load_data(self):
|
||||
self.repo_dirs = Repo.probe_valid_repos_in_dir(
|
||||
Path(config_var.get()['global']["paths"]["data"]) / "repo"
|
||||
)
|
||||
for repo_dir in self.repo_dirs:
|
||||
repo = Repo.create_from_repodir(repo_dir)
|
||||
self._analyse_repo(repo)
|
||||
def analyser(self, filename: str) -> dict:
|
||||
"""分析文件状态以生成显示文本
|
||||
|
||||
def _analyse_repo(self, repo: Repo):
|
||||
dirname = repo.source.name # type: ignore
|
||||
title = repo.manifest["title"]
|
||||
Args:
|
||||
filename: 要分析的文件名
|
||||
|
||||
Returns:
|
||||
dict: 包含显示文本的字典,键为行号
|
||||
"""
|
||||
from heurams.kernel.particles.loader import load_electron, load_nucleon
|
||||
|
||||
result = {}
|
||||
filestem = pathlib.Path(filename).stem
|
||||
|
||||
# 构建电子文件路径
|
||||
electron_dir = config_var.get()["paths"]["electron_dir"]
|
||||
electron_file_path = pathlib.Path(electron_dir) / f"{filestem}.json"
|
||||
|
||||
logger.debug(f"电子文件路径: {electron_file_path}")
|
||||
|
||||
# 确保电子文件存在
|
||||
if not electron_file_path.exists():
|
||||
electron_file_path.touch()
|
||||
electron_file_path.write_text("{}")
|
||||
|
||||
# 加载电子数据
|
||||
electron_dict = load_electron(path=electron_file_path)
|
||||
logger.debug(f"电子数据: {electron_dict}")
|
||||
|
||||
# 分析电子状态
|
||||
is_due = 0
|
||||
unit_sum = len(repo)
|
||||
activated_sum = 0
|
||||
nextdate = float('inf')
|
||||
for i in repo.ident_index:
|
||||
nucleon = pt.Nucleon.create_on_nucleonic_data(
|
||||
nucleonic_data=repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
electron = pt.Electron.create_on_electonic_data(
|
||||
electronic_data=repo.electronic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
if electron.is_activated():
|
||||
activated_sum += 1
|
||||
is_activated = 0
|
||||
nextdate = 0x3F3F3F3F
|
||||
|
||||
for electron in electron_dict.values():
|
||||
logger.debug(f"{electron}, 是否到期: {electron.is_due()}")
|
||||
|
||||
if electron.is_due():
|
||||
is_due = 1
|
||||
if electron.is_activated():
|
||||
is_activated = 1
|
||||
nextdate = min(nextdate, electron.nextdate())
|
||||
is_unfinished = unit_sum > activated_sum
|
||||
if is_unfinished:
|
||||
nextdate = min(nextdate, timer.get_daystamp())
|
||||
need_to_study = is_due or is_unfinished
|
||||
prompt = f"{title}\0\n 进度: {activated_sum}/{unit_sum} ({round(activated_sum/unit_sum*100)}%)\n {'需要学习' if need_to_study else '无需操作'}"
|
||||
stat = {
|
||||
"is_due": is_due,
|
||||
"unit_sum": unit_sum,
|
||||
"title": title,
|
||||
"activated_sum": activated_sum,
|
||||
"nextdate": nextdate,
|
||||
"is_unfinished": is_unfinished,
|
||||
"need_to_study": need_to_study,
|
||||
"prompt": prompt,
|
||||
"dirname": dirname,
|
||||
}
|
||||
self.repostat[dirname] = stat
|
||||
self.title2dirname[title] = dirname
|
||||
self.title2repo[title] = repo
|
||||
self.dirname2repo[dirname] = repo
|
||||
|
||||
# 检查是否需要更多复习
|
||||
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
||||
nucleon_path = pathlib.Path(nucleon_dir) / f"{filestem}.toml"
|
||||
nucleon_count = len(load_nucleon(nucleon_path))
|
||||
electron_count = len(electron_dict)
|
||||
is_more = not (electron_count >= nucleon_count)
|
||||
|
||||
logger.debug(f"是否需要更多复习: {is_more}")
|
||||
|
||||
# 更新状态
|
||||
self.nextdates[filename] = nextdate
|
||||
self.stay_enabled[filename] = is_due or is_more
|
||||
|
||||
# 构建返回结果
|
||||
result[0] = f"{filename}\0"
|
||||
|
||||
if not is_activated:
|
||||
result[1] = " 尚未激活"
|
||||
else:
|
||||
status_text = "需要复习" if is_due else "当前无需复习"
|
||||
result[1] = f"下一次复习: {nextdate}\n{status_text}"
|
||||
|
||||
return result
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""挂载组件时初始化"""
|
||||
repo_list_widget = self.query_one("#repo-list", ListView)
|
||||
union_list_widget = self.query_one("#union-list", ListView)
|
||||
probe = probe_all(0)
|
||||
|
||||
# 分析所有文件
|
||||
for file in probe["nucleon"]:
|
||||
self.texts[file] = self.analyser(file)
|
||||
|
||||
# 按下次复习时间排序
|
||||
repodirs = sorted(
|
||||
self.repo_dirs,
|
||||
key=lambda f: self.repostat[f.name]["nextdate"],
|
||||
nucleon_files = sorted(
|
||||
probe["nucleon"],
|
||||
key=lambda f: self.nextdates[f],
|
||||
reverse=True,
|
||||
)
|
||||
repotitles = map(lambda f: self.repostat[f.name]["title"], repodirs)
|
||||
|
||||
# 填充列表
|
||||
if not repodirs:
|
||||
repo_list_widget.append(
|
||||
if not probe["nucleon"]:
|
||||
union_list_widget.append(
|
||||
ListItem(
|
||||
Static(
|
||||
"在 ./data/repo/ 中未找到任何仓库.\n"
|
||||
"请导入仓库后重启应用, 或者新建空的仓库."
|
||||
"在 ./nucleon/ 中未找到任何内容源数据文件。\n"
|
||||
"请放置文件后重启应用,或者新建空的单元集。"
|
||||
)
|
||||
)
|
||||
)
|
||||
repo_list_widget.disabled = True
|
||||
union_list_widget.disabled = True
|
||||
return
|
||||
|
||||
for repotitle in repotitles:
|
||||
prompt = self.repostat[self.title2dirname[repotitle]]["prompt"]
|
||||
list_item = ListItem(Label(prompt), Button(f"开始学习", flat=True, variant="primary", classes="repo_listitem_btn", id=f"launch_{self.repostat[self.title2dirname[repotitle]]['dirname']}"), classes="repo_listitem")
|
||||
repo_list_widget.append(list_item)
|
||||
for file in nucleon_files:
|
||||
text = self.texts[file]
|
||||
list_item = ListItem(Label(f"{text[0]}\n{text[1]}"))
|
||||
union_list_widget.append(list_item)
|
||||
|
||||
# if not self.stay_enabled[repodir]:
|
||||
# list_item.disabled = True
|
||||
if not self.stay_enabled[file]:
|
||||
list_item.disabled = True
|
||||
|
||||
def on_list_view_selected(self, event) -> None:
|
||||
"""处理列表项选择事件"""
|
||||
@@ -162,34 +162,46 @@ class DashboardScreen(Screen):
|
||||
return
|
||||
|
||||
selected_label = event.item.query_one(Label)
|
||||
label_text = str(selected_label.render())
|
||||
label_text = str(selected_label.renderable)
|
||||
|
||||
if "未找到任何仓库" in label_text:
|
||||
if "未找到任何 .toml 文件" in label_text:
|
||||
return
|
||||
|
||||
# 提取文件名
|
||||
selected_repotitle = label_text.partition("\0")[0].replace("*", "")
|
||||
selected_repo = self.title2repo[label_text.partition("\0")[0].replace("*", "")]
|
||||
selected_filename = pathlib.Path(label_text.partition("\0")[0].replace("*", ""))
|
||||
|
||||
# 构建文件路径
|
||||
nucleon_dir = config_var.get()["paths"]["nucleon_dir"]
|
||||
electron_dir = config_var.get()["paths"]["electron_dir"]
|
||||
|
||||
nucleon_file_path = pathlib.Path(nucleon_dir) / selected_filename
|
||||
electron_file_path = (
|
||||
pathlib.Path(electron_dir) / f"{selected_filename.stem}.json"
|
||||
)
|
||||
|
||||
# 跳转到准备屏幕
|
||||
self.app.push_screen(
|
||||
PreparationScreen(
|
||||
selected_repo, self.repostat[self.title2dirname[selected_repotitle]]
|
||||
)
|
||||
)
|
||||
self.app.push_screen(PreparationScreen(nucleon_file_path, electron_file_path))
|
||||
|
||||
def on_button_pressed(self, event) -> None:
|
||||
"""处理按钮点击事件"""
|
||||
button_id = event.button.id
|
||||
|
||||
if button_id == "new_nucleon_button":
|
||||
from .nucreator import NucleonCreatorScreen
|
||||
|
||||
new_screen = NucleonCreatorScreen()
|
||||
self.app.push_screen(new_screen)
|
||||
|
||||
elif button_id == "precache_all_button":
|
||||
from .precache import PrecachingScreen
|
||||
|
||||
precache_screen = PrecachingScreen()
|
||||
self.app.push_screen(precache_screen)
|
||||
|
||||
elif button_id == "about_button":
|
||||
about_screen = AboutScreen()
|
||||
self.app.push_screen(about_screen)
|
||||
|
||||
def action_quit_app(self) -> None:
|
||||
"""退出应用程序"""
|
||||
self.app.exit()
|
||||
|
||||
def action_open_navigator(self) -> None:
|
||||
"""打开导航器"""
|
||||
self.app.push_screen(NavigatorScreen())
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
logger.debug(f"event.button.id: {event.button.id}")
|
||||
"""处理按钮点击事件"""
|
||||
if str(event.button.id).startswith("launch_"): # type: ignore
|
||||
from .preparation import launch
|
||||
launch(repo=self.dirname2repo[event.button.id[7:]], app=self.app, scheduled_num=-1) # type: ignore
|
||||
# TODO: 这样启动的记忆实例的状态机无法绑定到 PreparationScreen 中
|
||||
|
||||
@@ -1,204 +0,0 @@
|
||||
"""收藏夹管理器界面"""
|
||||
|
||||
import base64
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import (
|
||||
Button,
|
||||
Footer,
|
||||
Header,
|
||||
Label,
|
||||
ListItem,
|
||||
ListView,
|
||||
Markdown,
|
||||
Static,
|
||||
)
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.repolib import Repo
|
||||
from heurams.services.favorite_service import FavoriteItem, favorite_manager
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class FavoriteManagerScreen(Screen):
|
||||
"""收藏夹管理器屏幕"""
|
||||
|
||||
SUB_TITLE = "收藏夹"
|
||||
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
("d", "toggle_dark", ""),
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
self.favorites: List[FavoriteItem] = []
|
||||
self._load_favorites()
|
||||
|
||||
def _load_favorites(self) -> None:
|
||||
"""加载收藏列表"""
|
||||
self.favorites = favorite_manager.get_all()
|
||||
logger.debug("加载 %d 个收藏项", len(self.favorites))
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""组合界面组件"""
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer(id="favorites-container"):
|
||||
if not self.favorites:
|
||||
yield Label("暂无收藏", classes="empty-label")
|
||||
yield Static("使用 * 键在记忆界面中添加收藏.")
|
||||
else:
|
||||
yield Label(f"共 {len(self.favorites)} 个收藏项", classes="count-label")
|
||||
yield ListView(id="favorites-list")
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""挂载后填充列表"""
|
||||
if self.favorites:
|
||||
list_view = self.query_one("#favorites-list")
|
||||
for fav in self.favorites:
|
||||
list_view.append(self._create_favorite_item(fav)) # type: ignore
|
||||
|
||||
def _encode_favorite_key(self, repo_path: str, ident: str) -> str:
|
||||
"""编码仓库路径和标识符为安全的按钮 ID 部分"""
|
||||
# 使用 \x00 分隔两部分,然后进行 base64 编码
|
||||
combined = f"{repo_path}\x00{ident}"
|
||||
encoded = base64.urlsafe_b64encode(combined.encode()).decode()
|
||||
# 去掉填充的等号
|
||||
return encoded.rstrip("=")
|
||||
|
||||
def _decode_favorite_key(self, key: str) -> tuple[str, str]:
|
||||
"""解码按钮 ID 部分为仓库路径和标识符"""
|
||||
# 补全等号以使长度是4的倍数
|
||||
padded = key + "=" * ((4 - len(key) % 4) % 4)
|
||||
decoded = base64.urlsafe_b64decode(padded.encode()).decode()
|
||||
repo_path, ident = decoded.split("\x00", 1)
|
||||
return repo_path, ident
|
||||
|
||||
def _create_favorite_item(self, fav: FavoriteItem) -> ListItem:
|
||||
"""创建收藏项列表项"""
|
||||
# 尝试获取仓库信息
|
||||
repo_info = self._get_repo_info(fav.repo_path, fav)
|
||||
title = repo_info.get("title", fav.repo_path) if repo_info else fav.repo_path
|
||||
content_preview = repo_info.get("content_preview", "") if repo_info else ""
|
||||
added_time = self._format_time(fav.added)
|
||||
|
||||
# 构建显示文本
|
||||
display_text = f"[b]{title}[/b] ({fav.ident})\n"
|
||||
if content_preview:
|
||||
display_text += f"{content_preview}\n"
|
||||
display_text += f"添加于: {added_time}"
|
||||
if fav.tags:
|
||||
display_text += f" 标签: {', '.join(fav.tags)}"
|
||||
|
||||
# 创建安全的按钮 ID
|
||||
button_key = self._encode_favorite_key(fav.repo_path, fav.ident)
|
||||
# 创建列表项,包含移除按钮
|
||||
container = ScrollableContainer(
|
||||
Markdown(display_text, classes="favorite-content"),
|
||||
Button("移除", id=f"remove-{button_key}", variant="error"),
|
||||
classes="favorite-item",
|
||||
)
|
||||
return ListItem(container)
|
||||
|
||||
def _get_repo_info(self, repo_path: str, fav: FavoriteItem) -> Optional[dict]:
|
||||
"""获取仓库信息(标题、原子内容预览)"""
|
||||
try:
|
||||
data_repo = Path(config_var.get()['global']["paths"]["data"]) / "repo"
|
||||
repo_dir = data_repo / repo_path
|
||||
if not repo_dir.exists():
|
||||
logger.warning("仓库目录不存在: %s", repo_dir)
|
||||
return None
|
||||
repo = Repo.create_from_repodir(repo_dir)
|
||||
# 获取原子内容预览
|
||||
content_preview = ""
|
||||
payload = repo.payload
|
||||
# 查找对应 ident 的 payload 条目
|
||||
for ident_key, content in payload:
|
||||
if ident_key == fav.ident:
|
||||
# 截断过长的内容
|
||||
if isinstance(content, dict) and "content" in content:
|
||||
text = content["content"]
|
||||
else:
|
||||
text = str(content)
|
||||
if len(text) > 100:
|
||||
content_preview = text[:100] + "..."
|
||||
else:
|
||||
content_preview = text
|
||||
break
|
||||
return {
|
||||
"title": repo.manifest["title"],
|
||||
"content_preview": content_preview,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("获取仓库信息失败: %s", e)
|
||||
return None
|
||||
|
||||
def _format_time(self, timestamp: int) -> str:
|
||||
"""格式化时间戳"""
|
||||
from datetime import datetime
|
||||
|
||||
dt = datetime.fromtimestamp(timestamp)
|
||||
return dt.strftime("%Y-%m-%d %H:%M")
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""处理按钮点击事件"""
|
||||
button_id = event.button.id
|
||||
if button_id and button_id.startswith("remove-"):
|
||||
# 提取编码后的键
|
||||
key = button_id[7:] # 去掉 "remove-" 前缀
|
||||
try:
|
||||
repo_path, ident = self._decode_favorite_key(key)
|
||||
self._remove_favorite(repo_path, ident)
|
||||
except Exception as e:
|
||||
logger.error("解析按钮 ID 失败: %s", e)
|
||||
self.app.notify("操作失败: 无效的按钮标识", severity="error")
|
||||
|
||||
def _remove_favorite(self, repo_path: str, ident: str) -> None:
|
||||
"""移除收藏项"""
|
||||
if favorite_manager.remove(repo_path, ident):
|
||||
self.app.notify(f"已移除收藏: {ident}", severity="information")
|
||||
# 重新加载列表
|
||||
self._load_favorites()
|
||||
# 刷新界面
|
||||
self._refresh_list()
|
||||
else:
|
||||
self.app.notify(f"移除失败: {ident}", severity="error")
|
||||
|
||||
def _refresh_list(self) -> None:
|
||||
"""刷新列表显示"""
|
||||
container = self.query_one("#favorites-container")
|
||||
# 清空容器
|
||||
for child in container.children:
|
||||
child.remove()
|
||||
# 重新组合
|
||||
if not self.favorites:
|
||||
container.mount(Label("暂无收藏", classes="empty-label"))
|
||||
container.mount(Static("使用 * 键在记忆界面中添加收藏。"))
|
||||
else:
|
||||
container.mount(
|
||||
Label(f"共 {len(self.favorites)} 个收藏项", classes="count-label")
|
||||
)
|
||||
list_view = ListView(id="favorites-list")
|
||||
container.mount(list_view)
|
||||
for fav in self.favorites:
|
||||
list_view.append(self._create_favorite_item(fav))
|
||||
|
||||
def action_go_back(self) -> None:
|
||||
"""返回上一屏幕"""
|
||||
self.app.pop_screen()
|
||||
|
||||
def action_toggle_dark(self) -> None:
|
||||
"""切换暗黑模式"""
|
||||
self.app.dark = not self.app.dark # type: ignore
|
||||
@@ -1 +0,0 @@
|
||||
"""整体式记忆工作界面"""
|
||||
@@ -1,258 +0,0 @@
|
||||
"""队列式记忆工作界面"""
|
||||
|
||||
from enum import Enum, auto
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Center, ScrollableContainer
|
||||
from textual.reactive import reactive
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button, Footer, Header, Label, Static
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.reactor import *
|
||||
from heurams.services.favorite_service import favorite_manager
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .. import shim
|
||||
|
||||
|
||||
class AtomState(Enum):
|
||||
FAILED = auto()
|
||||
NORMAL = auto()
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MemScreen(Screen):
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
("p", "prev", "查看上一个"),
|
||||
("d", "toggle_dark", ""),
|
||||
("v", "play_voice", "朗读"),
|
||||
("*", "toggle_favorite", "收藏"),
|
||||
("0,1,2,3", "app.push_screen('about')", ""),
|
||||
]
|
||||
|
||||
if config_var.get()['interface']['global']["quick_pass"]:
|
||||
BINDINGS.append(("k", "quick_pass", "正确应答"))
|
||||
BINDINGS.append(("f", "quick_fail", "错误应答"))
|
||||
rating = reactive(-1)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
phaser: Phaser,
|
||||
save_func: Callable,
|
||||
repo=None,
|
||||
name=None,
|
||||
id=None,
|
||||
classes=None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
self.phaser = phaser
|
||||
self.save_func = save_func
|
||||
self.repo = repo
|
||||
self.update_state()
|
||||
self.fission: Fission
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer():
|
||||
yield Label(self._get_progress_text(), id="progress")
|
||||
yield ScrollableContainer(id="puzzle-container")
|
||||
yield Footer()
|
||||
|
||||
def update_state(self):
|
||||
"""更新状态机"""
|
||||
self.procession: Procession = self.phaser.current_procession() # type: ignore
|
||||
self.atom: pt.Atom = self.procession.current_atom # type: ignore
|
||||
|
||||
|
||||
def on_mount(self):
|
||||
self.fission = self.procession.get_fission()
|
||||
self.mount_puzzle()
|
||||
self.update_display()
|
||||
|
||||
def puzzle_widget(self):
|
||||
try:
|
||||
puzzle = self.fission.get_current_puzzle_inf()
|
||||
return shim.puzzle2widget[puzzle["puzzle"]]( # type: ignore
|
||||
atom=self.atom, alia=puzzle["alia"] # type: ignore
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(f"调度展开出错: {e}")
|
||||
return Static(f"无法生成谜题 {e}")
|
||||
|
||||
def _get_progress_text(self):
|
||||
s = f"阶段: {self.procession.phase.name}\n"
|
||||
# 收藏状态
|
||||
if self.repo is not None:
|
||||
fav_status = "已收藏" if self._is_current_atom_favorited() else "未收藏"
|
||||
s += f"收藏: {fav_status}\n"
|
||||
'''if config_var.get().get("debug_topline", 0):
|
||||
try:
|
||||
alia = self.fission.get_current_puzzle_inf()["alia"] # type: ignore
|
||||
s += f"谜题: {alia}\n"
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
stat = self.phaser.__repr__("simple", "")
|
||||
s += f"{stat}\n"
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
stat = self.procession.__repr__("simple", "")
|
||||
s += f"{stat}\n"
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
stat = self.fission.__repr__("simple", "")
|
||||
s += f"{stat}\n"
|
||||
except Exception as e:
|
||||
s = str(e)'''
|
||||
s += f"进度: {self.procession.process() + 1}/{self.procession.total_length()}"
|
||||
return s
|
||||
|
||||
def update_display(self):
|
||||
"""更新进度显示"""
|
||||
progress_widget = self.query_one("#progress")
|
||||
progress_widget.update(self._get_progress_text()) # type: ignore
|
||||
|
||||
def mount_puzzle(self):
|
||||
"""挂载当前谜题组件"""
|
||||
if self.procession.phase == PhaserState.FINISHED:
|
||||
self.mount_finished_widget()
|
||||
return
|
||||
container = self.query_one("#puzzle-container")
|
||||
for i in container.children:
|
||||
i.remove()
|
||||
container.mount(self.puzzle_widget())
|
||||
|
||||
def mount_finished_widget(self):
|
||||
"""挂载已完成组件"""
|
||||
container = self.query_one("#puzzle-container")
|
||||
for i in container.children:
|
||||
i.remove()
|
||||
from heurams.interface.widgets.finished import Finished
|
||||
|
||||
if config_var.get()['interface']['global']["persist_to_file"]:
|
||||
self.save_func()
|
||||
container.mount(Finished(is_saved=['interface']['global']["persist_to_file"]))
|
||||
|
||||
def on_button_pressed(self, event):
|
||||
event.stop()
|
||||
|
||||
def action_play_voice(self):
|
||||
self.run_worker(self.play_voice, exclusive=True, thread=True)
|
||||
|
||||
def play_voice(self):
|
||||
"""朗读当前内容"""
|
||||
from pathlib import Path
|
||||
|
||||
from heurams.services.audio_service import play_by_path
|
||||
from heurams.services.hasher import get_md5
|
||||
|
||||
path = Path(config_var.get()['global']["paths"]["data"]) / "cache" / "voice"
|
||||
path = path / f"{get_md5(self.atom.registry['nucleon']["tts_text"])}.wav"
|
||||
if path.exists():
|
||||
play_by_path(path)
|
||||
else:
|
||||
from heurams.services.tts_service import convertor
|
||||
|
||||
convertor(self.atom.registry["nucleon"]["tts_text"], path)
|
||||
play_by_path(path)
|
||||
|
||||
def watch_rating(self, old_rating, new_rating) -> None:
|
||||
if new_rating == -1: # 安全值
|
||||
return
|
||||
self.update_state()
|
||||
if self.procession.phase == PhaserState.FINISHED:
|
||||
rating = -1
|
||||
return
|
||||
self.fission.report(new_rating)
|
||||
self.forward(new_rating)
|
||||
self.rating = -1
|
||||
|
||||
|
||||
def forward(self, rating):
|
||||
self.update_state()
|
||||
allow_forward = 1 if rating >= 4 else 0
|
||||
if allow_forward:
|
||||
self.fission.forward()
|
||||
if self.fission.state == "retronly":
|
||||
self.forward_atom(self.fission.get_quality())
|
||||
self.update_state()
|
||||
self.mount_puzzle()
|
||||
self.update_display()
|
||||
|
||||
def atom_reporter(self, quality):
|
||||
if not self.atom.registry["runtime"]["locked"]:
|
||||
if not self.atom.registry["electron"].is_activated():
|
||||
self.atom.registry["electron"].activate()
|
||||
logger.debug(f"激活原子 {self.atom}")
|
||||
self.atom.lock(1)
|
||||
self.atom.minimize(5)
|
||||
else:
|
||||
self.atom.minimize(quality)
|
||||
else:
|
||||
pass
|
||||
|
||||
def forward_atom(self, quality):
|
||||
logger.debug(f"Quality: {quality}")
|
||||
self.atom_reporter(quality)
|
||||
if quality <= 3:
|
||||
self.procession.append()
|
||||
self.update_state() # 刷新状态
|
||||
self.procession.forward(1)
|
||||
self.update_state() # 刷新状态
|
||||
self.fission = self.procession.get_fission()
|
||||
|
||||
def action_go_back(self):
|
||||
self.app.pop_screen()
|
||||
|
||||
def action_quick_pass(self):
|
||||
self.rating = 5
|
||||
|
||||
def action_quick_fail(self):
|
||||
self.rating = 3
|
||||
|
||||
def _get_repo_rel_path(self) -> str:
|
||||
"""获取仓库相对路径(相对于 data/repo)"""
|
||||
if self.repo is None:
|
||||
return ""
|
||||
# self.repo.source 是 Path 对象,指向仓库目录
|
||||
repo_full_path = self.repo.source
|
||||
data_repo_path = Path(config_var.get()['global']["paths"]["data"]) / "repo"
|
||||
try:
|
||||
rel_path = repo_full_path.relative_to(data_repo_path)
|
||||
return str(rel_path)
|
||||
except ValueError:
|
||||
# 如果不在 data/repo 下,则返回完整路径(字符串形式)
|
||||
return str(repo_full_path)
|
||||
|
||||
def _is_current_atom_favorited(self) -> bool:
|
||||
"""检查当前原子是否已收藏"""
|
||||
if self.repo is None:
|
||||
return False
|
||||
repo_path = self._get_repo_rel_path()
|
||||
return favorite_manager.has(repo_path, self.atom.ident)
|
||||
|
||||
def action_toggle_favorite(self):
|
||||
"""切换收藏状态"""
|
||||
if self.repo is None:
|
||||
self.app.notify("无法收藏:未关联仓库", severity="error")
|
||||
return
|
||||
repo_path = self._get_repo_rel_path()
|
||||
ident = self.atom.ident
|
||||
if favorite_manager.has(repo_path, ident):
|
||||
favorite_manager.remove(repo_path, ident)
|
||||
self.app.notify(f"已取消收藏:{ident}", severity="information")
|
||||
else:
|
||||
favorite_manager.add(repo_path, ident)
|
||||
self.app.notify(f"已收藏:{ident}", severity="information")
|
||||
# 更新显示(如果需要)
|
||||
self.update_display()
|
||||
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python3
|
||||
from enum import Enum, auto
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Center, ScrollableContainer
|
||||
from textual.reactive import reactive
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button, Footer, Header, Label, Static
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.reactor import *
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .. import shim
|
||||
|
||||
|
||||
class AtomState(Enum):
|
||||
FAILED = auto()
|
||||
NORMAL = auto()
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class MemScreen(Screen):
|
||||
BINDINGS = [
|
||||
("q", "pop_screen", "返回"),
|
||||
# ("p", "prev", "复习上一个"),
|
||||
("d", "toggle_dark", ""),
|
||||
("v", "play_voice", "朗读"),
|
||||
("0,1,2,3", "app.push_screen('about')", ""),
|
||||
]
|
||||
|
||||
if config_var.get()["quick_pass"]:
|
||||
BINDINGS.append(("k", "quick_pass", "跳过"))
|
||||
rating = reactive(-1)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
atoms: list,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
self.atoms = atoms
|
||||
for i in self.atoms:
|
||||
i.do_eval()
|
||||
self.phaser = Phaser(atoms)
|
||||
# logger.debug(self.phaser.state)
|
||||
self.procession: Procession = self.phaser.current_procession() # type: ignore
|
||||
self.atom: pt.Atom = self.procession.current_atom
|
||||
# logger.debug(self.phaser.state)
|
||||
# self.procession.forward(1)
|
||||
|
||||
def on_mount(self):
|
||||
self.load_puzzle()
|
||||
pass
|
||||
|
||||
def puzzle_widget(self):
|
||||
try:
|
||||
logger.debug(self.phaser.state)
|
||||
logger.debug(self.procession.cursor)
|
||||
logger.debug(self.atom)
|
||||
self.fission = Fission(self.atom, self.phaser.state)
|
||||
puzzle_debug = next(self.fission.generate())
|
||||
# logger.debug(puzzle_debug)
|
||||
return shim.puzzle2widget[puzzle_debug["puzzle"]](
|
||||
atom=self.atom, alia=puzzle_debug["alia"]
|
||||
)
|
||||
except (KeyError, StopIteration, AttributeError) as e:
|
||||
logger.debug(f"调度展开出错: {e}")
|
||||
return Static("无法生成谜题")
|
||||
# logger.debug(shim.puzzle2widget[puzzle_debug["puzzle"]])
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer():
|
||||
yield Label(self._get_progress_text(), id="progress")
|
||||
|
||||
# self.mount(self.current_widget()) # type: ignore
|
||||
yield ScrollableContainer(id="puzzle-container")
|
||||
# yield Button("重新学习此单元", id="re-recognize", variant="warning")
|
||||
yield Footer()
|
||||
|
||||
def _get_progress_text(self):
|
||||
return f"当前进度: {self.procession.process() + 1}/{self.procession.total_length()}"
|
||||
|
||||
def update_display(self):
|
||||
progress_widget = self.query_one("#progress")
|
||||
progress_widget.update(self._get_progress_text()) # type: ignore
|
||||
|
||||
def load_puzzle(self):
|
||||
self.atom: pt.Atom = self.procession.current_atom
|
||||
container = self.query_one("#puzzle-container")
|
||||
for i in container.children:
|
||||
i.remove()
|
||||
container.mount(self.puzzle_widget())
|
||||
|
||||
def load_finished_widget(self):
|
||||
container = self.query_one("#puzzle-container")
|
||||
for i in container.children:
|
||||
i.remove()
|
||||
from heurams.interface.widgets.finished import Finished
|
||||
|
||||
container.mount(Finished())
|
||||
|
||||
def on_button_pressed(self, event):
|
||||
event.stop()
|
||||
|
||||
def watch_rating(self, old_rating, new_rating) -> None:
|
||||
if self.procession == 0:
|
||||
return
|
||||
if new_rating == -1:
|
||||
return
|
||||
forwards = 1 if new_rating >= 4 else 0
|
||||
self.rating = -1
|
||||
logger.debug(f"试图前进: {"允许" if forwards else "禁止"}")
|
||||
if forwards:
|
||||
ret = self.procession.forward(1)
|
||||
if ret == 0: # 若结束了此次队列
|
||||
self.procession = self.phaser.current_procession() # type: ignore
|
||||
if self.procession == 0: # 若所有队列都结束了
|
||||
logger.debug(f"记忆进程结束")
|
||||
for i in self.atoms:
|
||||
i: pt.Atom
|
||||
i.revise()
|
||||
i.persist("electron")
|
||||
self.load_finished_widget()
|
||||
return
|
||||
else:
|
||||
logger.debug(f"建立新队列 {self.procession.phase}")
|
||||
self.load_puzzle()
|
||||
else: # 若不通过
|
||||
self.procession.append()
|
||||
self.update_display()
|
||||
|
||||
def action_quick_pass(self):
|
||||
self.rating = 5
|
||||
self.atom.minimize(5)
|
||||
self.atom.registry["electron"].activate()
|
||||
self.atom.lock(1)
|
||||
|
||||
def action_play_voice(self):
|
||||
self.run_worker(self.play_voice, exclusive=True, thread=True)
|
||||
|
||||
def play_voice(self):
|
||||
"""朗读当前内容"""
|
||||
from pathlib import Path
|
||||
|
||||
from heurams.services.audio_service import play_by_path
|
||||
from heurams.services.hasher import get_md5
|
||||
|
||||
path = Path(config_var.get()["paths"]["cache_dir"])
|
||||
path = (
|
||||
path
|
||||
/ f"{get_md5(self.atom.registry['nucleon'].metadata["formation"]["tts_text"])}.wav"
|
||||
)
|
||||
if path.exists():
|
||||
play_by_path(path)
|
||||
else:
|
||||
from heurams.services.tts_service import convertor
|
||||
|
||||
convertor(
|
||||
self.atom.registry["nucleon"].metadata["formation"]["tts_text"], path
|
||||
)
|
||||
play_by_path(path)
|
||||
|
||||
def action_toggle_dark(self):
|
||||
self.app.action_toggle_dark()
|
||||
|
||||
def action_pop_screen(self):
|
||||
self.app.pop_screen()
|
||||
@@ -1,92 +0,0 @@
|
||||
import webbrowser
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Grid, ScrollableContainer
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Button, Footer, Header, Label, ListItem, ListView, Static
|
||||
|
||||
from heurams.context import *
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .favmgr import FavoriteManagerScreen
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class NavigatorScreen(ModalScreen):
|
||||
"""导航器模态窗口"""
|
||||
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
("escape", "go_back", "返回"),
|
||||
("n", "go_back", "切换"),
|
||||
]
|
||||
|
||||
SCREENS = [
|
||||
("仪表盘", "dashboard"),
|
||||
# ("创建仓库", "repo_creator"),
|
||||
("缓存管理器", "precache_all"),
|
||||
("收藏夹", FavoriteManagerScreen),
|
||||
("设置页面", "setting"),
|
||||
# ("调试日志", "logviewer"),
|
||||
("同步工具", "synctool"),
|
||||
("关于此软件", "about"),
|
||||
# ("仓库编辑器", "repo_editor"),
|
||||
]
|
||||
|
||||
OTHERS = [
|
||||
("退出程序", "self.app.exit()"),
|
||||
("项目主页", "webbrowser.open('https://ams.imwangzhiyu.xyz')"),
|
||||
]
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""组合界面组件"""
|
||||
with Grid(id="dialog"):
|
||||
yield Label(
|
||||
"[b]请选择要跳转的功能\n或记忆会话实例[/b]\n\n将在此处显示提示",
|
||||
classes="title-label",
|
||||
)
|
||||
yield ListView(
|
||||
*[ListItem(Label(title)) for title, _ in (self.SCREENS + self.OTHERS)],
|
||||
id="nav-list",
|
||||
classes="nav-list-view",
|
||||
)
|
||||
yield Static("按下回车以完成切换\n所有会话将被保存")
|
||||
yield Button(
|
||||
"关闭 (n)", id="close_button", variant="primary", classes="close-button", flat=True
|
||||
)
|
||||
|
||||
def on_mount(self) -> None:
|
||||
# 设置焦点到列表
|
||||
nav_list = self.query_one("#nav-list", ListView)
|
||||
nav_list.focus()
|
||||
|
||||
def on_list_view_selected(self, event) -> None:
|
||||
if not isinstance(event.item, ListItem):
|
||||
return
|
||||
selected_label = event.item.query_one(Label)
|
||||
label_text = str(selected_label.render())
|
||||
# 查找对应的屏幕标识
|
||||
for title, screen_id in self.SCREENS:
|
||||
if title == label_text:
|
||||
self.app.pop_screen()
|
||||
# 跳转到目标屏幕
|
||||
if isinstance(screen_id, str):
|
||||
# 已注册的字符串标识符
|
||||
self.app.push_screen(screen_id)
|
||||
else:
|
||||
self.app.push_screen(screen_id())
|
||||
return
|
||||
for title, cmd in self.OTHERS:
|
||||
if title == label_text:
|
||||
exec(cmd)
|
||||
return
|
||||
return
|
||||
|
||||
def on_button_pressed(self, event) -> None:
|
||||
event.stop()
|
||||
if event.button.id == "close_button":
|
||||
self.action_go_back()
|
||||
|
||||
def action_go_back(self) -> None:
|
||||
self.app.pop_screen()
|
||||
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
import toml
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import (Button, Footer, Header, Input, Label, Markdown,
|
||||
Select)
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.services.version import ver
|
||||
|
||||
|
||||
class NucleonCreatorScreen(Screen):
|
||||
BINDINGS = [("q", "go_back", "返回")]
|
||||
SUB_TITLE = "单元集创建向导"
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(name=None, id=None, classes=None)
|
||||
|
||||
def search_templates(self):
|
||||
from pathlib import Path
|
||||
|
||||
from heurams.context import config_var
|
||||
|
||||
template_dir = Path(config_var.get()["paths"]["template_dir"])
|
||||
templates = list()
|
||||
for i in template_dir.iterdir():
|
||||
if i.name.endswith(".toml"):
|
||||
try:
|
||||
import toml
|
||||
|
||||
with open(i, "r") as f:
|
||||
dic = toml.load(f)
|
||||
desc = dic["__metadata__.attribution"]["desc"]
|
||||
templates.append(desc + " (" + i.name + ")")
|
||||
except Exception as e:
|
||||
templates.append(f"无描述模板 ({i.name})")
|
||||
print(e)
|
||||
return templates
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer(id="vice_container"):
|
||||
yield Label(f"[b]空白单元集创建向导\n")
|
||||
yield Markdown(
|
||||
"> 提示: 你可能注意到当选中文本框时底栏和操作按键绑定将被覆盖 \n只需选中(使用鼠标或 Tab)选择框即可恢复底栏功能"
|
||||
)
|
||||
yield Markdown("1. 键入单元集名称")
|
||||
yield Input(placeholder="单元集名称", id="name_input")
|
||||
yield Markdown(
|
||||
"> 单元集名称不应与现有单元集重复. \n> 新的单元集文件将创建在 ./nucleon/你输入的名称.toml"
|
||||
)
|
||||
yield Label(f"\n")
|
||||
yield Markdown("2. 选择单元集模板")
|
||||
LINES = self.search_templates()
|
||||
"""带有宏支持的空白单元集 ({ver})
|
||||
古诗词模板单元集 ({ver})
|
||||
英语词汇和短语模板单元集 ({ver})
|
||||
"""
|
||||
yield Select.from_values(LINES, prompt="选择类型", id="template_select")
|
||||
yield Markdown("> 新单元集的版本号将和主程序版本保持同步")
|
||||
yield Label(f"\n")
|
||||
yield Markdown("3. 输入常见附加元数据 (可选)")
|
||||
yield Input(placeholder="作者", id="author_input")
|
||||
yield Input(placeholder="内容描述", id="desc_input")
|
||||
yield Button(
|
||||
"新建空白单元集",
|
||||
id="submit_button",
|
||||
variant="primary",
|
||||
classes="start-button",
|
||||
)
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self):
|
||||
self.query_one("#submit_button").focus()
|
||||
|
||||
def action_go_back(self):
|
||||
self.app.pop_screen()
|
||||
|
||||
def action_quit_app(self):
|
||||
self.app.exit()
|
||||
|
||||
def on_button_pressed(self, event) -> None:
|
||||
event.stop()
|
||||
if event.button.id == "submit_button":
|
||||
# 获取输入值
|
||||
name_input = self.query_one("#name_input")
|
||||
template_select = self.query_one("#template_select")
|
||||
author_input = self.query_one("#author_input")
|
||||
desc_input = self.query_one("#desc_input")
|
||||
|
||||
name = name_input.value.strip() # type: ignore
|
||||
author = author_input.value.strip() # type: ignore
|
||||
desc = desc_input.value.strip() # type: ignore
|
||||
selected = template_select.value # type: ignore
|
||||
|
||||
# 验证
|
||||
if not name:
|
||||
self.notify("单元集名称不能为空", severity="error")
|
||||
return
|
||||
|
||||
# 获取配置路径
|
||||
config = config_var.get()
|
||||
nucleon_dir = Path(config["paths"]["nucleon_dir"])
|
||||
template_dir = Path(config["paths"]["template_dir"])
|
||||
|
||||
# 检查文件是否已存在
|
||||
nucleon_path = nucleon_dir / f"{name}.toml"
|
||||
if nucleon_path.exists():
|
||||
self.notify(f"单元集 '{name}' 已存在", severity="error")
|
||||
return
|
||||
|
||||
# 确定模板文件
|
||||
if selected is None:
|
||||
self.notify("请选择一个模板", severity="error")
|
||||
return
|
||||
# selected 是描述字符串, 格式如 "描述 (filename.toml)"
|
||||
# 提取文件名
|
||||
import re
|
||||
|
||||
match = re.search(r"\(([^)]+)\)$", selected)
|
||||
if not match:
|
||||
self.notify("模板选择格式无效", severity="error")
|
||||
return
|
||||
template_filename = match.group(1)
|
||||
template_path = template_dir / template_filename
|
||||
if not template_path.exists():
|
||||
self.notify(f"模板文件不存在: {template_filename}", severity="error")
|
||||
return
|
||||
|
||||
# 加载模板
|
||||
try:
|
||||
with open(template_path, "r", encoding="utf-8") as f:
|
||||
template_data = toml.load(f)
|
||||
except Exception as e:
|
||||
self.notify(f"加载模板失败: {e}", severity="error")
|
||||
return
|
||||
|
||||
# 更新元数据
|
||||
metadata = template_data.get("__metadata__", {})
|
||||
attribution = metadata.get("attribution", {})
|
||||
if author:
|
||||
attribution["author"] = author
|
||||
if desc:
|
||||
attribution["desc"] = desc
|
||||
attribution["name"] = name
|
||||
# 可选: 设置版本
|
||||
attribution["version"] = ver
|
||||
metadata["attribution"] = attribution
|
||||
template_data["__metadata__"] = metadata
|
||||
|
||||
# 确保 nucleon_dir 存在
|
||||
nucleon_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 写入新文件
|
||||
try:
|
||||
with open(nucleon_path, "w", encoding="utf-8") as f:
|
||||
toml.dump(template_data, f)
|
||||
except Exception as e:
|
||||
self.notify(f"保存单元集失败: {e}", severity="error")
|
||||
return
|
||||
|
||||
self.notify(f"单元集 '{name}' 创建成功")
|
||||
self.app.pop_screen()
|
||||
@@ -1,9 +1,8 @@
|
||||
"""缓存工具界面"""
|
||||
|
||||
#!/usr/bin/env python3
|
||||
import pathlib
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Horizontal, ScrollableContainer, Container
|
||||
from textual.containers import Horizontal, ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button, Footer, Header, Label, ProgressBar, Static
|
||||
from textual.worker import get_current_worker
|
||||
@@ -12,19 +11,6 @@ import heurams.kernel.particles as pt
|
||||
import heurams.services.hasher as hasher
|
||||
from heurams.context import *
|
||||
|
||||
# 兼容性缓存路径:优先使用 paths.cache,否则使用 data/cache
|
||||
paths = config_var.get()['global']["paths"]
|
||||
cache_dir = pathlib.Path(paths.get("cache", paths["data"] + "/cache")) / "voice"
|
||||
|
||||
|
||||
def format_size(bytes_num: int) -> str:
|
||||
"""将字节数格式化为人类可读的字符串"""
|
||||
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
||||
if bytes_num < 1024.0:
|
||||
return f"{bytes_num:.2f} {unit}"
|
||||
bytes_num /= 1024.0 # type: ignore
|
||||
return f"{bytes_num:.2f} PB"
|
||||
|
||||
|
||||
class PrecachingScreen(Screen):
|
||||
"""预缓存音频文件屏幕
|
||||
@@ -37,9 +23,7 @@ class PrecachingScreen(Screen):
|
||||
"""
|
||||
|
||||
SUB_TITLE = "缓存管理器"
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
]
|
||||
BINDINGS = [("q", "go_back", "返回")]
|
||||
|
||||
def __init__(self, nucleons: list = [], desc: str = ""):
|
||||
super().__init__(name=None, id=None, classes=None)
|
||||
@@ -53,56 +37,18 @@ class PrecachingScreen(Screen):
|
||||
self.precache_worker = None
|
||||
self.cancel_flag = 0
|
||||
self.desc = desc
|
||||
# 不再需要缓存配置,保留配置读取以兼容
|
||||
self.cache_stats = {"total_size": 0, "file_count": 0, "human_size": "0 B", "cached_units": 0, "total_units": 0, "cache_rate": 0}
|
||||
self._update_cache_stats()
|
||||
|
||||
def _get_total_units(self) -> int:
|
||||
"""获取所有仓库的总单元数"""
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.repolib import Repo
|
||||
repo_path = pathlib.Path(config_var.get()['global']["paths"]["data"]) / "repo"
|
||||
repo_dirs = Repo.probe_valid_repos_in_dir(repo_path)
|
||||
repos = map(Repo.create_from_repodir, repo_dirs)
|
||||
total = 0
|
||||
for repo in repos:
|
||||
try:
|
||||
total += len(repo.ident_index)
|
||||
except:
|
||||
continue
|
||||
return total
|
||||
|
||||
def _update_cache_stats(self) -> None:
|
||||
"""更新缓存统计信息"""
|
||||
total_size = 0
|
||||
file_count = 0
|
||||
cached_units = 0
|
||||
if cache_dir.exists():
|
||||
for file in cache_dir.rglob("*"):
|
||||
if file.is_file():
|
||||
total_size += file.stat().st_size
|
||||
file_count += 1
|
||||
if file.suffix.lower() == ".wav":
|
||||
cached_units += 1
|
||||
total_units = self._get_total_units()
|
||||
cache_rate = (cached_units / total_units * 100) if total_units > 0 else 0
|
||||
|
||||
self.cache_stats["total_size"] = total_size
|
||||
self.cache_stats["file_count"] = file_count
|
||||
self.cache_stats["human_size"] = format_size(total_size)
|
||||
self.cache_stats["cached_units"] = cached_units
|
||||
self.cache_stats["total_units"] = total_units
|
||||
self.cache_stats["cache_rate"] = cache_rate
|
||||
for i in nucleons:
|
||||
i: pt.Nucleon
|
||||
atom = pt.Atom()
|
||||
atom.link("nucleon", i)
|
||||
atom.do_eval()
|
||||
# print("完成 EVAL")
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer(id="precache_container"):
|
||||
yield Label("[b]音频预缓存[/b]", classes="title-label")
|
||||
with Container():
|
||||
yield Static(
|
||||
f"缓存率: {self.cache_stats.get('cache_rate', 0):.1f}% (已缓存 {self.cache_stats.get('cached_units', 0)} / {self.cache_stats.get('total_units', 0)} 个单元)",
|
||||
classes="cache-usage-text"
|
||||
)
|
||||
|
||||
if self.nucleons:
|
||||
yield Static(f"目标单元归属: [b]{self.desc}[/b]", classes="target-info")
|
||||
yield Static(f"单元数量: {len(self.nucleons)}", classes="target-info")
|
||||
@@ -112,6 +58,7 @@ class PrecachingScreen(Screen):
|
||||
yield Static(id="status", classes="status-info")
|
||||
yield Static(id="current_item", classes="current-item")
|
||||
yield ProgressBar(total=100, show_eta=False, id="progress_bar")
|
||||
|
||||
with Horizontal(classes="button-group"):
|
||||
if not self.is_precaching:
|
||||
yield Button("开始预缓存", id="start_precache", variant="primary")
|
||||
@@ -119,11 +66,7 @@ class PrecachingScreen(Screen):
|
||||
yield Button("取消预缓存", id="cancel_precache", variant="error")
|
||||
yield Button("清空缓存", id="clear_cache", variant="warning")
|
||||
yield Button("返回", id="go_back", variant="default")
|
||||
with Container(classes="cache-info"):
|
||||
yield Static(f"缓存路径: {cache_dir}", classes="cache-path")
|
||||
yield Static(f"文件数: {self.cache_stats['file_count']}", classes="cache-count")
|
||||
yield Static(f"总大小: {self.cache_stats['human_size']}", classes="cache-size")
|
||||
yield Button("刷新", id="refresh_cache_stats", variant="default", flat=True)
|
||||
|
||||
yield Static("若您离开此界面, 未完成的缓存进程会自动停止.")
|
||||
yield Static('缓存程序支持 "断点续传".')
|
||||
|
||||
@@ -132,7 +75,6 @@ class PrecachingScreen(Screen):
|
||||
def on_mount(self):
|
||||
"""挂载时初始化状态"""
|
||||
self.update_status("就绪", "等待开始...")
|
||||
self._update_cache_display()
|
||||
|
||||
def update_status(self, status, current_item="", progress=None):
|
||||
"""更新状态显示"""
|
||||
@@ -147,29 +89,11 @@ class PrecachingScreen(Screen):
|
||||
progress_bar.progress = progress
|
||||
progress_bar.advance(0) # 刷新显示
|
||||
|
||||
def _update_cache_display(self) -> None:
|
||||
"""更新缓存信息显示"""
|
||||
# 更新统计信息
|
||||
self._update_cache_stats()
|
||||
# 更新缓存率进度条
|
||||
# 更新缓存大小和文件数显示
|
||||
cache_count_widget = self.query_one(".cache-count", Static)
|
||||
cache_size_widget = self.query_one(".cache-size", Static)
|
||||
cache_usage_text = self.query_one(".cache-usage-text", Static)
|
||||
if cache_count_widget:
|
||||
cache_count_widget.update(f"文件数: {self.cache_stats['file_count']}")
|
||||
if cache_size_widget:
|
||||
cache_size_widget.update(f"总大小: {self.cache_stats['human_size']}")
|
||||
if cache_usage_text:
|
||||
cache_usage_text.update(
|
||||
f"缓存率: {self.cache_stats.get('cache_rate', 0):.1f}% "
|
||||
f"(已缓存 {self.cache_stats.get('cached_units', 0)} / {self.cache_stats.get('total_units', 0)} 个单元)"
|
||||
)
|
||||
|
||||
def precache_by_text(self, text: str):
|
||||
"""预缓存单段文本的音频"""
|
||||
from heurams.context import config_var, rootdir, workdir
|
||||
|
||||
cache_dir = pathlib.Path(config_var.get()["paths"]["cache_dir"])
|
||||
cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
cache_file = cache_dir / f"{hasher.get_md5(text)}.wav"
|
||||
if not cache_file.exists():
|
||||
@@ -185,8 +109,10 @@ class PrecachingScreen(Screen):
|
||||
|
||||
def precache_by_nucleon(self, nucleon: pt.Nucleon):
|
||||
"""依据 Nucleon 缓存"""
|
||||
ret = self.precache_by_text(nucleon["tts_text"])
|
||||
# print(nucleon.metadata['formation']['tts_text'])
|
||||
ret = self.precache_by_text(nucleon.metadata["formation"]["tts_text"])
|
||||
return ret
|
||||
# print(f"TTS 缓存: {nucleon.metadata['formation']['tts_text']}")
|
||||
|
||||
def precache_by_list(self, nucleons: list):
|
||||
"""依据 Nucleons 列表缓存"""
|
||||
@@ -195,7 +121,7 @@ class PrecachingScreen(Screen):
|
||||
worker = get_current_worker()
|
||||
if worker and worker.is_cancelled: # 函数在worker中执行且已被取消
|
||||
return False
|
||||
text = nucleon["tts_text"]
|
||||
text = nucleon.metadata["formation"]["tts_text"]
|
||||
# self.current_item = text[:30] + "..." if len(text) > 50 else text
|
||||
# print(text)
|
||||
self.processed += 1
|
||||
@@ -225,30 +151,38 @@ class PrecachingScreen(Screen):
|
||||
# print(f"返回 {ret}")
|
||||
return ret
|
||||
|
||||
def precache_by_filepath(self, path: pathlib.Path):
|
||||
"""预缓存单个文件的所有内容"""
|
||||
lst = list()
|
||||
for i in pt.load_nucleon(path):
|
||||
lst.append(i[0])
|
||||
return self.precache_by_list(lst)
|
||||
|
||||
def precache_all_files(self):
|
||||
"""预缓存所有文件"""
|
||||
from heurams.context import config_var, rootdir, workdir
|
||||
from heurams.kernel.repolib import Repo
|
||||
|
||||
repo_path = pathlib.Path(config_var.get()['global']["paths"]["data"]) / "repo"
|
||||
repo_dirs = Repo.probe_valid_repos_in_dir(repo_path)
|
||||
repos = map(Repo.create_from_repodir, repo_dirs)
|
||||
nucleon_path = pathlib.Path(config_var.get()["paths"]["nucleon_dir"])
|
||||
nucleon_files = [
|
||||
f for f in nucleon_path.iterdir() if f.suffix == ".toml"
|
||||
] # TODO: 解耦合
|
||||
|
||||
# 计算总项目数
|
||||
self.total = 0
|
||||
nucleon_list = list()
|
||||
for repo in repos:
|
||||
nu = list()
|
||||
for file in nucleon_files:
|
||||
try:
|
||||
for i in repo.ident_index:
|
||||
nucleon_list.append(
|
||||
pt.Nucleon.create_on_nucleonic_data(
|
||||
repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
)
|
||||
for i in pt.load_nucleon(file):
|
||||
nu.append(i[0])
|
||||
except:
|
||||
continue
|
||||
self.total = len(nucleon_list)
|
||||
return self.precache_by_list(nucleon_list)
|
||||
self.total = len(nu)
|
||||
for i in nu:
|
||||
i: pt.Nucleon
|
||||
atom = pt.Atom()
|
||||
atom.link("nucleon", i)
|
||||
atom.do_eval()
|
||||
return self.precache_by_list(nu)
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
event.stop()
|
||||
@@ -285,19 +219,16 @@ class PrecachingScreen(Screen):
|
||||
|
||||
from heurams.context import config_var, rootdir, workdir
|
||||
|
||||
shutil.rmtree(cache_dir, ignore_errors=True)
|
||||
shutil.rmtree(
|
||||
f"{config_var.get()["paths"]["cache_dir"]}", ignore_errors=True
|
||||
)
|
||||
self.update_status("已清空", "音频缓存已清空", 0)
|
||||
self._update_cache_display() # 更新缓存统计显示
|
||||
except Exception as e:
|
||||
self.update_status("错误", f"清空缓存失败: {e}")
|
||||
self.cancel_flag = 1
|
||||
self.processed = 0
|
||||
self.progress = 0
|
||||
|
||||
elif event.button.id == "refresh_cache_stats":
|
||||
# 刷新缓存统计信息
|
||||
self._update_cache_display()
|
||||
self.app.notify("缓存信息已刷新", severity="information")
|
||||
elif event.button.id == "go_back":
|
||||
self.action_go_back()
|
||||
|
||||
@@ -305,3 +236,8 @@ class PrecachingScreen(Screen):
|
||||
if self.is_precaching and self.precache_worker:
|
||||
self.precache_worker.cancel()
|
||||
self.app.pop_screen()
|
||||
|
||||
def action_quit_app(self):
|
||||
if self.is_precaching and self.precache_worker:
|
||||
self.precache_worker.cancel()
|
||||
self.app.exit()
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
"""记忆准备界面"""
|
||||
|
||||
#!/usr/bin/env python3
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.reactive import reactive
|
||||
from textual.screen import Screen
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Button, Footer, Header, Label, Markdown, Static, Rule, Sparkline
|
||||
from textual.widgets import Button, Footer, Header, Label, Markdown, Static
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.services.hasher as hasher
|
||||
from heurams.context import *
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.repolib import *
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -28,23 +26,27 @@ class PreparationScreen(Screen):
|
||||
("0,1,2,3", "app.push_screen('about')", ""),
|
||||
]
|
||||
|
||||
scheduled_num = reactive(config_var.get()['interface']['global']["scheduled_num"])
|
||||
scheduled_num = reactive(config_var.get()["scheduled_num"])
|
||||
|
||||
def __init__(self, repo: Repo, repostat: dict) -> None:
|
||||
def __init__(self, nucleon_file: pathlib.Path, electron_file: pathlib.Path) -> None:
|
||||
super().__init__(name=None, id=None, classes=None)
|
||||
self.repo = repo
|
||||
self.repostat = repostat
|
||||
self.load_data()
|
||||
self.nucleon_file = nucleon_file
|
||||
self.electron_file = electron_file
|
||||
self.nucleons_with_orbital = pt.load_nucleon(self.nucleon_file)
|
||||
self.electrons = pt.load_electron(self.electron_file)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer(id="vice_container"):
|
||||
yield Label(f"准备就绪: [b]{self.repostat['title']}[/b]\n")
|
||||
yield Label(f"准备就绪: [b]{self.nucleon_file.stem}[/b]\n")
|
||||
yield Label(
|
||||
f"仓库路径: {config_var.get()['global']['paths']['data']}/repo/[b]{self.repostat['dirname']}[/b]"
|
||||
f"内容源文件: {config_var.get()['paths']['nucleon_dir']}/[b]{self.nucleon_file.name}[/b]"
|
||||
)
|
||||
yield Label(f"\n单元数量: {len(self.repo)}\n")
|
||||
yield Label(f"最小记忆分组: {self.scheduled_num}\n", id="schnum_label")
|
||||
yield Label(
|
||||
f"元数据文件: {config_var.get()['paths']['electron_dir']}/[b]{self.electron_file.name}[/b]"
|
||||
)
|
||||
yield Label(f"\n单元数量: {len(self.nucleons_with_orbital)}\n")
|
||||
yield Label(f"单次记忆数量: {self.scheduled_num}", id="schnum_label")
|
||||
|
||||
yield Button(
|
||||
"开始记忆",
|
||||
@@ -59,13 +61,8 @@ class PreparationScreen(Screen):
|
||||
classes="precache-button",
|
||||
)
|
||||
|
||||
yield Static()
|
||||
yield Sparkline(self.spark_line_arr, summary_function=max)
|
||||
yield Rule()
|
||||
#yield Static(str(self.spark_line_arr))
|
||||
yield Static(f"单元状态预览:\n")
|
||||
for i in self.content.splitlines():
|
||||
yield Static(i, classes="full")
|
||||
yield Static(f"\n单元预览:\n")
|
||||
yield Markdown(self._get_full_content().replace("/", ""), classes="full")
|
||||
yield Footer()
|
||||
|
||||
# def watch_scheduled_num(self, old_scheduled_num, new_scheduled_num):
|
||||
@@ -76,27 +73,13 @@ class PreparationScreen(Screen):
|
||||
# except:
|
||||
# pass
|
||||
|
||||
def load_data(self):
|
||||
def _get_full_content(self):
|
||||
content = ""
|
||||
spark_line_arr = []
|
||||
for i in self.repo.ident_index:
|
||||
n = pt.Nucleon.create_on_nucleonic_data(
|
||||
nucleonic_data=self.repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
e = pt.Electron.create_on_electonic_data(electronic_data=self.repo.electronic_data_lict.get_itemic_unit(i))
|
||||
statstr = ""
|
||||
|
||||
if e.is_activated():
|
||||
statstr = '[#00ff00]A[/]'
|
||||
if e.is_due():
|
||||
statstr = '[#ffff00]R[/]'
|
||||
#statstr += ('[dim]' + str(e.rept(real_rept=True)).zfill(2)+'[/]')
|
||||
else:
|
||||
statstr = '[#ff0000]U[/]'
|
||||
spark_line_arr.append(e.rept(real_rept=True))
|
||||
content += f" {statstr} {n['content'].replace('/', '')} \n"
|
||||
self.content = content
|
||||
self.spark_line_arr = spark_line_arr
|
||||
for nucleon, orbital in self.nucleons_with_orbital:
|
||||
nucleon: pt.Nucleon
|
||||
# print(nucleon.payload)
|
||||
content += " - " + nucleon["content"] + " \n"
|
||||
return content
|
||||
|
||||
def action_go_back(self):
|
||||
self.app.pop_screen()
|
||||
@@ -105,15 +88,9 @@ class PreparationScreen(Screen):
|
||||
from ..screens.precache import PrecachingScreen
|
||||
|
||||
lst = list()
|
||||
for i in self.repo.ident_index:
|
||||
lst.append(
|
||||
pt.Nucleon.create_on_nucleonic_data(
|
||||
self.repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
)
|
||||
precache_screen = PrecachingScreen(
|
||||
nucleons=lst, desc=self.repo.manifest["title"]
|
||||
)
|
||||
for i in self.nucleons_with_orbital:
|
||||
lst.append(i[0])
|
||||
precache_screen = PrecachingScreen(lst)
|
||||
self.app.push_screen(precache_screen)
|
||||
|
||||
def action_quit_app(self):
|
||||
@@ -123,41 +100,39 @@ class PreparationScreen(Screen):
|
||||
event.stop()
|
||||
logger.debug("按下按钮")
|
||||
if event.button.id == "start_memorizing_button":
|
||||
launch(repo=self.repo, app=self.app, scheduled_num=self.scheduled_num)
|
||||
|
||||
elif event.button.id == "precache_button":
|
||||
self.action_precache()
|
||||
|
||||
def launch(repo, app, scheduled_num):
|
||||
if scheduled_num == -1:
|
||||
scheduled_num = config_var.get()['interface']['global']["scheduled_num"]
|
||||
atoms = list()
|
||||
for i in repo.ident_index:
|
||||
n = pt.Nucleon.create_on_nucleonic_data(
|
||||
nucleonic_data=repo.nucleonic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
e = pt.Electron.create_on_electonic_data(
|
||||
electronic_data=repo.electronic_data_lict.get_itemic_unit(i)
|
||||
)
|
||||
a = pt.Atom(n, e, repo.orbitic_data)
|
||||
atoms.append(a)
|
||||
|
||||
for nucleon, orbital in self.nucleons_with_orbital:
|
||||
atom = pt.Atom(nucleon.ident)
|
||||
atom.link("nucleon", nucleon)
|
||||
try:
|
||||
atom.link("electron", self.electrons[nucleon.ident])
|
||||
except KeyError:
|
||||
atom.link("electron", pt.Electron(nucleon.ident))
|
||||
atom.link("orbital", orbital)
|
||||
atom.link("nucleon_fmt", "toml")
|
||||
atom.link("electron_fmt", "json")
|
||||
atom.link("orbital_fmt", "toml")
|
||||
atom.link("nucleon_path", self.nucleon_file)
|
||||
atom.link("electron_path", self.electron_file)
|
||||
atom.link("orbital_path", None)
|
||||
atoms.append(atom)
|
||||
atoms_to_provide = list()
|
||||
left_new = scheduled_num
|
||||
left_new = self.scheduled_num
|
||||
for i in atoms:
|
||||
i: pt.Atom
|
||||
if i.registry["electron"].is_activated():
|
||||
if i.registry["electron"].is_due():
|
||||
atoms_to_provide.append(i)
|
||||
else:
|
||||
if i.registry["electron"].is_activated():
|
||||
pass
|
||||
else:
|
||||
left_new -= 1
|
||||
if left_new >= 0:
|
||||
atoms_to_provide.append(i)
|
||||
import heurams.kernel.reactor as rt
|
||||
logger.debug(f"ATP: {atoms_to_provide}")
|
||||
from .memorizor import MemScreen
|
||||
|
||||
from .memoqueue import MemScreen
|
||||
|
||||
pheser = rt.Phaser(atoms_to_provide)
|
||||
save_func = repo.persist_to_repodir
|
||||
memscreen = MemScreen(pheser, save_func, repo=repo)
|
||||
app.push_screen(memscreen)
|
||||
memscreen = MemScreen(atoms_to_provide)
|
||||
self.app.push_screen(memscreen)
|
||||
elif event.button.id == "precache_button":
|
||||
self.action_precache()
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
"""设置页面"""
|
||||
|
||||
from functools import reduce
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import ScrollableContainer, Container, Horizontal, Vertical
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button, Footer, Header, Label, ListItem, ListView, Static, Collapsible, Input, Switch, Select
|
||||
from textual.layouts import horizontal
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.services.timer as timer
|
||||
import heurams.services.version as version
|
||||
from heurams.context import *
|
||||
from heurams.kernel.particles import *
|
||||
from heurams.kernel.repolib import *
|
||||
from heurams.kernel.algorithms import algorithms
|
||||
from heurams.services.logger import get_logger
|
||||
from heurams.services.textproc import domize, undomize
|
||||
from heurams.services.epath import epath
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class SettingScreen(Screen):
|
||||
"""设置页面屏幕"""
|
||||
|
||||
SUB_TITLE = "设置"
|
||||
BINDINGS = [
|
||||
("q", "go_back", "返回"),
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(name, id, classes)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""组合界面组件"""
|
||||
yield Header(show_clock=True)
|
||||
with ScrollableContainer():
|
||||
yield Label('[b]设置页面[/b]')
|
||||
for i in config_var.get():
|
||||
if i.startswith('_'):
|
||||
continue
|
||||
a = self._get_subcfg(f'{i}')
|
||||
if a:
|
||||
yield Collapsible(*a, title=i + f'\n{config_var.get().get(f"_{i}_desc", "")}')
|
||||
yield Footer()
|
||||
|
||||
def _get_subcfg(self, parent_epath: str):
|
||||
parent = epath(config_var.get(), parent_epath)
|
||||
if isinstance(parent, ConfigDict):
|
||||
if parent.is_dir:
|
||||
lst = list()
|
||||
for i in parent:
|
||||
if i.startswith('_'):
|
||||
continue
|
||||
a = self._get_subcfg(f"{parent_epath}.{i}")
|
||||
if a:
|
||||
lst.append(Collapsible(*a, title=i + f'\n{parent.get(f"_{i}_desc", "")}'))
|
||||
return lst
|
||||
if isinstance(parent, dict) or (isinstance(parent, ConfigDict) and not parent.is_dir):
|
||||
lst = list()
|
||||
for i in parent:
|
||||
if i.startswith('_'):
|
||||
continue
|
||||
if isinstance(parent[i], dict):
|
||||
a = self._get_subcfg(f"{parent_epath}.{i}")
|
||||
if a:
|
||||
lst.append(Collapsible(*a, title=i + f'\n{parent.get(f"_{i}_desc", "")}'))
|
||||
elif f'_{i}_candidate' in parent: # 选择框模式
|
||||
if isinstance(parent[f'_{i}_candidate'], dict):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Select(((f"{j} ({k})", j) for j, k in parent[f'_{i}_candidate'].items()), prompt=f'{parent.get(f"{i}", "")}', id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'
|
||||
))
|
||||
elif isinstance(parent[f'_{i}_candidate'], list):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Select(((j, j) for j in parent[f'_{i}_candidate']), prompt=f'{parent.get(f"{i}", "")}', id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'
|
||||
))
|
||||
else:
|
||||
if isinstance(parent[i], float):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Input(value=str(parent[i]), placeholder='要求一个浮点数', type='number', id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'))
|
||||
elif isinstance(parent[i], str):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Input(value=parent[i], placeholder='要求一个字符串', type='text', id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'))
|
||||
elif isinstance(parent[i], bool):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Switch(value=parent[i], id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'))
|
||||
elif isinstance(parent[i], int):
|
||||
lst.append(Horizontal(
|
||||
Label(i + f'\n{parent.get(f"_{i}_desc", "")}'),
|
||||
Input(value=str(parent[i]), placeholder='要求一个整数', type='integer', id=domize(f"{parent_epath}.{i}")),
|
||||
classes='container'))
|
||||
elif isinstance(parent[i], list):
|
||||
pass
|
||||
else:
|
||||
lst.append(Label('未知类型'))
|
||||
return lst
|
||||
return [Label('无子项')]
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""挂载组件时初始化"""
|
||||
pass
|
||||
|
||||
def action_go_back(self) -> None:
|
||||
"""返回上一屏幕"""
|
||||
config_var.get().persist()
|
||||
self.app.pop_screen()
|
||||
|
||||
def action_quit_app(self) -> None:
|
||||
"""退出应用程序"""
|
||||
self.app.exit()
|
||||
|
||||
def action_open_navigator(self) -> None:
|
||||
"""打开导航器"""
|
||||
self.app.push_screen(NavigatorScreen())
|
||||
|
||||
|
||||
def on_input_changed(self, event: Input.Changed) -> None:
|
||||
widget_id = event.input.id
|
||||
if not widget_id:
|
||||
return
|
||||
eepath = undomize(widget_id)
|
||||
value = event.value
|
||||
epath(config_var.get(), eepath, enable_modify=True, new_value=type(epath(config_var.get(), eepath))(value))
|
||||
|
||||
def on_switch_changed(self, event: Switch.Changed) -> None:
|
||||
widget_id = event.switch.id
|
||||
if not widget_id:
|
||||
return
|
||||
eepath = undomize(widget_id)
|
||||
value = event.value
|
||||
epath(config_var.get(), eepath, enable_modify=True, new_value=type(epath(config_var.get(), eepath))(value))
|
||||
|
||||
def on_select_changed(self, event: Select.Changed) -> None:
|
||||
widget_id = event.select.id
|
||||
if not widget_id:
|
||||
return
|
||||
eepath = undomize(widget_id)
|
||||
value = event.value
|
||||
epath(config_var.get(), eepath, enable_modify=True, new_value=type(epath(config_var.get(), eepath))(value))
|
||||
@@ -1,5 +1,4 @@
|
||||
"""同步工具界面"""
|
||||
|
||||
#!/usr/bin/env python3
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
|
||||
@@ -1,8 +1,34 @@
|
||||
"""Kernel 操作辅助函数库"""
|
||||
|
||||
import random
|
||||
from typing import TypedDict
|
||||
|
||||
import heurams.interface.widgets as pzw
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
|
||||
staging = {} # 细粒度缓存区, 是 ident -> quality 的封装
|
||||
|
||||
|
||||
def report_to_staging(atom: pt.Atom, quality):
|
||||
staging[atom.ident] = min(quality, staging[atom.ident])
|
||||
|
||||
|
||||
def clear():
|
||||
staging = dict()
|
||||
|
||||
|
||||
def deploy_to_electron():
|
||||
for atom_ident, quality in staging.items():
|
||||
if pt.atom_registry[atom_ident].registry["electron"].is_activated:
|
||||
pt.atom_registry[atom_ident].registry["electron"].revisor(quality=quality)
|
||||
else:
|
||||
pt.atom_registry[atom_ident].registry["electron"].revisor(
|
||||
quality=quality, is_new_activation=True
|
||||
)
|
||||
clear()
|
||||
|
||||
|
||||
puzzle2widget = {
|
||||
pz.RecognitionPuzzle: pzw.Recognition,
|
||||
pz.ClozePuzzle: pzw.ClozePuzzle,
|
||||
|
||||
@@ -5,11 +5,10 @@ from typing import TypedDict
|
||||
from textual.containers import Container
|
||||
from textual.message import Message
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Button, Label, Markdown
|
||||
from textual.widgets import Button, Label
|
||||
|
||||
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 .base_puzzle_widget import BasePuzzleWidget
|
||||
@@ -54,7 +53,7 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
self.hashmap = dict()
|
||||
|
||||
def _load(self):
|
||||
setting = self.atom.registry["nucleon"]["puzzles"][self.alia]
|
||||
setting = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
self.puzzle = pz.ClozePuzzle(
|
||||
text=setting["text"],
|
||||
delimiter=setting["delimiter"],
|
||||
@@ -66,30 +65,20 @@ class ClozePuzzle(BasePuzzleWidget):
|
||||
|
||||
def compose(self):
|
||||
yield Label(self.puzzle.wording, id="sentence")
|
||||
yield Markdown(f"> {self.listprint(self.inputlist)}", id="inputpreview")
|
||||
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
|
||||
# 渲染当前问题的选项
|
||||
with Container(id="btn-container"):
|
||||
for i in self.ans:
|
||||
h = str(hash(i))
|
||||
self.hashmap[h] = i
|
||||
btnid = f"sel000-{h}"
|
||||
self.hashmap[str(hash(i))] = i
|
||||
btnid = f"sel000-{hash(i)}"
|
||||
logger.debug(f"建立按钮 {btnid}")
|
||||
yield Button(i, id=f"{btnid}")
|
||||
|
||||
yield Button("退格", id="delete")
|
||||
|
||||
def listprint(self, lst):
|
||||
s = ""
|
||||
if lst:
|
||||
lastone = lst[-1]
|
||||
for i in lst[:-1]:
|
||||
s += (i + ' ')
|
||||
s += f" `{lastone}`"
|
||||
return s
|
||||
|
||||
def update_display(self):
|
||||
preview = self.query_one("#inputpreview")
|
||||
preview.update(f"> {self.listprint(self.inputlist)}") # type: ignore
|
||||
preview.update(f"当前输入: {self.inputlist}") # type: ignore
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
button_id = event.button.id
|
||||
|
||||
@@ -7,27 +7,24 @@ class Finished(Widget):
|
||||
self,
|
||||
*children: Widget,
|
||||
alia="",
|
||||
is_saved=0,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
markup: bool = True,
|
||||
markup: bool = True
|
||||
) -> None:
|
||||
self.alia = alia
|
||||
self.is_saved = is_saved
|
||||
super().__init__(
|
||||
*children,
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
markup=markup,
|
||||
markup=markup
|
||||
)
|
||||
|
||||
def compose(self):
|
||||
yield Label("本次记忆进程结束", id="finished_msg")
|
||||
yield Label(f"算法数据{'已保存' if self.is_saved else "未能保存"}")
|
||||
yield Button("返回上一级", id="back-to-menu")
|
||||
|
||||
def on_button_pressed(self, event):
|
||||
|
||||
@@ -54,16 +54,18 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
self._load()
|
||||
|
||||
def _load(self):
|
||||
cfg = self.atom.registry["nucleon"]["puzzles"][self.alia]
|
||||
if cfg['mapping'] == {}:
|
||||
self.screen.rating = 5 # type: ignore
|
||||
cfg = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
self.puzzle = pz.MCQPuzzle(
|
||||
cfg["mapping"], cfg["jammer"], int(cfg["max_riddles_num"]), cfg["prefix"]
|
||||
)
|
||||
self.puzzle.refresh()
|
||||
|
||||
def compose(self):
|
||||
setting: Setting = self.atom.registry["nucleon"]["puzzles"][self.alia]
|
||||
setting: Setting = self.atom.registry["nucleon"].metadata["orbital"]["puzzles"][
|
||||
self.alia
|
||||
]
|
||||
logger.debug(f"Puzzle Setting: {setting}")
|
||||
logger.debug(f"WIRED INDEX: {len(self.inputlist)}")
|
||||
if len(self.inputlist) > len(self.puzzle.options):
|
||||
logger.debug("ERR IDX")
|
||||
logger.debug(self.inputlist)
|
||||
@@ -152,7 +154,7 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
for option in current_options:
|
||||
button_id = f"sel{str(self.cursor).zfill(3)}-{hash(option)}"
|
||||
if button_id not in self.hashmap:
|
||||
self.hashmap[button_id[7:]] = option
|
||||
self.hashmap[button_id] = option
|
||||
new_button = Button(option, id=button_id)
|
||||
container.mount(new_button)
|
||||
|
||||
|
||||
@@ -51,11 +51,11 @@ class Recognition(BasePuzzleWidget):
|
||||
def compose(self):
|
||||
from heurams.context import config_var
|
||||
|
||||
autovoice = config_var.get()["interface"]["widgets"]["autovoice"]
|
||||
autovoice = config_var.get()["interface"]["memorizor"]["autovoice"]
|
||||
if autovoice:
|
||||
self.screen.action_play_voice() # type: ignore
|
||||
cfg: RecognitionConfig = self.atom.registry["nucleon"]["puzzles"][self.alia]
|
||||
delim = self.atom.registry["nucleon"]["delimiter"]
|
||||
cfg: RecognitionConfig = self.atom.registry["orbital"]["puzzles"][self.alia]
|
||||
delim = self.atom.registry["nucleon"].metadata["formation"]["delimiter"]
|
||||
replace_dict = {
|
||||
", ": ",",
|
||||
". ": ".",
|
||||
@@ -68,6 +68,8 @@ class Recognition(BasePuzzleWidget):
|
||||
f":{delim}": ":",
|
||||
}
|
||||
|
||||
nucleon = self.atom.registry["nucleon"]
|
||||
metadata = self.atom.registry["nucleon"].metadata
|
||||
primary = cfg["primary"]
|
||||
|
||||
with Center():
|
||||
@@ -88,12 +90,12 @@ class Recognition(BasePuzzleWidget):
|
||||
for item in cfg["secondary"]:
|
||||
if isinstance(item, list):
|
||||
for j in item:
|
||||
yield Markdown(f"### 笔记: {j}") #TODO ANNOTATION
|
||||
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"
|
||||
total += f"> **{j}**: {k} \n"
|
||||
yield Markdown(total)
|
||||
if isinstance(item, str):
|
||||
yield Markdown(item)
|
||||
@@ -105,3 +107,13 @@ class Recognition(BasePuzzleWidget):
|
||||
if event.button.id == "ok":
|
||||
self.screen.rating = 5 # type: ignore
|
||||
self.handler(5)
|
||||
|
||||
def handler(self, rating):
|
||||
if not self.atom.registry["runtime"]["locked"]:
|
||||
if not self.atom.registry["electron"].is_activated():
|
||||
self.atom.registry["electron"].activate()
|
||||
logger.debug(f"激活原子 {self.atom}")
|
||||
self.atom.lock(1)
|
||||
self.atom.minimize(5)
|
||||
else:
|
||||
pass
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
# Kernel - HeurAMS 核心
|
||||
|
||||
记忆规划相关算法与数据结构, 可脱离业务层
|
||||
@@ -1,18 +1,18 @@
|
||||
from .base import BaseAlgorithm
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .sm2 import SM2Algorithm
|
||||
from .sm15m import SM15MAlgorithm
|
||||
from .nsp0 import NSP0Algorithm
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
__all__ = [
|
||||
"SM2Algorithm",
|
||||
"BaseAlgorithm",
|
||||
"SM15MAlgorithm",
|
||||
"NSP0Algorithm",
|
||||
]
|
||||
|
||||
algorithms = {
|
||||
"SM-2": SM2Algorithm,
|
||||
"NSP-0": NSP0Algorithm,
|
||||
"SM-15M": SM15MAlgorithm,
|
||||
"Base": BaseAlgorithm,
|
||||
# "SM-15M": SM15MAlgorithm,
|
||||
}
|
||||
|
||||
logger.debug("算法模块初始化完成, 注册的算法: %s", list(algorithms.keys()))
|
||||
|
||||
@@ -8,8 +8,9 @@ logger = get_logger(__name__)
|
||||
|
||||
class BaseAlgorithm:
|
||||
algo_name = "BaseAlgorithm"
|
||||
desc = '算法基类'
|
||||
|
||||
class AlgodataDict(TypedDict):
|
||||
efactor: float
|
||||
real_rept: int
|
||||
rept: int
|
||||
interval: int
|
||||
@@ -51,7 +52,7 @@ class BaseAlgorithm:
|
||||
return 1
|
||||
|
||||
@classmethod
|
||||
def get_rating(cls, algodata) -> str:
|
||||
def rate(cls, algodata) -> str:
|
||||
"""获取评分信息"""
|
||||
logger.debug(
|
||||
"BaseAlgorithm.rate 被调用, algodata keys: %s",
|
||||
@@ -67,11 +68,3 @@ class BaseAlgorithm:
|
||||
list(algodata.keys()) if algodata else [],
|
||||
)
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
def check_integrity(cls, algodata):
|
||||
try:
|
||||
cls.AlgodataDict(**algodata[cls.algo_name])
|
||||
return 1
|
||||
except:
|
||||
return 0
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
from typing import TypedDict
|
||||
|
||||
import heurams.services.timer as timer
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .base import BaseAlgorithm
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class NSP0Algorithm(BaseAlgorithm):
|
||||
algo_name = "NSP-0"
|
||||
desc = '快速筛选用特殊调度器'
|
||||
class AlgodataDict(TypedDict):
|
||||
real_rept: int
|
||||
rept: int
|
||||
interval: int
|
||||
important: int
|
||||
last_date: int
|
||||
next_date: int
|
||||
is_activated: int
|
||||
last_modify: float
|
||||
|
||||
defaults = {
|
||||
"real_rept": 0,
|
||||
'important': 0,
|
||||
"rept": 0,
|
||||
"interval": 0,
|
||||
"last_date": 0,
|
||||
"next_date": 0,
|
||||
"is_activated": 0,
|
||||
"last_modify": timer.get_timestamp(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def revisor(
|
||||
cls, algodata: dict, feedback: int = 5, is_new_activation: bool = False
|
||||
):
|
||||
"""NSP-0 算法迭代决策机制实现
|
||||
根据 quality(0 ~ 5) 进行参数迭代最佳间隔
|
||||
quality 由主程序评估
|
||||
|
||||
Args:
|
||||
quality (int): 记忆保留率量化参数
|
||||
"""
|
||||
logger.debug(
|
||||
"NSP0.revisor 开始, feedback: %d, is_new_activation: %s",
|
||||
feedback,
|
||||
is_new_activation,
|
||||
)
|
||||
|
||||
if feedback == -1:
|
||||
logger.debug("feedback 为 -1, 跳过更新")
|
||||
return
|
||||
algodata[cls.algo_name]["interval"] = (1 if feedback <= 3 else float('inf'))
|
||||
algodata[cls.algo_name]["important"] = (1 if feedback <= 3 else algodata[cls.algo_name]["important"])
|
||||
algodata[cls.algo_name]["last_date"] = timer.get_daystamp()
|
||||
algodata[cls.algo_name]["next_date"] = (
|
||||
timer.get_daystamp() + algodata[cls.algo_name]["interval"]
|
||||
)
|
||||
algodata[cls.algo_name]["last_modify"] = timer.get_timestamp()
|
||||
|
||||
logger.debug(
|
||||
"更新日期: last_date=%d, next_date=%d, last_modify=%f",
|
||||
algodata[cls.algo_name]["last_date"],
|
||||
algodata[cls.algo_name]["next_date"],
|
||||
algodata[cls.algo_name]["last_modify"],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def is_due(cls, algodata):
|
||||
result = algodata[cls.algo_name]["next_date"] <= timer.get_daystamp()
|
||||
logger.debug(
|
||||
"NSP0.is_due: next_date=%d, current_daystamp=%d, result=%s",
|
||||
algodata[cls.algo_name]["next_date"],
|
||||
timer.get_daystamp(),
|
||||
result,
|
||||
)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def get_rating(cls, algodata):
|
||||
efactor = algodata[cls.algo_name]["efactor"]
|
||||
logger.debug("NSP0.rate: efactor=%f", efactor)
|
||||
return str(efactor)
|
||||
|
||||
@classmethod
|
||||
def nextdate(cls, algodata) -> int:
|
||||
next_date = algodata[cls.algo_name]["next_date"]
|
||||
logger.debug("NSP0.nextdate: %d", next_date)
|
||||
return next_date
|
||||
@@ -14,22 +14,13 @@ import pathlib
|
||||
from typing import TypedDict
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.algorithms.sm15m_calc import (
|
||||
MAX_AF,
|
||||
MIN_AF,
|
||||
NOTCH_AF,
|
||||
RANGE_AF,
|
||||
RANGE_REPETITION,
|
||||
SM,
|
||||
THRESHOLD_RECALL,
|
||||
Item,
|
||||
)
|
||||
from heurams.kernel.algorithms.sm15m_calc import (MAX_AF, MIN_AF, NOTCH_AF,
|
||||
RANGE_AF, RANGE_REPETITION,
|
||||
SM, THRESHOLD_RECALL, Item)
|
||||
|
||||
# 全局状态文件路径
|
||||
_GLOBAL_STATE_FILE = os.path.expanduser(
|
||||
pathlib.Path(config_var.get()['global']["paths"]["data"])
|
||||
/ "global"
|
||||
/ "sm15m_global_state.json"
|
||||
pathlib.Path(config_var.get()["paths"]["global_dir"]) / "sm15m_global_state.json"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ logger = get_logger(__name__)
|
||||
|
||||
class SM2Algorithm(BaseAlgorithm):
|
||||
algo_name = "SM-2"
|
||||
desc = '经典间隔重复算法'
|
||||
|
||||
class AlgodataDict(TypedDict):
|
||||
efactor: float
|
||||
real_rept: int
|
||||
@@ -116,7 +116,7 @@ class SM2Algorithm(BaseAlgorithm):
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def get_rating(cls, algodata):
|
||||
def rate(cls, algodata):
|
||||
efactor = algodata[cls.algo_name]["efactor"]
|
||||
logger.debug("SM2.rate: efactor=%f", efactor)
|
||||
return str(efactor)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
from .evalizor import Evalizer
|
||||
from .lict import Lict
|
||||
from .refvar import RefVar
|
||||
|
||||
__all__ = ["Evalizer", "Lict", "RefVar"]
|
||||
@@ -1,33 +0,0 @@
|
||||
class Evalizer:
|
||||
"""几乎无副作用的模板系统
|
||||
|
||||
接受环境信息并创建一个模板解析工具, 工具传入参数支持list, dict及其嵌套
|
||||
副作用问题: 仅存在于 eval 函数
|
||||
"""
|
||||
|
||||
# TODO: 弃用风险极高的 eval
|
||||
# TODO: 异步/多线程执行避免堵塞
|
||||
def __init__(self, environment: dict) -> None:
|
||||
self.env = environment
|
||||
|
||||
def __call__(self, anyobj):
|
||||
return self.travel(anyobj)
|
||||
|
||||
def travel(self, anyobj):
|
||||
if isinstance(anyobj, list):
|
||||
return list(map(self.travel, anyobj))
|
||||
elif isinstance(anyobj, dict):
|
||||
return dict(map(self.travel, anyobj.items()))
|
||||
elif isinstance(anyobj, tuple):
|
||||
return tuple(map(self.travel, anyobj))
|
||||
elif isinstance(anyobj, str):
|
||||
if anyobj.startswith("eval:"):
|
||||
return self.eval_with_env(anyobj[5:])
|
||||
else:
|
||||
return anyobj
|
||||
else:
|
||||
return anyobj
|
||||
|
||||
def eval_with_env(self, s: str):
|
||||
ret = eval(s, globals(), self.env)
|
||||
return ret
|
||||
@@ -1,149 +0,0 @@
|
||||
from collections import UserList
|
||||
from typing import Any, Iterator
|
||||
|
||||
|
||||
class Lict(UserList): # TODO: 优化同步(惰性同步), 当前性能为 O(n)
|
||||
""" "列典" 对象
|
||||
|
||||
同时兼容字典和列表大多数 API, 两边数据同步的容器
|
||||
列表数据是 dictobj.items() 的格式
|
||||
支持根据字典或列表初始化
|
||||
限制要求:
|
||||
- 键名一定唯一, 且仅能为字符串
|
||||
- 值一定是引用对象
|
||||
- 不使用并发
|
||||
- 不在乎列表顺序语义(严格按键名字符序排列)和列表索引查找, 因此外部的 sort, index 等功能不可用
|
||||
- append 的元组中, 表示键名的元素不能重复, 否则会导致覆盖行为
|
||||
|
||||
只有在 Python 3.7+ 中, forced_order 行为才能被取消.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
initlist: list | None = None,
|
||||
initdict: dict | None = None,
|
||||
forced_order=False,
|
||||
):
|
||||
self.dicted_data = {}
|
||||
if initdict != None:
|
||||
initlist = list(initdict.items())
|
||||
super().__init__(initlist=initlist)
|
||||
self.forced_order = forced_order
|
||||
self._sync_based_on_list()
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def _sync_based_on_dict(self):
|
||||
self.data = list(self.dicted_data.items())
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def _sync_based_on_list(self):
|
||||
self.dicted_data = {}
|
||||
for i in self.data:
|
||||
self.dicted_data[i[0]] = i[1]
|
||||
|
||||
def __iter__(self) -> Iterator:
|
||||
return self.data.__iter__()
|
||||
|
||||
def __getitem__(self, i):
|
||||
if isinstance(i, str):
|
||||
return self.dicted_data[i]
|
||||
else:
|
||||
return super().__getitem__(i)
|
||||
|
||||
def get_itemic_unit(self, ident):
|
||||
return (ident, self.dicted_data[ident])
|
||||
|
||||
def __setitem__(self, i, item):
|
||||
if isinstance(i, str):
|
||||
self.dicted_data[i] = item
|
||||
self._sync_based_on_dict()
|
||||
else:
|
||||
if item != (item[0], item[1]):
|
||||
raise NotImplementedError
|
||||
super().__setitem__(i, item)
|
||||
self._sync_based_on_list()
|
||||
|
||||
def __delitem__(self, i):
|
||||
if isinstance(i, str):
|
||||
del self.dicted_data[i]
|
||||
self._sync_based_on_dict()
|
||||
else:
|
||||
super().__delitem__(i)
|
||||
self._sync_based_on_list()
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.data or item in self.keys() or item in self.values()
|
||||
|
||||
def append(self, item: Any) -> None:
|
||||
if item != (item[0], item[1]):
|
||||
raise NotImplementedError
|
||||
super().append(item)
|
||||
self._sync_based_on_list()
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def append_new(self, item: Any):
|
||||
if item != (item[0], item[1]):
|
||||
raise NotImplementedError
|
||||
if item[0] not in self:
|
||||
super().append(item)
|
||||
self._sync_based_on_list()
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def insert(self, i: int, item: Any) -> None:
|
||||
if item != (item[0], item[1]): # 确保 item 是遵从限制的元组
|
||||
raise NotImplementedError
|
||||
super().insert(i, item)
|
||||
self._sync_based_on_list()
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def pop(self, i: int = -1) -> Any:
|
||||
res = super().pop(i)
|
||||
self._sync_based_on_list()
|
||||
return res
|
||||
|
||||
def remove(self, item: Any) -> None:
|
||||
if isinstance(item, str):
|
||||
item = (item, self.dicted_data[item])
|
||||
if item != (item[0], item[1]):
|
||||
raise NotImplementedError
|
||||
super().remove(item)
|
||||
self._sync_based_on_list()
|
||||
if self.forced_order:
|
||||
self.data.sort()
|
||||
|
||||
def clear(self) -> None:
|
||||
super().clear()
|
||||
self._sync_based_on_list()
|
||||
|
||||
def index(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def extend(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def sort(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def reverse(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def keys(self):
|
||||
return self.dicted_data.keys()
|
||||
|
||||
def values(self):
|
||||
return self.dicted_data.values()
|
||||
|
||||
def items(self):
|
||||
return self.data
|
||||
|
||||
def keys_equal_with(self, other):
|
||||
return self.key_equality(self, other)
|
||||
|
||||
@classmethod
|
||||
def key_equality(cls, a, b):
|
||||
return a.keys() == b.keys()
|
||||
@@ -1,241 +0,0 @@
|
||||
class RefVar:
|
||||
def __init__(self, initvalue) -> None:
|
||||
self.data = initvalue
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"RefVar({repr(self.data)})"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.data)
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data + other.data)
|
||||
return RefVar(self.data + other)
|
||||
|
||||
def __radd__(self, other):
|
||||
return RefVar(other + self.data)
|
||||
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data - other.data)
|
||||
return RefVar(self.data - other)
|
||||
|
||||
def __rsub__(self, other):
|
||||
return RefVar(other - self.data)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data * other.data)
|
||||
return RefVar(self.data * other)
|
||||
|
||||
def __rmul__(self, other):
|
||||
return RefVar(other * self.data)
|
||||
|
||||
def __truediv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data / other.data)
|
||||
return RefVar(self.data / other)
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
return RefVar(other / self.data)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data // other.data)
|
||||
return RefVar(self.data // other)
|
||||
|
||||
def __rfloordiv__(self, other):
|
||||
return RefVar(other // self.data)
|
||||
|
||||
def __mod__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data % other.data)
|
||||
return RefVar(self.data % other)
|
||||
|
||||
def __rmod__(self, other):
|
||||
return RefVar(other % self.data)
|
||||
|
||||
def __pow__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data**other.data)
|
||||
return RefVar(self.data**other)
|
||||
|
||||
def __rpow__(self, other):
|
||||
return RefVar(other**self.data)
|
||||
|
||||
def __neg__(self):
|
||||
return RefVar(-self.data)
|
||||
|
||||
def __pos__(self):
|
||||
return RefVar(+self.data)
|
||||
|
||||
def __abs__(self):
|
||||
return RefVar(abs(self.data))
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data == other.data
|
||||
return self.data == other
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data < other.data
|
||||
return self.data < other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data <= other.data
|
||||
return self.data <= other
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data > other.data
|
||||
return self.data > other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return self.data >= other.data
|
||||
return self.data >= other
|
||||
|
||||
# 位运算
|
||||
def __and__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data & other.data)
|
||||
return RefVar(self.data & other)
|
||||
|
||||
def __rand__(self, other):
|
||||
return RefVar(other & self.data)
|
||||
|
||||
def __or__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data | other.data)
|
||||
return RefVar(self.data | other)
|
||||
|
||||
def __ror__(self, other):
|
||||
return RefVar(other | self.data)
|
||||
|
||||
def __xor__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data ^ other.data)
|
||||
return RefVar(self.data ^ other)
|
||||
|
||||
def __rxor__(self, other):
|
||||
return RefVar(other ^ self.data)
|
||||
|
||||
def __lshift__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data << other.data)
|
||||
return RefVar(self.data << other)
|
||||
|
||||
def __rlshift__(self, other):
|
||||
return RefVar(other << self.data)
|
||||
|
||||
def __rshift__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
return RefVar(self.data >> other.data)
|
||||
return RefVar(self.data >> other)
|
||||
|
||||
def __rrshift__(self, other):
|
||||
return RefVar(other >> self.data)
|
||||
|
||||
def __invert__(self):
|
||||
return RefVar(~self.data)
|
||||
|
||||
# 类型转换
|
||||
def __int__(self):
|
||||
return int(self.data)
|
||||
|
||||
def __float__(self):
|
||||
return float(self.data)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.data)
|
||||
|
||||
def __complex__(self):
|
||||
return complex(self.data)
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self.data)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.data)
|
||||
|
||||
# 容器操作(如果底层数据支持)
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.data[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.data[key] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self.data[key]
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.data
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.data)
|
||||
|
||||
def __iadd__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data += other.data
|
||||
else:
|
||||
self.data += other
|
||||
return self
|
||||
|
||||
def __isub__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data -= other.data
|
||||
else:
|
||||
self.data -= other
|
||||
return self
|
||||
|
||||
def __imul__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data *= other.data
|
||||
else:
|
||||
self.data *= other
|
||||
return self
|
||||
|
||||
def __itruediv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data /= other.data
|
||||
else:
|
||||
self.data /= other
|
||||
return self
|
||||
|
||||
def __ifloordiv__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data //= other.data
|
||||
else:
|
||||
self.data //= other
|
||||
return self
|
||||
|
||||
def __imod__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data %= other.data
|
||||
else:
|
||||
self.data %= other
|
||||
return self
|
||||
|
||||
def __ipow__(self, other):
|
||||
if isinstance(other, RefVar):
|
||||
self.data **= other.data
|
||||
else:
|
||||
self.data **= other
|
||||
return self
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
if callable(self.data):
|
||||
return self.data(*args, **kwargs)
|
||||
raise TypeError(f"'{type(self.data).__name__}' object is not callable")
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.data, name)
|
||||
@@ -1,21 +1,29 @@
|
||||
from .atom import Atom
|
||||
from .electron import Electron
|
||||
from .nucleon import Nucleon
|
||||
from .placeholders import (
|
||||
AtomPlaceholder,
|
||||
ElectronPlaceholder,
|
||||
NucleonPlaceholder,
|
||||
orbital_placeholder,
|
||||
)
|
||||
"""
|
||||
Particle 模块 - 粒子对象系统
|
||||
|
||||
# from .orbital import Orbital
|
||||
提供闪卡所需对象, 使用物理学粒子的领域驱动设计
|
||||
"""
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
logger.debug("粒子模块已加载")
|
||||
|
||||
from .atom import Atom, atom_registry
|
||||
from .electron import Electron
|
||||
from .loader import load_electron, load_nucleon
|
||||
from .nucleon import Nucleon
|
||||
from .orbital import Orbital
|
||||
from .probe import probe_all, probe_by_filename
|
||||
|
||||
__all__ = [
|
||||
"Atom",
|
||||
"Electron",
|
||||
"Nucleon",
|
||||
"AtomPlaceholder",
|
||||
"NucleonPlaceholder",
|
||||
"ElectronPlaceholder",
|
||||
"orbital_placeholder",
|
||||
"Orbital",
|
||||
"Atom",
|
||||
"probe_all",
|
||||
"probe_by_filename",
|
||||
"load_nucleon",
|
||||
"load_electron",
|
||||
"atom_registry",
|
||||
]
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import json
|
||||
import pathlib
|
||||
import typing
|
||||
from typing import TypedDict
|
||||
|
||||
import bidict
|
||||
import toml
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .electron import Electron
|
||||
from .nucleon import Nucleon
|
||||
from .orbital import Orbital
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -11,13 +19,19 @@ logger = get_logger(__name__)
|
||||
class AtomRegister_runtime(TypedDict):
|
||||
locked: bool # 只读锁定标识符
|
||||
min_rate: int # 最低评分
|
||||
new_activation: bool # 新激活
|
||||
newact: bool # 新激活
|
||||
|
||||
|
||||
class AtomRegister(TypedDict):
|
||||
nucleon: Nucleon
|
||||
nucleon_path: pathlib.Path
|
||||
nucleon_fmt: str
|
||||
electron: Electron
|
||||
orbital: dict
|
||||
electron_path: pathlib.Path
|
||||
electron_fmt: str
|
||||
orbital: Orbital
|
||||
orbital_path: pathlib.Path
|
||||
orbital_fmt: str
|
||||
runtime: AtomRegister_runtime
|
||||
|
||||
|
||||
@@ -30,27 +44,88 @@ class Atom:
|
||||
以及关联路径
|
||||
"""
|
||||
|
||||
default_runtime = {
|
||||
"locked": False,
|
||||
"min_rate": float('inf'),
|
||||
"new_activation": False,
|
||||
}
|
||||
|
||||
def __init__(self, nucleon_obj=None, electron_obj=None, orbital_obj=None):
|
||||
self.ident = nucleon_obj["ident"] # type: ignore
|
||||
def __init__(self, ident=""):
|
||||
logger.debug("创建 Atom 实例, ident: '%s'", ident)
|
||||
self.ident = ident
|
||||
atom_registry[ident] = self
|
||||
logger.debug("Atom 已注册到全局注册表, 当前注册表大小: %d", len(atom_registry))
|
||||
# self.is_evaled = False
|
||||
self.registry: AtomRegister = { # type: ignore
|
||||
"ident": nucleon_obj["ident"], # type: ignore
|
||||
"nucleon": nucleon_obj,
|
||||
"electron": electron_obj,
|
||||
"orbital": orbital_obj,
|
||||
"runtime": dict(),
|
||||
"nucleon": None,
|
||||
"nucleon_path": None,
|
||||
"nucleon_fmt": "toml",
|
||||
"electron": None,
|
||||
"electron_path": None,
|
||||
"electron_fmt": "json",
|
||||
"orbital": None,
|
||||
"orbital_path": None, # 允许设置为 None, 此时使用 nucleon 文件内的推荐配置
|
||||
"orbital_fmt": "toml",
|
||||
"runtime": {"locked": False, "min_rate": 0x3F3F3F3F, "newact": False},
|
||||
}
|
||||
self.init_runtime()
|
||||
if self.registry["electron"].is_activated() == 0:
|
||||
self.registry["runtime"]["new_activation"] = True
|
||||
logger.debug("Atom 初始化完成")
|
||||
|
||||
def init_runtime(self):
|
||||
self.registry["runtime"] = AtomRegister_runtime(**self.default_runtime)
|
||||
def link(self, key, value):
|
||||
logger.debug("Atom.link: key='%s', value type: %s", key, type(value).__name__)
|
||||
if key in self.registry.keys():
|
||||
self.registry[key] = value
|
||||
logger.debug("键 '%s' 已链接, 触发 do_eval", key)
|
||||
if key == "electron":
|
||||
if self.registry["electron"].is_activated() == 0:
|
||||
self.registry["runtime"]["newact"] = True
|
||||
else:
|
||||
logger.error("尝试链接不受支持的键: '%s'", key)
|
||||
raise ValueError("不受支持的原子元数据链接操作")
|
||||
|
||||
def do_eval(self):
|
||||
"""
|
||||
执行并以结果替换当前单元的所有 eval 语句
|
||||
TODO: 带有限制的 eval, 异步/多线程执行避免堵塞
|
||||
"""
|
||||
logger.debug("EVAL 开始")
|
||||
|
||||
# eval 环境设置
|
||||
def eval_with_env(s: str):
|
||||
default = config_var.get()["puzzles"]
|
||||
payload = self.registry["nucleon"].payload
|
||||
metadata = self.registry["nucleon"].metadata
|
||||
eval_value = eval(s)
|
||||
if isinstance(eval_value, (int, float)):
|
||||
ret = str(eval_value)
|
||||
else:
|
||||
ret = eval_value
|
||||
logger.debug(
|
||||
"eval 执行成功: '%s' -> '%s'",
|
||||
s,
|
||||
str(ret)[:50] + "..." if len(ret) > 50 else ret,
|
||||
)
|
||||
return ret
|
||||
|
||||
def traverse(data, modifier):
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
data[key] = traverse(value, modifier)
|
||||
return data
|
||||
elif isinstance(data, list):
|
||||
for i, item in enumerate(data):
|
||||
data[i] = traverse(item, modifier)
|
||||
return data
|
||||
elif isinstance(data, tuple):
|
||||
return tuple(traverse(item, modifier) for item in data)
|
||||
else:
|
||||
if isinstance(data, str):
|
||||
if data.startswith("eval:"):
|
||||
logger.debug("发现 eval 表达式: '%s'", data[5:])
|
||||
return modifier(data[5:])
|
||||
return data
|
||||
|
||||
try:
|
||||
traverse(self.registry["nucleon"].payload, eval_with_env)
|
||||
traverse(self.registry["nucleon"].metadata, eval_with_env)
|
||||
traverse(self.registry["orbital"], eval_with_env)
|
||||
except Exception as e:
|
||||
ret = f"此 eval 实例发生错误: {e}"
|
||||
logger.warning(ret)
|
||||
logger.debug("EVAL 完成")
|
||||
|
||||
def minimize(self, rating):
|
||||
"""效果等同于 self.registry['runtime']['min_rate'] = min(rating, self.registry['runtime']['min_rate'])
|
||||
@@ -83,21 +158,62 @@ class Atom:
|
||||
logger.debug(f"允许总评分: {self.registry['runtime']['min_rate']}")
|
||||
self.registry["electron"].revisor(
|
||||
self.registry["runtime"]["min_rate"],
|
||||
is_new_activation=self.registry["runtime"]["new_activation"],
|
||||
is_new_activation=self.registry["runtime"]["newact"],
|
||||
)
|
||||
else:
|
||||
logger.debug("禁止总评分")
|
||||
|
||||
def persist(self, key):
|
||||
logger.debug("Atom.persist: key='%s'", key)
|
||||
path: pathlib.Path | None = self.registry[key + "_path"]
|
||||
if isinstance(path, pathlib.Path):
|
||||
path = typing.cast(pathlib.Path, path)
|
||||
logger.debug("持久化路径: %s, 格式: %s", path, self.registry[key + "_fmt"])
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if self.registry[key + "_fmt"] == "toml":
|
||||
with open(path, "r+") as f:
|
||||
f.seek(0)
|
||||
f.truncate()
|
||||
toml.dump(self.registry[key], f)
|
||||
logger.debug("TOML 数据已保存到: %s", path)
|
||||
elif self.registry[key + "_fmt"] == "json":
|
||||
with open(path, "r+") as f:
|
||||
origin = json.load(f)
|
||||
f.seek(0)
|
||||
f.truncate()
|
||||
origin[self.ident] = self.registry[key].algodata
|
||||
json.dump(origin, f, indent=2, ensure_ascii=False)
|
||||
logger.debug("JSON 数据已保存到: %s", path)
|
||||
else:
|
||||
logger.error("不受支持的持久化格式: %s", self.registry[key + "_fmt"])
|
||||
raise KeyError("不受支持的持久化格式")
|
||||
else:
|
||||
logger.error("路径未初始化: %s_path", key)
|
||||
raise TypeError("对未初始化的路径对象操作")
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.registry[key]
|
||||
logger.debug("Atom.__getitem__: key='%s'", key)
|
||||
if key in self.registry:
|
||||
value = self.registry[key]
|
||||
logger.debug("返回 value type: %s", type(value).__name__)
|
||||
return value
|
||||
logger.error("不支持的键: '%s'", key)
|
||||
raise KeyError(f"不支持的键: {key}")
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key == "ident":
|
||||
raise AttributeError("应为只读")
|
||||
logger.debug(
|
||||
"Atom.__setitem__: key='%s', value type: %s", key, type(value).__name__
|
||||
)
|
||||
if key in self.registry:
|
||||
self.registry[key] = value
|
||||
logger.debug("键 '%s' 已设置", key)
|
||||
else:
|
||||
logger.error("不支持的键: '%s'", key)
|
||||
raise KeyError(f"不支持的键: {key}")
|
||||
|
||||
def __repr__(self):
|
||||
from pprint import pformat
|
||||
@staticmethod
|
||||
def placeholder():
|
||||
return (Electron.placeholder(), Nucleon.placeholder(), {})
|
||||
|
||||
s = pformat(self.registry, indent=4)
|
||||
return s
|
||||
|
||||
atom_registry: bidict.bidict[str, Atom] = bidict.bidict()
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
from copy import deepcopy
|
||||
from typing import TypedDict
|
||||
|
||||
import heurams.kernel.algorithms as algolib
|
||||
import heurams.services.timer as timer
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.algorithms import algorithms
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
@@ -10,75 +7,94 @@ logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Electron:
|
||||
"""电子: 单算法支持的记忆数据包装"""
|
||||
"""电子: 记忆分析元数据及算法"""
|
||||
|
||||
def __init__(self, ident: str, algodata: dict = {}, algo_name: str = ""):
|
||||
"""初始化电子对象 (记忆数据)
|
||||
|
||||
Args:
|
||||
ident: 算法的唯一标识符, 用于区分不同的算法实例, 使用 algodata[ident] 获取
|
||||
algodata: 算法数据字典引用, 包含算法的各项参数和设置
|
||||
algo_name: 使用的算法模块标识
|
||||
algodata: 算法数据字典, 包含算法的各项参数和设置
|
||||
algo: 使用的算法模块标识
|
||||
"""
|
||||
if algo_name == "":
|
||||
algo_name = "SM-2"
|
||||
algo_name = config_var.get()["algorithm"]["default"]
|
||||
logger.debug(
|
||||
"创建 Electron 实例, ident: '%s', algo_name: '%s', algodata: %s",
|
||||
ident,
|
||||
algo_name,
|
||||
algodata,
|
||||
)
|
||||
self.algodata = algodata
|
||||
self.ident = ident
|
||||
self.algo: algolib.BaseAlgorithm = algorithms[algo_name]
|
||||
self.algo = algorithms[algo_name]
|
||||
logger.debug("使用的算法类: %s", self.algo.__name__)
|
||||
|
||||
if not self.algo.check_integrity(self.algodata):
|
||||
self.algodata[self.algo.algo_name] = deepcopy(self.algo.defaults)
|
||||
if self.algo.algo_name not in self.algodata.keys():
|
||||
self.algodata[self.algo.algo_name] = {}
|
||||
logger.debug("算法键 '%s' 不存在, 已创建空字典", self.algo)
|
||||
if not self.algodata[self.algo.algo_name]:
|
||||
logger.debug(
|
||||
f"算法数据为空, 使用默认值初始化{self.algodata[self.algo.algo_name]}"
|
||||
)
|
||||
self._default_init(self.algo.defaults)
|
||||
else:
|
||||
logger.debug("算法数据已存在, 跳过默认初始化")
|
||||
logger.debug(
|
||||
"Electron 初始化完成, algodata keys: %s", list(self.algodata.keys())
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
from pprint import pformat
|
||||
|
||||
s = pformat(self.algodata, indent=4)
|
||||
return s
|
||||
def _default_init(self, defaults: dict):
|
||||
"""默认初始化包装"""
|
||||
logger.debug(
|
||||
"Electron._default_init: 使用默认值, keys: %s", list(defaults.keys())
|
||||
)
|
||||
self.algodata[self.algo.algo_name] = defaults.copy()
|
||||
|
||||
def activate(self):
|
||||
"""激活此电子"""
|
||||
logger.debug("Electron.activate: 激活 ident='%s'", self.ident)
|
||||
self.algodata[self.algo.algo_name]["is_activated"] = 1
|
||||
self.algodata[self.algo.algo_name]["last_modify"] = timer.get_timestamp()
|
||||
logger.debug("电子已激活, is_activated=1")
|
||||
|
||||
def modify(self, key, value):
|
||||
def modify(self, var: str, value):
|
||||
"""修改 algodata[algo] 中子字典数据"""
|
||||
if key in self.algodata[self.algo.algo_name]:
|
||||
self.algodata[self.algo.algo_name][key] = value
|
||||
logger.debug("Electron.modify: var='%s', value=%s", var, value)
|
||||
if var in self.algodata[self.algo.algo_name]:
|
||||
self.algodata[self.algo.algo_name][var] = value
|
||||
self.algodata[self.algo.algo_name]["last_modify"] = timer.get_timestamp()
|
||||
logger.debug("变量 '%s' 已修改, 更新 last_modify", var)
|
||||
else:
|
||||
raise AttributeError("不存在的子键")
|
||||
logger.warning("'%s' 非已知元数据字段", var)
|
||||
print(f"警告: '{var}' 非已知元数据字段")
|
||||
|
||||
def is_due(self):
|
||||
"""是否应该复习"""
|
||||
logger.debug("Electron.is_due: 检查 ident='%s'", self.ident)
|
||||
result = self.algo.is_due(self.algodata)
|
||||
logger.debug("is_due 结果: %s", result)
|
||||
return result and self.is_activated()
|
||||
|
||||
def rept(self, real_rept = False):
|
||||
if real_rept:
|
||||
return self.algodata[self.algo.algo_name]['real_rept']
|
||||
return self.algodata[self.algo.algo_name]['rept']
|
||||
|
||||
def is_activated(self):
|
||||
result = self.algodata[self.algo.algo_name]["is_activated"]
|
||||
logger.debug("Electron.is_activated: ident='%s', 结果: %d", self.ident, result)
|
||||
return result
|
||||
|
||||
def last_modify(self):
|
||||
result = self.algodata[self.algo.algo_name]["last_modify"]
|
||||
return result
|
||||
|
||||
def get_rating(self):
|
||||
def get_rate(self):
|
||||
"评价"
|
||||
try:
|
||||
result = self.algo.get_rating(self.algodata)
|
||||
logger.debug("Electron.rate: ident='%s'", self.ident)
|
||||
result = self.algo.rate(self.algodata)
|
||||
logger.debug("rate 结果: %s", result)
|
||||
return result
|
||||
except:
|
||||
return 0
|
||||
|
||||
def nextdate(self) -> int:
|
||||
logger.debug("Electron.nextdate: ident='%s'", self.ident)
|
||||
result = self.algo.nextdate(self.algodata)
|
||||
return result
|
||||
|
||||
def lastdate(self) -> int:
|
||||
result = self.algodata[self.algo.algo_name]["lastdate"]
|
||||
logger.debug("nextdate 结果: %d", result)
|
||||
return result
|
||||
|
||||
def revisor(self, quality: int = 5, is_new_activation: bool = False):
|
||||
@@ -88,7 +104,32 @@ class Electron:
|
||||
quality (int): 记忆保留率量化参数 (0-5)
|
||||
is_new_activation (bool): 是否为初次激活
|
||||
"""
|
||||
logger.debug(
|
||||
"Electron.revisor: ident='%s', quality=%d, is_new_activation=%s",
|
||||
self.ident,
|
||||
quality,
|
||||
is_new_activation,
|
||||
)
|
||||
self.algo.revisor(self.algodata, quality, is_new_activation)
|
||||
logger.debug(
|
||||
"revisor 完成, 更新后的 algodata: %s", self.algodata.get(self.algo, {})
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
f"记忆单元预览 \n"
|
||||
f"标识符: '{self.ident}' \n"
|
||||
f"算法: {self.algo} \n"
|
||||
f"易度系数: {self.algodata[self.algo.algo_name]['efactor']:.2f} \n"
|
||||
f"已经重复的次数: {self.algodata[self.algo.algo_name]['rept']} \n"
|
||||
f"下次间隔: {self.algodata[self.algo.algo_name]['interval']} 天 \n"
|
||||
f"下次复习日期时间戳: {self.algodata[self.algo.algo_name]['next_date']}"
|
||||
)
|
||||
|
||||
def __eq__(self, other):
|
||||
if self.ident == other.ident:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.ident)
|
||||
@@ -112,9 +153,6 @@ class Electron:
|
||||
return len(self.algodata[self.algo.algo_name])
|
||||
|
||||
@staticmethod
|
||||
def create_on_electonic_data(electronic_data: tuple, algo_name: str = ""):
|
||||
_data = electronic_data
|
||||
ident = _data[0]
|
||||
algodata = _data[1]
|
||||
ident = ident
|
||||
return Electron(ident, algodata, algo_name)
|
||||
def placeholder():
|
||||
"""生成一个电子占位符"""
|
||||
return Electron("电子对象样例内容", {})
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import json
|
||||
import pathlib
|
||||
from copy import deepcopy
|
||||
|
||||
import toml
|
||||
|
||||
import heurams.services.hasher as hasher
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .electron import Electron
|
||||
from .nucleon import Nucleon
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def load_nucleon(path: pathlib.Path, fmt="toml"):
|
||||
logger.debug("load_nucleon: 加载文件 %s, 格式: %s", path, fmt)
|
||||
with open(path, "r") as f:
|
||||
dictdata = dict()
|
||||
dictdata = toml.load(f) # type: ignore
|
||||
logger.debug("TOML 解析成功, keys: %s", list(dictdata.keys()))
|
||||
lst = list()
|
||||
nested_data = dict()
|
||||
# 修正 toml 解析器的不管嵌套行为
|
||||
for key, value in dictdata.items():
|
||||
if "__metadata__" in key: # 以免影响句号
|
||||
if "." in key:
|
||||
parts = key.split(".")
|
||||
current = nested_data
|
||||
for part in parts[:-1]:
|
||||
if part not in current:
|
||||
current[part] = {}
|
||||
current = current[part]
|
||||
current[parts[-1]] = value
|
||||
logger.debug("处理元数据键: %s", key)
|
||||
else:
|
||||
nested_data[key] = value
|
||||
logger.debug("嵌套数据处理完成, keys: %s", list(nested_data.keys()))
|
||||
# print(nested_data)
|
||||
for item, attr in nested_data.items():
|
||||
if item == "__metadata__":
|
||||
continue
|
||||
logger.debug("处理项目: %s", item)
|
||||
lst.append(
|
||||
(
|
||||
Nucleon(item, attr, deepcopy(nested_data["__metadata__"])),
|
||||
deepcopy(nested_data["__metadata__"]["orbital"]),
|
||||
)
|
||||
)
|
||||
logger.debug("load_nucleon 完成, 加载了 %d 个 Nucleon 对象", len(lst))
|
||||
return lst
|
||||
|
||||
|
||||
def load_electron(path: pathlib.Path, fmt="json") -> dict:
|
||||
"""从文件路径加载电子对象
|
||||
|
||||
Args:
|
||||
path (pathlib.Path): 路径
|
||||
fmt (str): 文件格式(可选, 默认 json)
|
||||
|
||||
Returns:
|
||||
dict: 键名是电子对象名称, 值是电子对象
|
||||
"""
|
||||
logger.debug("load_electron: 加载文件 %s, 格式: %s", path, fmt)
|
||||
with open(path, "r") as f:
|
||||
dictdata = dict()
|
||||
dictdata = json.load(f) # type: ignore
|
||||
logger.debug("JSON 解析成功, keys: %s", list(dictdata.keys()))
|
||||
dic = dict()
|
||||
for item, attr in dictdata.items():
|
||||
logger.debug("处理电子项目: %s, %s", item, attr)
|
||||
dic[item] = Electron(item, attr)
|
||||
logger.debug("load_electron 完成, 加载了 %d 个 Electron 对象", len(dic))
|
||||
return dic
|
||||
@@ -1,79 +1,56 @@
|
||||
from copy import deepcopy
|
||||
from logging import config
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.services.logger import get_logger
|
||||
from heurams.kernel.auxiliary.evalizor import Evalizer
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Nucleon:
|
||||
"""原子核: 带有运行时隔离的模板化只读材料元数据容器"""
|
||||
"""原子核: 材料元数据"""
|
||||
|
||||
def __init__(self, ident, payload, common):
|
||||
def __init__(self, ident: str, payload: dict, metadata: dict = {}):
|
||||
"""初始化原子核 (记忆内容)
|
||||
|
||||
Args:
|
||||
ident: 唯一标识符
|
||||
payload: 记忆内容信息
|
||||
metadata: 可选元数据信息
|
||||
"""
|
||||
logger.debug(
|
||||
"创建 Nucleon 实例, ident: '%s', payload keys: %s, metadata keys: %s",
|
||||
ident,
|
||||
list(payload.keys()) if payload else [],
|
||||
list(metadata.keys()) if metadata else [],
|
||||
)
|
||||
self.metadata = metadata
|
||||
self.payload = payload
|
||||
self.ident = ident
|
||||
try:
|
||||
data_safe = deepcopy((payload | common))
|
||||
data_puz = deepcopy(data_safe['puzzles'])
|
||||
data_safe['puzzles'] = {}
|
||||
env = {
|
||||
"payload": data_safe,
|
||||
"default": config_var.get()['interface']["puzzles"],
|
||||
"nucleon": data_safe,
|
||||
}
|
||||
self.evalizer = Evalizer(environment=env)
|
||||
data_safe = self.evalizer(deepcopy(data_safe))
|
||||
env = {
|
||||
"payload": data_safe,
|
||||
"default": config_var.get()['interface']["puzzles"],
|
||||
"nucleon": data_safe,
|
||||
}
|
||||
self.evalizer = Evalizer(environment=env)
|
||||
data_puz = self.evalizer(deepcopy(data_puz))
|
||||
data_safe['puzzles'] = data_puz # type: ignore
|
||||
self.data: dict = data_safe # type: ignore
|
||||
except Exception:
|
||||
self.data = (payload | common)
|
||||
logger.debug("Nucleon 初始化完成")
|
||||
|
||||
def __getitem__(self, key):
|
||||
if isinstance(key, str):
|
||||
logger.debug("Nucleon.__getitem__: key='%s'", key)
|
||||
if key == "ident":
|
||||
logger.debug("返回 ident: '%s'", self.ident)
|
||||
return self.ident
|
||||
return self.data[key]
|
||||
if key in self.payload:
|
||||
value = self.payload[key]
|
||||
logger.debug(
|
||||
"返回 payload['%s'], value type: %s", key, type(value).__name__
|
||||
)
|
||||
return value
|
||||
else:
|
||||
raise AttributeError
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
raise AttributeError("应为只读")
|
||||
|
||||
def __delitem__(self, key):
|
||||
raise AttributeError("应为只读")
|
||||
logger.error("键 '%s' 未在 payload 中找到", key)
|
||||
raise KeyError(f"Key '{key}' not found in payload.")
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.data)
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in (self.data)
|
||||
|
||||
def get(self, key, default=None):
|
||||
if key in self:
|
||||
return self[key]
|
||||
return default
|
||||
yield from self.payload.keys()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
return len(self.payload)
|
||||
|
||||
def __repr__(self):
|
||||
from pprint import pformat
|
||||
|
||||
s = pformat(self.data, indent=4)
|
||||
return s
|
||||
def __hash__(self):
|
||||
return hash(self.ident)
|
||||
|
||||
@staticmethod
|
||||
def create_on_nucleonic_data(nucleonic_data: tuple):
|
||||
_data = nucleonic_data
|
||||
payload = _data[1][0]
|
||||
common = _data[1][1]
|
||||
ident = _data[0] # TODO:实现eval
|
||||
return Nucleon(ident, payload, common)
|
||||
def placeholder():
|
||||
"""生成一个占位原子核"""
|
||||
logger.debug("创建 Nucleon 占位符")
|
||||
return Nucleon("核子对象样例内容", {})
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
"""轨道对象"""
|
||||
from typing import TypedDict
|
||||
|
||||
# 似乎没有实现这个类的必要...
|
||||
# 那不妨在这儿写点文档
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
"""
|
||||
orbital, 即轨道, 是定义队列式复习阶段流程的数据结构, 其实就是个字典, 至于为何不用typeddict, 因为懒.
|
||||
|
||||
orbital_example = {
|
||||
"schedule": [列表 存储阶段(phases)名称]
|
||||
"phases":{
|
||||
阶段名称 = [["谜题(puzzle 现称 Puzzles 评估器)名称", "概率系数 可大于1(整数部分为重复次数) 注意使用字符串包裹(toml 规范)"], ...],
|
||||
...
|
||||
}
|
||||
}
|
||||
至于谜题定义 放在 nucleon['puzzles'], 这样设计是为了兼容多种不同谜题实现的记忆单元, 尽管如此, 你也可见其谜题调度方式必须是相同的.
|
||||
logger = get_logger(__name__)
|
||||
logger.debug("Orbital 类型定义模块已加载")
|
||||
|
||||
|
||||
class OrbitalSchedule(TypedDict):
|
||||
quick_review: list
|
||||
recognition: list
|
||||
final_review: list
|
||||
|
||||
|
||||
class Orbital(TypedDict):
|
||||
schedule: OrbitalSchedule
|
||||
puzzles: dict
|
||||
|
||||
|
||||
"""一份示例
|
||||
["__metadata__.orbital.puzzles"] # 谜题定义
|
||||
"Recognition" = { __origin__ = "recognition", __hint__ = "", primary = "eval:nucleon['content']", secondery = ["eval:nucleon['keyword_note']", "eval:nucleon['note']"], top_dim = ["eval:nucleon['translation']"] }
|
||||
"SelectMeaning" = { __origin__ = "mcq", __hint__ = "eval:nucleon['content']", jammer = "eval:nucleon['keyword_note']", max_riddles_num = "eval:default['mcq']['max_riddles_num']", prefix = "选择正确项: " }
|
||||
"FillBlank" = { __origin__ = "cloze", __hint__ = "", text = "eval:nucleon['content']", delimiter = "eval:metadata['formation']['delimiter']", min_denominator = "eval:default['cloze']['min_denominator']"}
|
||||
|
||||
["__metadata__.orbital.schedule"] # 内置的推荐学习方案
|
||||
quick_review = [["FillBlank", "1.0"], ["SelectMeaning", "0.5"], ["recognition", "1.0"]]
|
||||
recognition = [["recognition", "1.0"]]
|
||||
final_review = [["FillBlank", "0.7"], ["SelectMeaning", "0.7"], ["recognition", "1.0"]]
|
||||
"""
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
from heurams.kernel.particles import orbital
|
||||
|
||||
from .atom import Atom
|
||||
from .electron import Electron
|
||||
from .nucleon import Nucleon
|
||||
|
||||
orbital_placeholder = {
|
||||
"schedule": ["quick_review", "recognition", "final_review"],
|
||||
"phases": {
|
||||
"quick_review": [
|
||||
["FillBlank", 1.0],
|
||||
["SelectMeaning", 0.5],
|
||||
["Recognition", 1.0],
|
||||
],
|
||||
"recognition": [["Recognition", 1.0]],
|
||||
"final_review": [
|
||||
["FillBlank", 0.7],
|
||||
["SelectMeaning", 0.7],
|
||||
["Recognition", 1.0],
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class NucleonPlaceholder(Nucleon):
|
||||
def __init__(self):
|
||||
super().__init__("__placeholder__", {}, {})
|
||||
|
||||
def __getitem__(self, key):
|
||||
return f"__placeholder__ attempted {key}"
|
||||
|
||||
|
||||
class ElectronPlaceholder(Electron):
|
||||
def __init__(self):
|
||||
super().__init__("__placeholder__", {"": {"": ""}}, "")
|
||||
|
||||
|
||||
class AtomPlaceholder(Atom):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
NucleonPlaceholder(), ElectronPlaceholder(), orbital_placeholder
|
||||
)
|
||||
@@ -0,0 +1,62 @@
|
||||
import pathlib
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def probe_by_filename(filename):
|
||||
"""探测指定文件 (无扩展名) 的所有信息"""
|
||||
logger.debug("probe_by_filename: 探测文件 '%s'", filename)
|
||||
paths: dict = config_var.get().get("paths")
|
||||
logger.debug("配置路径: %s", paths)
|
||||
formats = ["toml", "json"]
|
||||
result = {}
|
||||
for item, attr in paths.items():
|
||||
for i in formats:
|
||||
attr: pathlib.Path = pathlib.Path(attr) / filename + "." + i
|
||||
if attr.exists():
|
||||
logger.debug("找到文件: %s", attr)
|
||||
result[item.replace("_dir", "")] = str(attr)
|
||||
else:
|
||||
logger.debug("文件不存在: %s", attr)
|
||||
logger.debug("probe_by_filename 结果: %s", result)
|
||||
return result
|
||||
|
||||
|
||||
def probe_all(is_stem=1):
|
||||
"""依据目录探测所有信息
|
||||
|
||||
Args:
|
||||
is_stem (boolean): 是否**删除**文件扩展名
|
||||
|
||||
Returns:
|
||||
dict: 有三项, 每一项的键名都是文件组类型, 值都是文件组列表, 只包含文件名
|
||||
"""
|
||||
logger.debug("probe_all: 开始探测, is_stem=%d", is_stem)
|
||||
paths: dict = config_var.get().get("paths")
|
||||
logger.debug("配置路径: %s", paths)
|
||||
result = {}
|
||||
for item, attr in paths.items():
|
||||
attr: pathlib.Path = pathlib.Path(attr)
|
||||
result[item.replace("_dir", "")] = list()
|
||||
logger.debug("扫描目录: %s", attr)
|
||||
file_count = 0
|
||||
for i in attr.iterdir():
|
||||
if not i.is_dir():
|
||||
file_count += 1
|
||||
if is_stem:
|
||||
result[item.replace("_dir", "")].append(str(i.stem))
|
||||
else:
|
||||
result[item.replace("_dir", "")].append(str(i.name))
|
||||
logger.debug("目录 %s 中找到 %d 个文件", attr, file_count)
|
||||
logger.debug("probe_all 完成, 结果 keys: %s", list(result.keys()))
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
|
||||
print(os.getcwd())
|
||||
print(probe_all())
|
||||
@@ -1,11 +1,13 @@
|
||||
"""
|
||||
Puzzles 模块 - 生成评估模块
|
||||
Puzzle 模块 - 谜题生成系统
|
||||
|
||||
提供多种类型的辅助评估生成器, 支持从字符串、字典等数据源导入题目
|
||||
提供多种类型的谜题生成器, 支持从字符串、字典等数据源导入题目
|
||||
"""
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
from .base import BasePuzzle
|
||||
from .cloze import ClozePuzzle
|
||||
from .mcq import MCQPuzzle
|
||||
@@ -24,3 +26,38 @@ puzzles = {
|
||||
"recognition": RecognitionPuzzle,
|
||||
"base": BasePuzzle,
|
||||
}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def create_by_dict(config_dict: dict) -> BasePuzzle:
|
||||
"""
|
||||
根据配置字典创建谜题
|
||||
|
||||
Args:
|
||||
config_dict: 配置字典, 包含谜题类型和参数
|
||||
|
||||
Returns:
|
||||
BasePuzzle: 谜题实例
|
||||
|
||||
Raises:
|
||||
ValueError: 当配置无效时抛出
|
||||
"""
|
||||
logger.debug(
|
||||
"puzzles.create_by_dict: config_dict keys=%s", list(config_dict.keys())
|
||||
)
|
||||
puzzle_type = config_dict.get("type")
|
||||
|
||||
if puzzle_type == "cloze":
|
||||
return puzzles["cloze"](
|
||||
text=config_dict["text"],
|
||||
min_denominator=config_dict.get("min_denominator", 7),
|
||||
)
|
||||
elif puzzle_type == "mcq":
|
||||
return puzzles["mcq"](
|
||||
mapping=config_dict["mapping"],
|
||||
jammer=config_dict.get("jammer", []),
|
||||
max_riddles_num=config_dict.get("max_riddles_num", 2),
|
||||
prefix=config_dict.get("prefix", ""),
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"未知的谜题类型: {puzzle_type}")
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import random
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .base import BasePuzzle
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class GuessPuzzle(BasePuzzle):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -1,173 +0,0 @@
|
||||
# Reactor - 记忆流程状态机模块
|
||||
|
||||
Reactor 是 HeurAMS 的记忆流程状态机模块, 和界面 (interface) 的实现是解耦的, 以便后期与其他框架的适配.\
|
||||
得益于 Pickle, 状态机模块支持快照!
|
||||
|
||||
## Phaser - 全局阶段控制器
|
||||
|
||||
在一次队列记忆流程中, Phaser 代表记忆流程本身.
|
||||
|
||||
### 属性
|
||||
|
||||
#### 状态属性
|
||||
|
||||
其有状态属性:
|
||||
|
||||
- unsure - 用于初始化
|
||||
- \*quick_review - 复习逾期的单元
|
||||
- \*recognition - 辨识新单元
|
||||
- \*final_review - 复习所有逾期的和新辨认的单元
|
||||
- finished - 表示完成
|
||||
|
||||
> 逾期的: 指 SM-2 算法间隔显示应该复习的单元
|
||||
|
||||
带 * 的属性表示实际的记忆阶段, 由 repo 中 schedule.toml 中 schedule 列表显式声明, 运行过程中可以选择性执行, "空的" Procession 会被直接跳过.
|
||||
|
||||
在初始化 Procession 时, 每个 Procession 被赋予一个不重复的状态属性 作为"阶段状态"属性, 以此标识 Procession 的阶段属性, 因为每个 Procession 管理一个阶段下的复习进程.
|
||||
|
||||
你可以用 state 属性获取 Phaser 的当前状态.
|
||||
|
||||
#### Procession 属性
|
||||
|
||||
储存一个顺序列表, 保存所有构造的 Procession.\
|
||||
顺序与 repo 中 schedule.toml 中 schedule 列表中的顺序完全相同
|
||||
|
||||
### 初始化
|
||||
|
||||
Phaser 接受一个存储 Atom 对象的列表, 作为组织记忆流程的材料\
|
||||
在内部, 根据是否激活将其分为 new_atoms 与 old_atoms.\
|
||||
因此, 如果你传入的列表中有算法上"无所事事"的 Atom, 流程会对其进行"加强复习"
|
||||
由此创建 Procession.
|
||||
|
||||
### 直接输出呈现形式
|
||||
|
||||
Phaser 的 __repr__ 定义了此对象"官方的显示"用作直观的调试.\
|
||||
其以 ascii 表格形式输出, 格式也符合 markdown 表格规范, 你可以直接复制到 markdown.\
|
||||
示例:
|
||||
|
||||
```text
|
||||
| Type | State | Processions | Current Procession |
|
||||
|:-------|:--------|:-----------------------|:---------------------|
|
||||
| Phaser | unsure | ['新记忆', '总体复习'] | 新记忆 |
|
||||
```
|
||||
|
||||
| Type | State | Processions | Current Procession |
|
||||
|:-------|:--------|:-----------------------|:---------------------|
|
||||
| Phaser | unsure | ['新记忆', '总体复习'] | 新记忆 |
|
||||
|
||||
### 方法
|
||||
|
||||
作为一个 Transition Machine 对象的继承, 其拥有 Machine 对象拥有的所有方法.\
|
||||
除此之外, 它也拥有一些其他方法.
|
||||
|
||||
#### current_procession(self)
|
||||
|
||||
用于查询当前的 Procession, 并且根据当前 Procession 更新自身状态.\
|
||||
返回一个 Procession 对象, 是当前阶段的 Procession.\
|
||||
内部运作是返回第一个状态不为 finished 的 Procession, 并将自身状态变更为 Procession 的"阶段状态"属性\
|
||||
若所有 Procession 都已完成, 将返回一个"阶段状态"为 finished 的 Procession 占位符对象(它不在 procession 属性中), 并更新自身状态为 finished.
|
||||
|
||||
## Procession - 阶段管理器
|
||||
|
||||
### 属性
|
||||
|
||||
#### 状态属性
|
||||
|
||||
其有状态属性:
|
||||
|
||||
- active - 标识未完成, 初始化的默认属性
|
||||
- finished - 完成了
|
||||
|
||||
#### 其他属性
|
||||
|
||||
- current_atom: 当前记忆原子的引用
|
||||
- atoms: 队列中所有原子列表
|
||||
- cursor: 指针, 是当前原子在 atoms 列表中的索引
|
||||
- phase: "阶段属性"
|
||||
|
||||
> 注意区分 "Phaser" 和 "Phase", 其中 "Phase" 表示 "Phaser State".
|
||||
|
||||
- name\_: 阶段的命名
|
||||
- state: 当前状态属性
|
||||
|
||||
### 初始化
|
||||
|
||||
接受一个 atoms 列表与 phase_state (PhaserState Enum 类型)对象
|
||||
|
||||
### 直接输出呈现形式
|
||||
|
||||
同 Phaser, 但显示数据有所不同\
|
||||
与 Phaser 不同, Procession 显示队列会对过长的 atom.ident 进行缩略(末尾 `>` 符号)
|
||||
|
||||
```text
|
||||
| Type | Name | State | Progress | Queue | Current Atom |
|
||||
|:-----------|:-------|:--------|:-----------|:-----------------------|:------------------------------|
|
||||
| Procession | 新记忆 | active | 1 / 2 | ['秦孝公>', '君臣固>'] | 秦孝公据崤函之固, 拥雍州之地, |
|
||||
```
|
||||
|
||||
| Type | Name | State | Progress | Queue | Current Atom |
|
||||
|:-----------|:-------|:--------|:-----------|:-----------------------|:------------------------------|
|
||||
| Procession | 新记忆 | active | 1 / 2 | ['秦孝公>', '君臣固>'] | 秦孝公据崤函之固, 拥雍州之地, |
|
||||
|
||||
### 方法
|
||||
|
||||
作为一个 Transition Machine 对象的继承, 其拥有 Machine 对象拥有的所有方法.\
|
||||
除此之外, 它也拥有一些其他方法.
|
||||
|
||||
#### forward(self, step=1)
|
||||
|
||||
移动 cursor 并依情况更新 current_atom 和状态属性\
|
||||
无论 Procession 是否处于完成状态, forward 操作都是可逆的, 你可以传入负数, 此时已完成的 Procession 会自动"重启".
|
||||
|
||||
#### append(self, atom=None)
|
||||
|
||||
追加(回忆失败的)原子(默认为当前原子, 传入 None 会自动转化为当前原子)到队列末端\
|
||||
如果这个原子已经处于队列末端, 不会重复追加, 除非队列只剩下这个原子还没完成(此时最多重复两个)
|
||||
|
||||
#### process(self)
|
||||
|
||||
返回 cursor 值
|
||||
|
||||
#### __len__(self)
|
||||
|
||||
返回剩余原子量(而不是原子总量)\
|
||||
可以使用 len 函数调用
|
||||
获取原子总量请用 len(obj.atoms), 或者 total_length(self) 方法
|
||||
|
||||
#### total_length(self)
|
||||
|
||||
返回队列原子总量
|
||||
|
||||
#### is_empty(self)
|
||||
|
||||
判断是否为空队列(传入原子列表对象是空列表的队列)
|
||||
|
||||
#### get_fission(self)
|
||||
|
||||
获取当前原子的 Fission 对象, 用于单原子调度展开
|
||||
|
||||
## Fission - 单原子调度控制器
|
||||
|
||||
### 属性
|
||||
|
||||
#### 状态属性
|
||||
|
||||
- exammode: 测试模式(默认)
|
||||
- retronly: 仅回顾模式
|
||||
|
||||
#### 其他属性
|
||||
|
||||
- cursor
|
||||
- atom
|
||||
- current_puzzle
|
||||
- orbital_schedule
|
||||
- orbital_puzzles
|
||||
- puzzles
|
||||
|
||||
### 初始化
|
||||
|
||||
接受 atom 对象和 phase 参数
|
||||
|
||||
### 方法
|
||||
|
||||
#### get_puzzles(self)
|
||||
@@ -5,4 +5,8 @@ from .phaser import Phaser
|
||||
from .procession import Procession
|
||||
from .states import PhaserState, ProcessionState
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
__all__ = ["PhaserState", "ProcessionState", "Procession", "Fission", "Phaser"]
|
||||
|
||||
logger.debug("反应堆模块已加载")
|
||||
|
||||
@@ -1,129 +1,45 @@
|
||||
import random
|
||||
from functools import reduce
|
||||
|
||||
from tabulate import tabulate as tabu
|
||||
from transitions import Machine
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as puz
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .states import FissionState, PhaserState
|
||||
|
||||
logger = get_logger(__name__)
|
||||
from .states import PhaserState
|
||||
|
||||
|
||||
class Fission(Machine):
|
||||
"""单原子调度展开器"""
|
||||
class Fission:
|
||||
"""裂变器: 单原子调度展开器"""
|
||||
|
||||
def __init__(self, atom: pt.Atom, phase=PhaserState.RECOGNITION):
|
||||
self.phase = phase
|
||||
self.cursor = 0
|
||||
self.logger = get_logger(__name__)
|
||||
self.atom = atom
|
||||
self.current_puzzle_inf: dict
|
||||
# phase 为 PhaserState 枚举实例, 需要获取其value
|
||||
phase_value = phase.value
|
||||
states = [
|
||||
{"name": FissionState.EXAMMODE.value},
|
||||
{"name": FissionState.RETRONLY.value},
|
||||
]
|
||||
|
||||
transitions = [
|
||||
{
|
||||
"trigger": "finish",
|
||||
"source": FissionState.EXAMMODE.value,
|
||||
"dest": FissionState.RETRONLY.value,
|
||||
},
|
||||
]
|
||||
if phase == PhaserState.FINISHED:
|
||||
Machine.__init__(
|
||||
self,
|
||||
states=states,
|
||||
transitions=transitions,
|
||||
initial=FissionState.EXAMMODE.value,
|
||||
)
|
||||
return
|
||||
orbital_schedule = atom.registry["orbital"]["phases"][phase_value] # type: ignore
|
||||
orbital_puzzles = atom.registry["nucleon"]["puzzles"]
|
||||
self.puzzles_inf = list()
|
||||
self.min_ratings = []
|
||||
for item, possibility in orbital_schedule: # type: ignore
|
||||
logger.debug(f"开始处理: {item}")
|
||||
|
||||
puzzle = puz.puzzles[orbital_puzzles[item]["__origin__"]]
|
||||
|
||||
# print(f"{phase.value}")
|
||||
self.orbital_schedule = atom.registry["orbital"]["schedule"][phase.value] # type: ignore
|
||||
self.orbital_puzzles = atom.registry["orbital"]["puzzles"]
|
||||
# print(self.orbital_schedule)
|
||||
self.puzzles = list()
|
||||
for item, possibility in self.orbital_schedule: # type: ignore
|
||||
print(f"ad:{item}")
|
||||
self.logger.debug(f"开始处理 orbital 项: {item}")
|
||||
if not isinstance(possibility, float):
|
||||
possibility = float(possibility)
|
||||
|
||||
while possibility > 1:
|
||||
self.puzzles_inf.append(
|
||||
self.puzzles.append(
|
||||
{
|
||||
"puzzle": puzzle,
|
||||
"puzzle": puz.puzzles[self.orbital_puzzles[item]["__origin__"]],
|
||||
"alia": item,
|
||||
}
|
||||
)
|
||||
possibility -= 1
|
||||
|
||||
if random.random() <= possibility:
|
||||
self.puzzles_inf.append(
|
||||
self.puzzles.append(
|
||||
{
|
||||
"puzzle": puzzle,
|
||||
"puzzle": puz.puzzles[self.orbital_puzzles[item]["__origin__"]],
|
||||
"alia": item,
|
||||
}
|
||||
)
|
||||
if self.puzzles_inf:
|
||||
self.current_puzzle_inf = self.puzzles_inf[0]
|
||||
print(f"ok:{item}")
|
||||
self.logger.debug(f"orbital 项处理完成: {item}")
|
||||
|
||||
for i in range(len(self.puzzles_inf)):
|
||||
self.min_ratings.append(float('inf'))
|
||||
|
||||
Machine.__init__(
|
||||
self,
|
||||
states=states,
|
||||
transitions=transitions,
|
||||
initial=FissionState.EXAMMODE.value,
|
||||
)
|
||||
|
||||
def get_puzzles_inf(self):
|
||||
if self.state == "retronly":
|
||||
return [{"puzzle": puz.puzzles["recognition"], "alia": "Recognition"}]
|
||||
return self.puzzles_inf
|
||||
|
||||
def get_current_puzzle_inf(self):
|
||||
if self.state == "retronly":
|
||||
return {"puzzle": puz.puzzles["recognition"], "alia": "Recognition"}
|
||||
return self.current_puzzle_inf
|
||||
|
||||
def report(self, rating):
|
||||
if self.puzzles_inf:
|
||||
self.min_ratings[self.cursor] = min(rating, self.min_ratings[self.cursor])
|
||||
|
||||
def get_quality(self):
|
||||
if self.puzzles_inf:
|
||||
if self.is_state("retronly", self):
|
||||
return reduce(lambda x, y: min(x, y), self.min_ratings)
|
||||
raise IndexError
|
||||
|
||||
def forward(self, step=1):
|
||||
"""将谜题指针向前移动并依情况更新或完成"""
|
||||
self.cursor += step
|
||||
if self.cursor >= len(self.puzzles_inf):
|
||||
if self.state != "retronly":
|
||||
self.finish()
|
||||
else:
|
||||
self.current_puzzle_inf = self.puzzles_inf[self.cursor]
|
||||
|
||||
def __repr__(self, style="pipe", ends="\n") -> str:
|
||||
from heurams.services.textproc import truncate
|
||||
|
||||
dic = [
|
||||
{
|
||||
"Type": "Fission",
|
||||
"Atom": truncate(self.atom.ident),
|
||||
"State": self.state,
|
||||
"Progress": f"{self.cursor + 1} / {len(self.puzzles_inf)}",
|
||||
"Queue": list(map(lambda f: truncate(f["alia"]), self.puzzles_inf)),
|
||||
"Current Puzzle": f"{self.current_puzzle_inf['alia']}@{self.current_puzzle_inf['puzzle'].__name__}", # type: ignore
|
||||
}
|
||||
]
|
||||
return str(tabu(dic, headers="keys", tablefmt=style)) + ends
|
||||
def generate(self):
|
||||
yield from self.puzzles
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from transitions import Machine
|
||||
# 移相器类定义
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
from heurams.kernel.particles.placeholders import AtomPlaceholder
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .procession import Procession
|
||||
@@ -10,139 +9,43 @@ from .states import PhaserState, ProcessionState
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Phaser(Machine):
|
||||
"""全局调度阶段管理器"""
|
||||
class Phaser:
|
||||
"""移相器: 全局调度阶段管理器"""
|
||||
|
||||
def __init__(self, atoms: list[pt.Atom]) -> None:
|
||||
logger.debug("Phaser.__init__: 原子数量=%d", len(atoms))
|
||||
|
||||
self.atoms = atoms
|
||||
new_atoms = list()
|
||||
old_atoms = list()
|
||||
|
||||
self.state = PhaserState.UNSURE
|
||||
for i in atoms:
|
||||
if not i.registry["electron"].is_activated():
|
||||
new_atoms.append(i)
|
||||
else:
|
||||
old_atoms.append(i)
|
||||
|
||||
logger.debug("新原子数量=%d, 旧原子数量=%d", len(new_atoms), len(old_atoms))
|
||||
|
||||
self.processions = list()
|
||||
# TODO: 改进为基于配置文件的可选复习阶段
|
||||
if len(old_atoms):
|
||||
self.processions.append(
|
||||
Procession(old_atoms, PhaserState.QUICK_REVIEW, "初始复习")
|
||||
)
|
||||
logger.debug("创建初始复习 Procession")
|
||||
|
||||
if len(new_atoms):
|
||||
self.processions.append(
|
||||
Procession(new_atoms, PhaserState.RECOGNITION, "新记忆")
|
||||
)
|
||||
logger.debug("创建新记忆 Procession")
|
||||
|
||||
self.processions.append(Procession(atoms, PhaserState.FINAL_REVIEW, "总体复习"))
|
||||
logger.debug("创建总体复习 Procession")
|
||||
logger.debug("Phaser 初始化完成, processions 数量=%d", len(self.processions))
|
||||
|
||||
# 设置transitions状态机
|
||||
states = [
|
||||
{"name": PhaserState.UNSURE.value, "on_enter": "on_unsure"},
|
||||
{"name": PhaserState.QUICK_REVIEW.value, "on_enter": "on_quick_review"},
|
||||
{"name": PhaserState.RECOGNITION.value, "on_enter": "on_recognition"},
|
||||
{"name": PhaserState.FINAL_REVIEW.value, "on_enter": "on_final_review"},
|
||||
{"name": PhaserState.FINISHED.value, "on_enter": "on_finished"},
|
||||
]
|
||||
|
||||
transitions = [
|
||||
{"trigger": "to_unsure", "source": "*", "dest": PhaserState.UNSURE.value},
|
||||
{
|
||||
"trigger": "to_quick_review",
|
||||
"source": "*",
|
||||
"dest": PhaserState.QUICK_REVIEW.value,
|
||||
},
|
||||
{
|
||||
"trigger": "to_recognition",
|
||||
"source": "*",
|
||||
"dest": PhaserState.RECOGNITION.value,
|
||||
},
|
||||
{
|
||||
"trigger": "to_final_review",
|
||||
"source": "*",
|
||||
"dest": PhaserState.FINAL_REVIEW.value,
|
||||
},
|
||||
{
|
||||
"trigger": "to_finished",
|
||||
"source": "*",
|
||||
"dest": PhaserState.FINISHED.value,
|
||||
},
|
||||
]
|
||||
|
||||
Machine.__init__(
|
||||
self,
|
||||
states=states,
|
||||
transitions=transitions,
|
||||
initial=PhaserState.UNSURE.value,
|
||||
)
|
||||
|
||||
self.to_unsure()
|
||||
|
||||
def on_unsure(self):
|
||||
"""进入UNSURE状态时的回调"""
|
||||
logger.debug("Phaser 进入 UNSURE 状态")
|
||||
|
||||
def on_quick_review(self):
|
||||
"""进入QUICK_REVIEW状态时的回调"""
|
||||
logger.debug("Phaser 进入 QUICK_REVIEW 状态")
|
||||
|
||||
def on_recognition(self):
|
||||
"""进入RECOGNITION状态时的回调"""
|
||||
logger.debug("Phaser 进入 RECOGNITION 状态")
|
||||
|
||||
def on_final_review(self):
|
||||
"""进入FINAL_REVIEW状态时的回调"""
|
||||
logger.debug("Phaser 进入 FINAL_REVIEW 状态")
|
||||
|
||||
def on_finished(self):
|
||||
"""进入FINISHED状态时的回调"""
|
||||
for i in self.atoms:
|
||||
i.lock(1)
|
||||
i.revise()
|
||||
logger.debug("Phaser 进入 FINISHED 状态")
|
||||
|
||||
def current_procession(self):
|
||||
logger.debug("Phaser.current_procession 被调用")
|
||||
for i in self.processions:
|
||||
i: Procession
|
||||
if i.state != ProcessionState.FINISHED.value:
|
||||
# if i.phase == PhaserState.UNSURE: 此判断是不必要的 因为没有这种 Procession
|
||||
if i.phase == PhaserState.QUICK_REVIEW:
|
||||
self.to_quick_review()
|
||||
elif i.phase == PhaserState.RECOGNITION:
|
||||
self.to_recognition()
|
||||
elif i.phase == PhaserState.FINAL_REVIEW:
|
||||
self.to_final_review()
|
||||
|
||||
if not i.state == ProcessionState.FINISHED:
|
||||
self.state = i.phase
|
||||
logger.debug("找到未完成的 Procession: phase=%s", i.phase)
|
||||
return i
|
||||
|
||||
# 所有Procession都已完成
|
||||
self.to_finished()
|
||||
self.state = PhaserState.FINISHED
|
||||
logger.debug("所有 Procession 已完成, 状态设置为 FINISHED")
|
||||
return Procession([AtomPlaceholder()], PhaserState.FINISHED)
|
||||
|
||||
def __repr__(self, style="pipe", ends="\n"):
|
||||
from tabulate import tabulate as tabu
|
||||
|
||||
from heurams.services.textproc import truncate
|
||||
|
||||
lst = [
|
||||
{
|
||||
"Type": "Phaser",
|
||||
"State": self.state,
|
||||
"Processions": list(map(lambda f: (f.name_), self.processions)),
|
||||
"Current Procession": "None" if not self.current_procession() else self.current_procession().name_, # type: ignore
|
||||
},
|
||||
]
|
||||
return str(tabu(tabular_data=lst, headers="keys", tablefmt=style)) + ends
|
||||
return 0
|
||||
|
||||
@@ -1,101 +1,61 @@
|
||||
from tabulate import tabulate as tabu
|
||||
from transitions import Machine
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .fission import Fission
|
||||
from .states import PhaserState, ProcessionState
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Procession(Machine):
|
||||
class Procession:
|
||||
"""队列: 标识单次记忆流程"""
|
||||
|
||||
def __init__(self, atoms: list, phase_state: PhaserState, name_: str = ""):
|
||||
def __init__(self, atoms: list, phase: PhaserState, name: str = ""):
|
||||
logger.debug(
|
||||
"Procession.__init__: 原子数量=%d, phase=%s, name='%s'",
|
||||
len(atoms),
|
||||
phase_state.value,
|
||||
name_,
|
||||
phase.value,
|
||||
name,
|
||||
)
|
||||
self.current_atom: pt.Atom | None
|
||||
self.atoms = atoms
|
||||
self.current_atom = atoms[0] if atoms else None
|
||||
self.queue = atoms.copy()
|
||||
self.current_atom = atoms[0]
|
||||
self.cursor = 0
|
||||
self.name_ = name_
|
||||
self.phase = phase_state
|
||||
|
||||
states = [
|
||||
{"name": ProcessionState.ACTIVE.value, "on_enter": "on_active"},
|
||||
{"name": ProcessionState.FINISHED.value, "on_enter": "on_finished"},
|
||||
]
|
||||
|
||||
transitions = [
|
||||
{
|
||||
"trigger": "finish",
|
||||
"source": ProcessionState.ACTIVE.value,
|
||||
"dest": ProcessionState.FINISHED.value,
|
||||
},
|
||||
{
|
||||
"trigger": "restart",
|
||||
"source": ProcessionState.FINISHED.value,
|
||||
"dest": ProcessionState.ACTIVE.value,
|
||||
},
|
||||
]
|
||||
|
||||
Machine.__init__(
|
||||
self,
|
||||
states=states,
|
||||
transitions=transitions,
|
||||
initial=ProcessionState.ACTIVE.value,
|
||||
)
|
||||
|
||||
logger.debug("Procession 初始化完成, 队列长度=%d", len(self.atoms))
|
||||
|
||||
def on_active(self):
|
||||
"""进入active状态时的回调"""
|
||||
logger.debug("Procession 进入 active 状态")
|
||||
|
||||
def on_finished(self):
|
||||
"""进入FINISHED状态时的回调"""
|
||||
logger.debug("Procession 进入 FINISHED 状态")
|
||||
self.name = name
|
||||
self.phase = phase
|
||||
self.state: ProcessionState = ProcessionState.RUNNING
|
||||
logger.debug("Procession 初始化完成, 队列长度=%d", len(self.queue))
|
||||
|
||||
def forward(self, step=1):
|
||||
"""将记忆原子指针向前移动并依情况更新原子(返回 1)或完成队列(返回 0)"""
|
||||
logger.debug("Procession.forward: step=%d, 当前 cursor=%d", step, self.cursor)
|
||||
self.cursor += step
|
||||
if self.cursor >= len(self.atoms):
|
||||
if self.state != ProcessionState.FINISHED.value:
|
||||
self.finish() # 触发状态转换
|
||||
if self.cursor == len(self.queue):
|
||||
self.state = ProcessionState.FINISHED
|
||||
logger.debug("Procession 已完成")
|
||||
else:
|
||||
if self.state != ProcessionState.ACTIVE.value:
|
||||
self.restart() # 确保在active状态
|
||||
self.current_atom = self.atoms[self.cursor]
|
||||
self.state = ProcessionState.RUNNING
|
||||
try:
|
||||
logger.debug("cursor 更新为: %d", self.cursor)
|
||||
logger.debug(
|
||||
"当前原子更新为: %s",
|
||||
self.current_atom.ident if self.current_atom else "None",
|
||||
)
|
||||
self.current_atom = self.queue[self.cursor]
|
||||
logger.debug("当前原子更新为: %s", self.current_atom.ident)
|
||||
return 1 # 成功
|
||||
except IndexError as e:
|
||||
logger.debug("IndexError: %s", e)
|
||||
self.state = ProcessionState.FINISHED
|
||||
logger.debug("Procession 因索引错误而完成")
|
||||
return 0
|
||||
|
||||
def append(self, atom=None):
|
||||
"""追加(回忆失败的)原子(默认为当前原子)到队列末端"""
|
||||
if atom is None:
|
||||
if atom == None:
|
||||
atom = self.current_atom
|
||||
logger.debug("Procession.append: atom=%s", atom.ident if atom else "None")
|
||||
|
||||
if not self.atoms or self.atoms[-1] != atom or len(self) <= 1:
|
||||
self.atoms.append(atom)
|
||||
logger.debug("原子已追加到队列, 新队列长度=%d", len(self.atoms))
|
||||
if self.queue[len(self.queue) - 1] != atom or len(self) <= 1:
|
||||
self.queue.append(atom)
|
||||
logger.debug("原子已追加到队列, 新队列长度=%d", len(self.queue))
|
||||
else:
|
||||
logger.debug("原子未追加(重复或队列长度<=1)")
|
||||
|
||||
def __len__(self):
|
||||
if not self.atoms:
|
||||
return 0
|
||||
length = len(self.atoms) - self.cursor
|
||||
length = len(self.queue) - self.cursor
|
||||
logger.debug("Procession.__len__: 剩余长度=%d", length)
|
||||
return length
|
||||
|
||||
@@ -104,29 +64,11 @@ class Procession(Machine):
|
||||
return self.cursor
|
||||
|
||||
def total_length(self):
|
||||
total = len(self.atoms)
|
||||
total = len(self.queue)
|
||||
logger.debug("Procession.total_length: %d", total)
|
||||
return total
|
||||
|
||||
def is_empty(self):
|
||||
empty = len(self.atoms) == 0
|
||||
logger.debug("Procession.is_empty: %s", empty)
|
||||
empty = len(self.queue)
|
||||
logger.debug("Procession.is_empty: %d", empty)
|
||||
return empty
|
||||
|
||||
def get_fission(self):
|
||||
return Fission(atom=self.current_atom, phase=self.phase) # type: ignore
|
||||
|
||||
def __repr__(self, style="pipe", ends="\n"):
|
||||
from heurams.services.textproc import truncate
|
||||
|
||||
dic = [
|
||||
{
|
||||
"Type": "Procession",
|
||||
"Name": self.name_,
|
||||
"State": self.state,
|
||||
"Progress": f"{self.cursor + 1} / {len(self.atoms)}",
|
||||
"Queue": list(map(lambda f: truncate(f.ident), self.atoms)),
|
||||
"Current Atom": self.current_atom.ident, # type: ignore
|
||||
}
|
||||
]
|
||||
return str(tabu(dic, headers="keys", tablefmt=style)) + ends
|
||||
|
||||
@@ -14,13 +14,8 @@ class PhaserState(Enum):
|
||||
|
||||
|
||||
class ProcessionState(Enum):
|
||||
ACTIVE = "active"
|
||||
FINISHED = "finished"
|
||||
|
||||
|
||||
class FissionState(Enum):
|
||||
EXAMMODE = "exammode"
|
||||
RETRONLY = "retronly"
|
||||
RUNNING = auto()
|
||||
FINISHED = auto()
|
||||
|
||||
|
||||
logger.debug("状态枚举定义已加载")
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
from .repo import Repo, RepoManifest
|
||||
|
||||
__all__ = ["Repo", "RepoManifest"]
|
||||
@@ -1,176 +0,0 @@
|
||||
import json
|
||||
from functools import reduce
|
||||
from pathlib import Path
|
||||
from typing import TypedDict
|
||||
|
||||
import toml
|
||||
|
||||
import heurams.kernel.particles as pt
|
||||
|
||||
from heurams.kernel.auxiliary.lict import Lict
|
||||
|
||||
|
||||
class RepoManifest(TypedDict):
|
||||
title: str
|
||||
author: str
|
||||
desc: str
|
||||
|
||||
|
||||
class Repo:
|
||||
file_mapping = {
|
||||
"schedule": "schedule.toml",
|
||||
"payload": "payload.toml",
|
||||
"algodata": "algodata.json",
|
||||
"manifest": "manifest.toml",
|
||||
"typedef": "typedef.toml",
|
||||
}
|
||||
|
||||
type_mapping = {
|
||||
"schedule": "dict",
|
||||
"payload": "lict",
|
||||
"algodata": "lict",
|
||||
"manifest": "dict",
|
||||
"typedef": "dict",
|
||||
}
|
||||
|
||||
default_save_list = ["algodata"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
schedule: dict,
|
||||
payload: Lict,
|
||||
manifest: dict,
|
||||
typedef: dict,
|
||||
algodata: Lict,
|
||||
source=None,
|
||||
) -> None:
|
||||
self.schedule: dict = schedule
|
||||
self.manifest: RepoManifest = manifest # type: ignore
|
||||
self.typedef: dict = typedef
|
||||
self.payload: Lict = payload
|
||||
self.algodata: Lict = algodata
|
||||
self.source: Path | None = source # 若存在, 指向 repo 所在 dir
|
||||
self.database = {
|
||||
"schedule": self.schedule,
|
||||
"payload": self.payload,
|
||||
"manifest": self.manifest,
|
||||
"typedef": self.typedef,
|
||||
"algodata": self.algodata,
|
||||
"source": self.source,
|
||||
}
|
||||
self.generate_particles_data()
|
||||
|
||||
def generate_particles_data(self):
|
||||
|
||||
self.nucleonic_data_lict = Lict(
|
||||
initlist=list(map(self._nucleonic_proc, self.payload))
|
||||
)
|
||||
self.orbitic_data = self.schedule
|
||||
self.ident_index = self.nucleonic_data_lict.keys()
|
||||
for i in self.ident_index:
|
||||
self.algodata.append_new((i, {}))
|
||||
self.electronic_data_lict = self.algodata
|
||||
|
||||
def _nucleonic_proc(self, unit):
|
||||
ident = unit[0]
|
||||
common = self.typedef["common"]
|
||||
return (ident, (unit[1], common))
|
||||
|
||||
@staticmethod
|
||||
def _merge(value):
|
||||
def inner(x):
|
||||
return (x, value)
|
||||
|
||||
return inner
|
||||
|
||||
def __len__(self):
|
||||
return len(self.payload)
|
||||
|
||||
def __repr__(self):
|
||||
from pprint import pformat
|
||||
|
||||
s = pformat(self.database, indent=4)
|
||||
return s
|
||||
|
||||
def persist_to_repodir(
|
||||
self, save_list: list | None = None, source: Path | None = None
|
||||
):
|
||||
if save_list == None:
|
||||
save_list = self.default_save_list
|
||||
if self.source != None and source == None:
|
||||
source = self.source
|
||||
if source == None:
|
||||
raise FileNotFoundError("不存在仓库到文件的映射")
|
||||
source.mkdir(parents=True, exist_ok=True)
|
||||
for keyname in save_list:
|
||||
filename = self.file_mapping[keyname]
|
||||
with open(source / filename, "w") as f:
|
||||
try:
|
||||
dict_data = self.database[keyname].dicted_data
|
||||
except:
|
||||
dict_data = dict(self.database[keyname])
|
||||
if filename.endswith("toml"):
|
||||
toml.dump(dict_data, f)
|
||||
elif filename.endswith("json"):
|
||||
json.dump(dict_data, f, ensure_ascii=False, indent=4)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件类型: {filename}")
|
||||
|
||||
def export_to_single_dict(self):
|
||||
return self.database
|
||||
|
||||
@classmethod
|
||||
def create_new_repo(cls, source=None):
|
||||
default_database = {
|
||||
"schedule": {},
|
||||
"payload": Lict([]),
|
||||
"algodata": Lict([]),
|
||||
"manifest": {},
|
||||
"typedef": {},
|
||||
"source": source,
|
||||
}
|
||||
return Repo(**default_database)
|
||||
|
||||
@classmethod
|
||||
def create_from_repodir(cls, source: Path):
|
||||
database = {}
|
||||
for keyname, filename in cls.file_mapping.items():
|
||||
with open(source / filename, "r") as f:
|
||||
loaded: dict
|
||||
if filename.endswith("toml"):
|
||||
loaded = toml.load(f)
|
||||
elif filename.endswith("json"):
|
||||
loaded = json.load(f)
|
||||
else:
|
||||
raise ValueError(f"不支持的文件类型: {filename}")
|
||||
if cls.type_mapping[keyname] == "lict":
|
||||
database[keyname] = Lict(list(loaded.items()))
|
||||
elif cls.type_mapping[keyname] == "dict":
|
||||
database[keyname] = loaded
|
||||
else:
|
||||
raise ValueError(f"不支持的数据容器: {cls.type_mapping[keyname]}")
|
||||
database["source"] = source
|
||||
return Repo(**database)
|
||||
|
||||
@classmethod
|
||||
def create_from_single_dict(cls, dictdata, source: Path | None = None):
|
||||
database = dictdata
|
||||
database["source"] = source
|
||||
return Repo(**database)
|
||||
|
||||
@classmethod
|
||||
def check_repodir(cls, source: Path):
|
||||
try:
|
||||
cls.create_from_repodir(source)
|
||||
return 1
|
||||
except:
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
def probe_valid_repos_in_dir(cls, folder: Path):
|
||||
lst = list()
|
||||
for i in folder.iterdir():
|
||||
if i.is_dir():
|
||||
if cls.check_repodir(i):
|
||||
lst.append(i)
|
||||
return lst
|
||||
@@ -1,3 +1,2 @@
|
||||
# Provider - 提供者
|
||||
|
||||
底层相关与第三方 API 接口包装
|
||||
@@ -0,0 +1,13 @@
|
||||
import pathlib
|
||||
from typing import Protocol
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class PlayFunctionProtocol(Protocol):
|
||||
def __call__(self, path: pathlib.Path) -> None: ...
|
||||
|
||||
|
||||
logger.debug("音频协议模块已加载")
|
||||
@@ -1,19 +1,6 @@
|
||||
# 大语言模型
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .base import BaseLLM
|
||||
from .openai import OpenAILLM
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
__all__ = [
|
||||
"BaseLLM",
|
||||
"OpenAILLM",
|
||||
]
|
||||
|
||||
providers = {
|
||||
"base": BaseLLM,
|
||||
"openai": OpenAILLM,
|
||||
}
|
||||
|
||||
logger.debug("LLM providers 已注册: %s", list(providers.keys()))
|
||||
logger.debug("LLM providers 模块已加载")
|
||||
|
||||
@@ -1,55 +1,5 @@
|
||||
"""LLM 提供者基类"""
|
||||
|
||||
import asyncio
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class BaseLLM:
|
||||
"""LLM 提供者基类"""
|
||||
|
||||
name = "BaseLLM"
|
||||
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
"""初始化 LLM 提供者
|
||||
|
||||
Args:
|
||||
config: 提供者配置字典
|
||||
"""
|
||||
self.config = config
|
||||
logger.debug("BaseLLM 初始化完成")
|
||||
|
||||
async def chat(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
||||
"""发送聊天消息并获取响应
|
||||
|
||||
Args:
|
||||
messages: 消息列表,每个消息为 {"role": "user"|"assistant"|"system", "content": "消息内容"}
|
||||
**kwargs: 其他参数,如 temperature, max_tokens 等
|
||||
|
||||
Returns:
|
||||
模型返回的文本响应
|
||||
"""
|
||||
logger.debug("BaseLLM.chat: messages=%d, kwargs=%s", len(messages), kwargs)
|
||||
logger.warning("BaseLLM.chat 是基类方法,未实现具体功能")
|
||||
await asyncio.sleep(0) # 避免未使用异步的警告
|
||||
return "BaseLLM 未实现具体功能"
|
||||
|
||||
async def chat_stream(self, messages: List[Dict[str, str]], **kwargs):
|
||||
"""流式聊天(可选实现)
|
||||
|
||||
Args:
|
||||
messages: 消息列表
|
||||
**kwargs: 其他参数
|
||||
|
||||
Yields:
|
||||
流式响应的文本块
|
||||
"""
|
||||
logger.debug(
|
||||
"BaseLLM.chat_stream: messages=%d, kwargs=%s", len(messages), kwargs
|
||||
)
|
||||
logger.warning("BaseLLM.chat_stream 是基类方法,未实现具体功能")
|
||||
await asyncio.sleep(0)
|
||||
yield "BaseLLM 未实现流式功能"
|
||||
logger.debug("LLM 基类模块已加载")
|
||||
|
||||
@@ -1,96 +1,5 @@
|
||||
"""OpenAI 兼容 LLM 提供者"""
|
||||
|
||||
import asyncio
|
||||
from typing import Any, AsyncGenerator, Dict, List, Optional
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .base import BaseLLM
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class OpenAILLM(BaseLLM):
|
||||
"""OpenAI 兼容 LLM 提供者"""
|
||||
|
||||
name = "OpenAI"
|
||||
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
super().__init__(config)
|
||||
self.api_key = config.get("key", "")
|
||||
self.base_url = config.get("url", "https://api.openai.com/v1")
|
||||
self._client = None
|
||||
logger.debug("OpenAILLM 初始化完成: base_url=%s", self.base_url)
|
||||
|
||||
def _get_client(self):
|
||||
"""获取 OpenAI 客户端(延迟导入)"""
|
||||
if self._client is None:
|
||||
try:
|
||||
from openai import AsyncOpenAI
|
||||
except ImportError:
|
||||
logger.error("未安装 openai 库,请运行: pip install openai")
|
||||
raise ImportError("未安装 openai 库,请运行: pip install openai")
|
||||
|
||||
self._client = AsyncOpenAI(
|
||||
api_key=self.api_key if self.api_key else None,
|
||||
base_url=self.base_url if self.base_url else None,
|
||||
)
|
||||
return self._client
|
||||
|
||||
async def chat(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
||||
"""发送聊天消息并获取响应"""
|
||||
logger.debug("OpenAILLM.chat: messages=%d", len(messages))
|
||||
|
||||
client = self._get_client()
|
||||
|
||||
# 默认参数
|
||||
default_kwargs = {
|
||||
"model": kwargs.get("model", "gpt-3.5-turbo"),
|
||||
"temperature": kwargs.get("temperature", 0.7),
|
||||
"max_tokens": kwargs.get("max_tokens", 1000),
|
||||
}
|
||||
|
||||
# 合并参数,优先使用传入的 kwargs
|
||||
request_kwargs = {**default_kwargs, **kwargs}
|
||||
request_kwargs["messages"] = messages
|
||||
|
||||
try:
|
||||
response = await client.chat.completions.create(**request_kwargs)
|
||||
content = response.choices[0].message.content
|
||||
logger.debug(
|
||||
"OpenAILLM.chat 成功: response length=%d",
|
||||
len(content) if content else 0,
|
||||
)
|
||||
return content or ""
|
||||
except Exception as e:
|
||||
logger.error("OpenAILLM.chat 失败: %s", e)
|
||||
raise
|
||||
|
||||
async def chat_stream(
|
||||
self, messages: List[Dict[str, str]], **kwargs
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""流式聊天"""
|
||||
logger.debug("OpenAILLM.chat_stream: messages=%d", len(messages))
|
||||
|
||||
client = self._get_client()
|
||||
|
||||
# 默认参数
|
||||
default_kwargs = {
|
||||
"model": kwargs.get("model", "gpt-3.5-turbo"),
|
||||
"temperature": kwargs.get("temperature", 0.7),
|
||||
"max_tokens": kwargs.get("max_tokens", 1000),
|
||||
"stream": True,
|
||||
}
|
||||
|
||||
# 合并参数
|
||||
request_kwargs = {**default_kwargs, **kwargs}
|
||||
request_kwargs["messages"] = messages
|
||||
|
||||
try:
|
||||
stream = await client.chat.completions.create(**request_kwargs)
|
||||
async for chunk in stream:
|
||||
if chunk.choices[0].delta.content:
|
||||
yield chunk.choices[0].delta.content
|
||||
except Exception as e:
|
||||
logger.error("OpenAILLM.chat_stream 失败: %s", e)
|
||||
raise
|
||||
logger.debug("OpenAI provider 模块已加载(未实现)")
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
# Services - 服务
|
||||
|
||||
基础服务相关代码
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user