目录
[Grammar correction](#Grammar correction)
[1. 云端 DeepSeek](#1. 云端 DeepSeek)
[2. 本地 Ollama](#2. 本地 Ollama)
Grammar correction
在 Word、浏览器、邮件或任何能选中文字的地方,选中一句话,按 Ctrl+Alt+G, 用 DeepSeek 云端 API 或 本机 Ollama 检查语法、拼写、标点和用词, 并在置顶窗口中流式显示「结论 / 问题 / 修改后 / 提示」。

Windows、Ubuntu 20.04+、macOS 均可运行, 也可以打包成单文件程序。
功能
- 划词检查:在任意应用选中文字,按全局快捷键即可检查。
- 固定窗口 :置顶、可调透明度、记忆窗口大小;粘贴文本后
Ctrl+Enter检查。 - 双引擎 :DeepSeek(
deepseek-chat/deepseek-reasoner)或本地 Ollama。 - 流式输出:结果边生成边显示,可随时停止。
- 两种模式:仅语法检查;语法 + 表达优化。
- 结果处理:复制完整报告,或只复制「修改后」的文本。
- 提示词自定义 :支持
{``{selectedText}}占位符。 - 命令行 :
--check-text、--check-clipboard、--list-ollama,适合脚本或 Wayland 快捷键。
环境要求
| 平台 | 要求 |
|---|---|
| Windows | Windows 10 1809+ / 11,Python 3.8+(官方安装包自带 Tk) |
| Ubuntu / Debian | Ubuntu 20.04+,sudo apt install python3-tk |
| macOS | macOS 10.15+,Python 3.8+(python.org 安装包自带 Tk) |
无需 pip install 即可运行。可选安装:
python3 -m pip install keyring # 把 API Key 存进系统钥匙串
python3 -m pip install pynput # macOS 全局快捷键(Windows/Linux 不需要)
快速开始
# Linux / macOS
cd "Grammar correction"
./run.sh
# 或
python3 grammar_correction.py
Windows:
cd /d "D:\path\to\Grammar correction"
run.bat
首次启动会提示配置模型。
1. 云端 DeepSeek
- 打开 DeepSeek,创建 API Key;
- 应用 → 设置 → 云端 DeepSeek;
- 粘贴 Key → 保存;
- 模型选
deepseek-chat(日常)或deepseek-reasoner(复杂文本); - 点 测试连接。
Key 优先保存到系统钥匙串;没有可用钥匙串时,会写入 config.json(权限 0600), 界面会明确提示。也可以用环境变量覆盖:
DEEPSEEK_API_KEY=sk-xxx python3 grammar_correction.py
2. 本地 Ollama
# 安装(Linux;Windows/macOS 用官网安装包)
curl -fsSL https://ollama.com/install.sh | sh
# 启动
ollama serve
# 下载一个对话模型
ollama pull qwen2.5:7b
# 确认服务
curl http://127.0.0.1:11434/api/version
应用 → 设置 → 本地 Ollama → 检测本地模型 → 选模型 → 测试连接 。 程序只把 capabilities 含 completion 的模型放进下拉框,自动排除 embedding/rerank 模型。
推荐模型:
| 模型 | 体积 | 说明 |
|---|---|---|
qwen2.5:7b |
~4.7 GB | 默认推荐,中英文都不错 |
qwen2.5:3b |
~2 GB | 轻量快 |
llama3.2:3b |
~2 GB | 英文够用 |
llama3.1:8b |
~4.9 GB | 英文表达更自然 |
使用方法
划词检查
- 在 Word / 浏览器 / 邮件里选中一句话;
- 按
Ctrl+Alt+G; - 工具窗口出现在鼠标附近并置顶,自动填入原文、开始检查;
- 点 复制修改后文本;
- 回到原文
Ctrl+V替换; - 按
Esc最小化窗口。
取词方式:
- Windows :保存剪贴板文本 → 模拟
Ctrl+C→ 读回 → 恢复剪贴板。 只恢复文本;图片/富文本格式无法保留。 - Linux X11:直接读取 PRIMARY 选区,不修改剪贴板。
- macOS :模拟
⌘C,需要在「系统设置 → 隐私与安全性 → 辅助功能」中授权。
固定窗口
勾选 固定窗口置顶 ,把文本粘贴到输入框,按 Ctrl+Enter 检查。 窗口大小、置顶状态、透明度会记住。适合划词快捷键不可用、或者想边改边查的场景。
快捷键
| 快捷键 | 作用 |
|---|---|
Ctrl+Alt+G |
检查当前选区(可在设置里改) |
Ctrl+Enter |
在输入框内检查 |
Esc |
检查中停止;否则最小化 / 返回检查页 |
Ctrl+V |
粘贴 |
命令行
不创建主窗口,适合脚本或 Wayland 自定义快捷键:
python3 grammar_correction.py --check-text "She go to school every day."
python3 grammar_correction.py --check-clipboard
python3 grammar_correction.py --list-ollama
python3 grammar_correction.py --hidden
python3 grammar_correction.py --portable
python3 grammar_correction.py --config ./config.json
Wayland 下建议在桌面环境里绑定系统快捷键到:
python3 /path/to/grammar_correction.py --check-clipboard
配置与隐私
配置文件位置:
| 平台 | 路径 |
|---|---|
| Windows | %APPDATA%\GrammarCorrection\config.json |
| Linux | ${XDG_CONFIG_HOME:-~/.config}/grammar-correction/config.json |
| macOS | ~/Library/Application Support/GrammarCorrection/config.json |
| 绿色版 | <程序目录>/data/config.json |
- 普通设置写入
config.json,原子写、权限0600; - API Key 优先系统钥匙串,失败才回退配置文件;
- 日志只记录长度、模型、耗时、错误,不记录正文与 Key;
- 云端模式会把选中文本发送给 DeepSeek;本地模式只发往你填的 Ollama 地址;
- 默认最大 6000 字,超过会截断,防止误选整篇文档。
打包
Windows 单 exe:
build_windows.bat
产物:dist\GrammarCorrection.exe。目标电脑不需要 Python。
Linux 单文件:
./build_linux.sh
./dist/grammar-correction
macOS:
python3 -m pip install pyinstaller
python3 -m PyInstaller --noconfirm --clean --windowed --name GrammarCorrection \
--add-data "assets:assets" --add-data "README.md:." grammar_correction.py
Linux 桌面菜单 / 开机自启:
./install_linux.sh
./install_linux.sh --autostart
./install_linux.sh --uninstall
兼容性说明
| 平台 | 划词 | 全局快捷键 | 备注 |
|---|---|---|---|
| Windows 10/11 | 模拟 Ctrl+C | RegisterHotKey |
目标应用不能以更高权限运行 |
| Ubuntu 20.04+ X11 | PRIMARY 选区 | XGrabKey |
推荐,最顺畅 |
| Wayland | 原生应用可能读不到 | 仅 XWayland 有效 | 用固定窗口或 --check-clipboard |
| macOS | 模拟 ⌘C | 可选 pynput |
需辅助功能权限 |
其他说明:
- 没有系统托盘;关闭窗口即退出。需要常驻可勾选「启动时隐藏」,配合快捷键和开机自启。
- Windows/macOS 取词会临时借用剪贴板,只恢复文本,不保留图片/富文本。
- Linux X11 主路径不改剪贴板;兜底复制路径退出程序后剪贴板可能失效。
- 小参数本地模型可能不严格遵守
## 修改后格式,此时用「复制结果」复制全文。 - 没有自动替换 Word 原文;复制「修改后」文本后手动粘贴,避免误改文档。
项目结构
Grammar correction/
├── grammar_correction.py # 入口
├── grammar_correction/
│ ├── api.py # DeepSeek SSE / Ollama NDJSON
│ ├── cli.py # 命令行
│ ├── config.py # 配置、路径、Key
│ ├── constants.py # 默认值
│ ├── hotkey.py # 全局快捷键
│ ├── logging_setup.py # 日志
│ ├── platform_utils.py # 平台工具
│ ├── prompts.py # 提示词与结果解析
│ ├── selection.py # 选区 / 剪贴板
│ └── ui.py # Tkinter 界面
├── tests/ # 单元与集成测试
├── assets/ # 图标与截图
├── packaging/ # Linux desktop 模板
├── run.sh / run.bat / run.command
├── build_windows.bat / build_linux.sh
├── install_linux.sh
├── run_tests.sh / run_tests.bat
├── requirements-optional.txt
└── README.md
项目源码:
https://gitcode.com/LNN0212/Grammar_correction_zucker