1+ name : Chinese Grammar Check
2+
3+ on :
4+ push :
5+ branches : [main]
6+ paths :
7+ - ' docs/cn/zh/**'
8+ pull_request :
9+ branches : [main]
10+ paths :
11+ - ' docs/cn/zh/**'
12+
13+ jobs :
14+ grammar-check :
15+ runs-on : ubuntu-latest
16+
17+ steps :
18+ - name : Checkout repository
19+ uses : actions/checkout@v4
20+
21+ - name : Set up Python
22+ uses : actions/setup-python@v5
23+ with :
24+ python-version : ' 3.10'
25+
26+ - name : Install dependencies
27+ run : |
28+ python -m pip install --upgrade pip
29+ pip install language-tool-python
30+
31+ - name : Run grammar check with enhanced reporting
32+ run : |
33+ python -c "
34+ import os
35+ import language_tool_python
36+ from pathlib import Path
37+ import time
38+
39+ # Retry LanguageTool init
40+ tool = None
41+ for _ in range(3):
42+ try:
43+ tool = language_tool_python.LanguageTool('zh-CN')
44+ tool.enable_spellchecking()
45+ break
46+ except Exception as e:
47+ print(f'⚠️ LanguageTool init failed: {e}, retrying...')
48+ time.sleep(5)
49+ if not tool:
50+ print('📢 [Warning] Failed to initialize LanguageTool after retries. Skipping check.')
51+ exit(0)
52+
53+ def get_line_number(text, offset):
54+ return text[:offset].count('\\n') + 1
55+
56+ def classify_rule(rule_id):
57+ if 'MORFOLOGIK_RULE' in rule_id:
58+ return '🔤 Spelling'
59+ elif 'GRAMMAR' in rule_id or 'CONJUGATION' in rule_id:
60+ return '🧩 Grammar'
61+ elif 'STYLE' in rule_id:
62+ return '✍️ Style'
63+ else:
64+ return '🔍 Other'
65+
66+ files_to_check = set()
67+ for directory in ['docs', 'docs/proposal', 'docs/ctl']:
68+ for file_path in Path(directory).rglob('*.md'):
69+ if file_path.is_file():
70+ files_to_check.add(file_path)
71+
72+ error_found = False
73+ error_messages = []
74+
75+ print('🚀 Starting Chinese grammar check...\n')
76+
77+ for file_path in sorted(files_to_check):
78+ try:
79+ with open(file_path, 'r', encoding='utf-8') as f:
80+ text = f.read()
81+ except UnicodeDecodeError:
82+ msg = f'❌ Encoding error: Unable to read {file_path} with UTF-8'
83+ print(msg)
84+ error_messages.append(msg)
85+ error_found = True
86+ continue
87+
88+ print(f'📄 Checking: {file_path}')
89+ matches = tool.check(text)
90+
91+ for match in matches:
92+ line_no = get_line_number(text, match.offset)
93+ rule_type = classify_rule(match.ruleId)
94+ message = match.message
95+ context = match.context.strip()
96+ suggestion = match.replacements[0] if match.replacements else 'No suggestion'
97+
98+ # Build formatted error message with emojis and spacing
99+ error_line = f' 📂 File: {file_path}:{line_no}'
100+ error_line += f'\\n 🔖 Type: {rule_type}'
101+ error_line += f'\\n ❌ Message: {message}'
102+ error_line += f'\\n 💬 Context: \"{context}\"'
103+ error_line += f'\\n 💡 Suggestion: \"{suggestion}\"'
104+ error_line += f'\\n'
105+
106+ error_messages.append(error_line)
107+ error_found = True
108+
109+ # GitHub warning annotation (with emoji in title)
110+ print(f'::warning file={file_path},line={line_no},title=🔍 {rule_type}::{message} (💡 {suggestion})')
111+
112+ if not matches:
113+ print(f'✅ No issues found in {file_path}')
114+
115+ # Final summary with spacing and emojis
116+ print('\\n' + '─' * 60)
117+ if error_found:
118+ print('📢 Chinese Grammar Check: Potential Issues Found')
119+ print('─' * 60)
120+ for msg in error_messages:
121+ print(msg)
122+ print('─' * 60)
123+ print('📌 Tip: Review the suggestions above. Some may be false positives.')
124+ print('💡 Pro tip: Fix critical issues like “降低3倍” or “原因是因为”.')
125+ else:
126+ print('✅ All Chinese content passed grammar and spelling checks.')
127+ print('✨ Great job! No issues found.')
128+ "
0 commit comments