Skip to content

Commit 610b83c

Browse files
committed
feat: 文書全体の品質分析を強化し、コードブロックを除外したテキスト分析を追加
1 parent 58f604c commit 610b83c

File tree

2 files changed

+65
-79
lines changed

2 files changed

+65
-79
lines changed

src/rules/ai-tech-writing-guideline.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
177177
];
178178

179179
// 文書全体の品質評価指標
180+
let hasDocumentIssues = false;
180181
const documentQualityMetrics = {
181182
redundancy: 0,
182183
voice: 0,
@@ -186,21 +187,17 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
186187
};
187188

188189
return {
189-
[Syntax.Document](node) {
190-
// 文書全体の分析を実行(enableDocumentAnalysisがtrueの場合)
191-
if (enableDocumentAnalysis) {
192-
const source = new StringSource(node);
193-
const totalIssues = analyzeDocumentQuality(source.toString());
194-
195-
if (totalIssues > 0) {
196-
report(node, {
197-
message: `【テクニカルライティング品質分析】この文書で${totalIssues}件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md`
198-
});
199-
}
200-
}
201-
},
202190
[Syntax.Paragraph](node) {
203-
const source = new StringSource(node);
191+
// StringSourceを使用してコードブロックを除外したテキストを取得
192+
const source = new StringSource(node, {
193+
replacer({ node, emptyValue }) {
194+
// コードブロック、インラインコードを除外
195+
if (node.type === "Code" || node.type === "InlineCode") {
196+
return emptyValue();
197+
}
198+
return undefined;
199+
}
200+
});
204201
const text = source.toString();
205202

206203
// 許可パターンのチェック
@@ -224,44 +221,57 @@ const rule: TextlintRuleModule<Options> = (context, options = {}) => {
224221
const matches = text.matchAll(pattern);
225222
for (const match of matches) {
226223
const index = match.index ?? 0;
227-
const originalPosition = source.originalIndexFromIndex(index);
228-
const originalEndPosition = source.originalIndexFromIndex(index + match[0].length);
229224

230-
// カテゴリ別のメトリクスを更新
231-
documentQualityMetrics[category as keyof typeof documentQualityMetrics]++;
225+
// プレーンテキストの位置を元のノード内の位置に変換
226+
const originalIndex = source.originalIndexFromIndex(index);
227+
const originalEndIndex = source.originalIndexFromIndex(index + match[0].length);
228+
229+
if (originalIndex !== undefined && originalEndIndex !== undefined) {
230+
const originalRange = [originalIndex, originalEndIndex] as const;
231+
232+
// カテゴリ別のメトリクスを更新
233+
documentQualityMetrics[category as keyof typeof documentQualityMetrics]++;
234+
hasDocumentIssues = true;
232235

233-
if (originalPosition !== undefined && originalEndPosition !== undefined) {
234-
const matchRange = [originalPosition, originalEndPosition] as const;
235236
report(node, {
236237
message: message,
237-
padding: locator.range(matchRange)
238+
padding: locator.range(originalRange)
238239
});
239240
}
240241
}
241242
}
242-
}
243-
};
243+
},
244+
[Syntax.DocumentExit](node) {
245+
// 文書全体の分析を実行(enableDocumentAnalysisがtrueの場合)
246+
if (enableDocumentAnalysis && hasDocumentIssues) {
247+
const totalIssues = Object.values(documentQualityMetrics).reduce((sum, count) => sum + count, 0);
244248

245-
// 文書全体の品質分析関数
246-
function analyzeDocumentQuality(text: string): number {
247-
let totalIssues = 0;
249+
// カテゴリ別の詳細な分析結果を含むメッセージを生成
250+
const categoryDetails = [];
251+
if (documentQualityMetrics.redundancy > 0) {
252+
categoryDetails.push(`簡潔性: ${documentQualityMetrics.redundancy}件`);
253+
}
254+
if (documentQualityMetrics.voice > 0) {
255+
categoryDetails.push(`明確性: ${documentQualityMetrics.voice}件`);
256+
}
257+
if (documentQualityMetrics.clarity > 0) {
258+
categoryDetails.push(`具体性: ${documentQualityMetrics.clarity}件`);
259+
}
260+
if (documentQualityMetrics.consistency > 0) {
261+
categoryDetails.push(`一貫性: ${documentQualityMetrics.consistency}件`);
262+
}
263+
if (documentQualityMetrics.structure > 0) {
264+
categoryDetails.push(`構造化: ${documentQualityMetrics.structure}件`);
265+
}
248266

249-
// コードブロックを除外して分析するためのパターンリスト
250-
const guidancePatterns = [
251-
...(disableRedundancyGuidance ? [] : redundancyGuidance),
252-
...(disableVoiceGuidance ? [] : voiceGuidance),
253-
...(disableClarityGuidance ? [] : clarityGuidance),
254-
...(disableConsistencyGuidance ? [] : consistencyGuidance),
255-
...(disableStructureGuidance ? [] : structureGuidance)
256-
];
267+
const detailsText = categoryDetails.length > 0 ? ` [内訳: ${categoryDetails.join(", ")}]` : "";
257268

258-
for (const { pattern } of guidancePatterns) {
259-
const matches = text.matchAll(pattern);
260-
totalIssues += Array.from(matches).length;
269+
report(node, {
270+
message: `【テクニカルライティング品質分析】この文書で${totalIssues}件の改善提案が見つかりました${detailsText}。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md`
271+
});
272+
}
261273
}
262-
263-
return totalIssues;
264-
}
274+
};
265275
};
266276

267277
export default rule;

test/rules/ai-tech-writing-guideline.test.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ tester.run("ai-tech-writing-guideline", rule, {
77
valid: [
88
// 良いテクニカルライティングの例
99
{
10-
text: "ユーザーは設定を変更します。この操作により、アプリケーションのパフォーマンスが向上します。"
10+
text: "ユーザーは設定を変更します。この操作により、アプリケーションのパフォーマンスが向上します。",
11+
options: { enableDocumentAnalysis: false }
1112
},
1213
{
13-
text: "応答時間は50ms未満になります。処理速度は従来比200%向上しました。"
14+
text: "応答時間は50ms未満になります。処理速度は従来比200%向上しました。",
15+
options: { enableDocumentAnalysis: false }
1416
},
1517
{
16-
text: "システムがデータを検証します。エラーが発生した場合、ログファイルに記録されます。"
18+
text: "システムがデータを検証します。エラーが発生した場合、ログファイルに記録されます。",
19+
options: { enableDocumentAnalysis: false }
1720
}
1821
],
1922
invalid: [
2023
// 冗長性の問題
2124
{
2225
text: "まず最初に設定ファイルを開きます。",
26+
options: { enableDocumentAnalysis: false },
2327
errors: [
24-
{
25-
message:
26-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
27-
},
2828
{
2929
message:
3030
"【簡潔性】冗長表現が検出されました。「まず最初に」→「まず」または「最初に」への簡潔化を検討してください。"
@@ -33,11 +33,8 @@ tester.run("ai-tech-writing-guideline", rule, {
3333
},
3434
{
3535
text: "この機能を使用することができます。",
36+
options: { enableDocumentAnalysis: false },
3637
errors: [
37-
{
38-
message:
39-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
40-
},
4138
{
4239
message:
4340
"【簡潔性】冗長な助動詞表現が検出されました。「できます」または「します」への簡潔化を検討してください。"
@@ -47,11 +44,8 @@ tester.run("ai-tech-writing-guideline", rule, {
4744
// 明確性の問題(能動態・動詞)
4845
{
4946
text: "データの変更を行います。",
47+
options: { enableDocumentAnalysis: false },
5048
errors: [
51-
{
52-
message:
53-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
54-
},
5549
{
5650
message:
5751
"【明確性】名詞化された表現が検出されました。「を変更する」のような直接的な動詞表現を検討してください。"
@@ -61,11 +55,8 @@ tester.run("ai-tech-writing-guideline", rule, {
6155
// 具体性の問題
6256
{
6357
text: "高速なパフォーマンスを実現します。",
58+
options: { enableDocumentAnalysis: false },
6459
errors: [
65-
{
66-
message:
67-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
68-
},
6960
{
7061
message:
7162
"【具体性】抽象的な性能表現が検出されました。具体的な数値基準の提示を検討してください(例:「50ms未満の応答時間」)。"
@@ -74,11 +65,8 @@ tester.run("ai-tech-writing-guideline", rule, {
7465
},
7566
{
7667
text: "大幅に向上します。",
68+
options: { enableDocumentAnalysis: false },
7769
errors: [
78-
{
79-
message:
80-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
81-
},
8270
{
8371
message:
8472
"【具体性】定量化されていない変化表現が検出されました。具体的な数値や割合の提示を検討してください。"
@@ -88,11 +76,8 @@ tester.run("ai-tech-writing-guideline", rule, {
8876
// 一貫性の問題
8977
{
9078
text: "ユーザーがログインし、クライアントが設定を変更します。",
79+
options: { enableDocumentAnalysis: false },
9180
errors: [
92-
{
93-
message:
94-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
95-
},
9681
{
9782
message:
9883
"【一貫性】同一対象を指す用語の混在が検出されました。文書全体で統一した用語の使用を検討してください。"
@@ -101,11 +86,8 @@ tester.run("ai-tech-writing-guideline", rule, {
10186
},
10287
{
10388
text: "設定画面を開き、設定ページでオプションを変更します。",
89+
options: { enableDocumentAnalysis: false },
10490
errors: [
105-
{
106-
message:
107-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
108-
},
10991
{
11092
message:
11193
"【一貫性】機能名称の表記揺れが検出されました。プロジェクト内で統一した名称の使用を検討してください。"
@@ -115,11 +97,8 @@ tester.run("ai-tech-writing-guideline", rule, {
11597
// 受動態の問題
11698
{
11799
text: "処理がシステムによって実行されます。",
100+
options: { enableDocumentAnalysis: false },
118101
errors: [
119-
{
120-
message:
121-
"【テクニカルライティング品質分析】この文書で1件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
122-
},
123102
{
124103
message:
125104
"【明確性】受動態表現が検出されました。「システムが○○を実行する」のような能動態への変更を検討してください。"
@@ -129,11 +108,8 @@ tester.run("ai-tech-writing-guideline", rule, {
129108
// 複数の問題が同時に存在する場合
130109
{
131110
text: "まず最初に高速なパフォーマンスの実装を実施することができます。",
111+
options: { enableDocumentAnalysis: false },
132112
errors: [
133-
{
134-
message:
135-
"【テクニカルライティング品質分析】この文書で4件の改善提案が見つかりました。効果的なテクニカルライティングの7つのC(Clear, Concise, Correct, Coherent, Concrete, Complete, Courteous)の原則に基づいて見直しを検討してください。詳細なガイドライン: https://github.com/textlint-ja/textlint-rule-preset-ai-writing/blob/main/docs/tech-writing-guidelines.md"
136-
},
137113
{
138114
message:
139115
"【簡潔性】冗長表現が検出されました。「まず最初に」→「まず」または「最初に」への簡潔化を検討してください。"

0 commit comments

Comments
 (0)