suiko 0.3.3

Deterministic diagnostics for natural and readable Japanese writing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! 品詞列(Sudachi形態素)ベースの検出器と、文単位のtoken補助。

use crate::Error;
use crate::morphology::{Morpheme, Morphology};
use crate::text::Sentence;

use super::{Finding, Span, Suggestion, make_span};

// Sudachi品詞体系。IPADICで名詞に含まれた代名詞と形容動詞語幹(形状詞)を
// 独立品詞として持つため、内容語の範囲を揃えて列挙する。
pub(super) const CONTENT_POS: &[&str] = &["名詞", "代名詞", "形状詞", "動詞", "形容詞", "副詞"];

const TRANSITIVE_SMELL_VERBS: &[&str] = &[
    "もたらす",
    "示す",
    "意味する",
    "証明する",
    "生み出す",
    "反映する",
    "示唆する",
    "物語る",
    "浮き彫りにする",
    "後押しする",
];

const ABSTRACT_METAPHOR_NOUNS: &[&str] = &[
    "地図",
    "羅針盤",
    "\u{5951}\u{7d04}",
    "道標",
    "土台",
    "架け橋",
];

const ABSTRACT_CONTEXT_NOUNS: &[&str] = &[
    "実装",
    "判断",
    "設計",
    "仕様",
    "方針",
    "計画",
    "戦略",
    "議論",
    "思考",
    "理解",
    "運用",
    "開発",
    "組織",
    "事業",
    "変革",
    "成長",
    "課題",
    "解決",
    "意思決定",
];

#[derive(Clone, Debug)]
pub(super) struct TokenizedSentence {
    pub(super) line: usize,
    pub(super) text: String,
    pub(super) raw_text: String,
    pub(super) end_mark: Option<char>,
    pub(super) line_byte_start: usize,
    pub(super) tokens: Vec<Morpheme>,
}

impl TokenizedSentence {
    /// 文内のtoken byte範囲を行内のbyte範囲へ写す。
    pub(super) fn span(
        &self,
        raw_lines: &[&str],
        byte_start: usize,
        byte_end: usize,
    ) -> Option<Span> {
        make_span(
            raw_lines,
            self.line,
            self.line_byte_start + byte_start,
            self.line,
            self.line_byte_start + byte_end,
        )
    }

    /// 文内byte範囲の抜粋。raw側が範囲を切り出せない場合はmasked本文へ落とす。
    pub(super) fn excerpt(&self, byte_start: usize, byte_end: usize) -> String {
        self.raw_text
            .get(byte_start..byte_end)
            .unwrap_or(&self.text[byte_start..byte_end])
            .to_owned()
    }
}

pub(super) fn tokenize(
    split: &[Sentence],
    morphology: &Morphology,
) -> Result<Vec<TokenizedSentence>, Error> {
    split
        .iter()
        .map(|sentence| {
            Ok(TokenizedSentence {
                line: sentence.line,
                text: sentence.text.clone(),
                raw_text: sentence.raw_text.clone(),
                end_mark: sentence.end_mark,
                line_byte_start: sentence.line_byte_start,
                tokens: morphology.tokenize(&sentence.text)?,
            })
        })
        .collect()
}

pub(super) fn significant_tokens(tokens: &[Morpheme]) -> &[Morpheme] {
    let start = tokens
        .iter()
        .position(|token| !matches!(token.pos(0), "記号" | "補助記号" | "空白"))
        .unwrap_or(tokens.len());
    &tokens[start..]
}

pub(super) fn punctuation_between(tokens: &[Morpheme], first: usize, second: usize) -> bool {
    tokens[first + 1..second]
        .iter()
        .any(|token| matches!(token.pos(0), "記号" | "補助記号"))
}

pub(super) fn noun_ended(tokens: &[Morpheme]) -> bool {
    tokens
        .iter()
        .rev()
        .find(|token| !matches!(token.pos(0), "記号" | "補助記号" | "空白"))
        .is_some_and(|token| matches!(token.pos(0), "名詞" | "代名詞"))
}

pub(super) fn buried_list(tokens: &[Morpheme]) -> Option<(usize, usize, usize)> {
    let mut bounds = Vec::new();
    let mut start = 0;
    for (index, token) in tokens.iter().enumerate() {
        if token.surface == "、" {
            bounds.push((start, index));
            start = index + 1;
        }
    }
    bounds.push((start, tokens.len()));
    let mut run = Vec::new();
    let mut best = None;
    for (index, (start, end)) in bounds.iter().copied().enumerate() {
        if end > start && noun_ended(&tokens[start..end]) {
            run.push((start, end));
        } else {
            run.clear();
        }
        if run.len() >= 2 && index + 1 < bounds.len() {
            let items = run.len() + 1;
            if best.is_none_or(|(_, _, best_items)| items > best_items) {
                best = Some((run[0].0, bounds[index + 1].1, items));
            }
        }
    }
    best
}

pub(super) fn mora_length(tokens: &[Morpheme]) -> usize {
    tokens
        .iter()
        .map(|token| {
            token
                .reading()
                .chars()
                .filter(|ch| {
                    !matches!(
                        ch,
                        'ァ' | 'ィ' | 'ゥ' | 'ェ' | 'ォ' | 'ャ' | 'ュ' | 'ョ' | 'ヮ'
                    )
                })
                .count()
        })
        .sum()
}

pub(super) fn translationese_morph_findings(
    tokenized: &[TokenizedSentence],
    raw_lines: &[&str],
) -> Vec<Finding> {
    let mut findings = Vec::new();
    for sentence in tokenized {
        for (index, token) in sentence.tokens.iter().enumerate() {
            let Some(particle) = sentence.tokens.get(index + 1) else {
                continue;
            };
            let Some(verb) = sentence.tokens.get(index + 2) else {
                continue;
            };
            // 2026-08-18の技術書翻訳21件の正解ラベルに基づき対象を絞る:
            // 「は」型(ことはできない)は否定の対比として自然、使役型
            // (させることができる)は縮約すると受身と紛れるため対象外。
            let causative = index > 0
                && matches!(
                    sentence.tokens[index - 1].dictionary_form(),
                    "せる" | "させる"
                );
            if token.surface == "こと"
                && token.pos(0) == "名詞"
                && particle.pos(0) == "助詞"
                && particle.surface == "が"
                && !causative
                && verb.pos(0) == "動詞"
                && verb.surface.starts_with("でき")
            {
                let start = sentence.tokens[index.saturating_sub(4)].byte_start;
                let mut finding = Finding::new(
                    sentence.line,
                    "translationese_morph",
                    sentence.excerpt(start, verb.byte_end),
                    "info",
                    "品詞列マッチ: 名詞/動詞+こと+が/は+できる型の翻訳調構文",
                );
                finding.span = sentence.span(raw_lines, start, verb.byte_end);
                finding.suggestion = suru_koto_ga_suggestion(sentence, raw_lines, index, particle);
                findings.push(finding);
            }
        }
    }
    findings
}

/// 機械的に安全な唯一の縮約: 「〜することができる」→「〜できる」。
/// 直前が動詞「する」で助詞が「が」の場合だけ、「することが」の削除候補を出す。
/// raw行のpreimageが一致しないときは出さない。
fn suru_koto_ga_suggestion(
    sentence: &TokenizedSentence,
    raw_lines: &[&str],
    koto_index: usize,
    particle: &Morpheme,
) -> Option<Suggestion> {
    if koto_index == 0 || particle.surface != "が" {
        return None;
    }
    let suru = sentence.tokens.get(koto_index - 1)?;
    if suru.pos(0) != "動詞" || suru.dictionary_form() != "する" {
        return None;
    }
    let koto = &sentence.tokens[koto_index];
    let expected = format!("{}{}{}", suru.surface, koto.surface, particle.surface);
    let line_start = sentence.line_byte_start + suru.byte_start;
    let line_end = sentence.line_byte_start + particle.byte_end;
    let matches_raw = raw_lines
        .get(sentence.line - 1)
        .and_then(|raw_line| raw_line.get(line_start..line_end))
        .is_some_and(|slice| slice == expected);
    if !matches_raw {
        return None;
    }
    Some(Suggestion {
        span: sentence.span(raw_lines, suru.byte_start, particle.byte_end)?,
        preimage: expected,
        replacement: String::new(),
    })
}

/// サ変名詞+を+行う型の冗長(「検証を行う」→「検証する」)。
/// 名詞とを、をと行うが隣接する場合だけ対象にし、受身(行われる)と
/// 名詞がサ変可能でない場合(「祭りを行う」)は対象外。textlintの
/// ai-tech-writing-guidelineが指摘する冗長クラスのうち、形態素で
/// 決定的に判定できる部分だけを扱う。
pub(super) fn redundant_light_verb_findings(
    tokenized: &[TokenizedSentence],
    raw_lines: &[&str],
) -> Vec<Finding> {
    let mut findings = Vec::new();
    for sentence in tokenized {
        for (index, noun) in sentence.tokens.iter().enumerate() {
            let Some(particle) = sentence.tokens.get(index + 1) else {
                continue;
            };
            let Some(verb) = sentence.tokens.get(index + 2) else {
                continue;
            };
            let verbal_noun = noun.pos(0) == "名詞"
                && (noun.pos(2) == "サ変可能" || noun.pos(2) == "サ変形状詞可能");
            if !verbal_noun
                || particle.pos(0) != "助詞"
                || particle.surface != "を"
                || verb.pos(0) != "動詞"
                || !matches!(verb.dictionary_form(), "行う" | "行なう")
            {
                continue;
            }
            // 受身・使役(行われる、行わせる)は言い換えの意味が変わるため対象外
            let passive_or_causative = sentence.tokens.get(index + 3).is_some_and(|next| {
                matches!(
                    next.dictionary_form(),
                    "れる" | "られる" | "せる" | "させる"
                )
            });
            if passive_or_causative {
                continue;
            }
            let mut finding = Finding::new(
                sentence.line,
                "redundant_light_verb",
                sentence.excerpt(noun.byte_start, verb.byte_end),
                "info",
                format!(
                    "サ変名詞+を+行う型の冗長候補: 「{}を{}」は「{}する」へ畳める。名詞の動作性を活かす方が簡潔(意図的な文体なら維持する)",
                    noun.surface, verb.surface, noun.surface
                ),
            );
            finding.span = sentence.span(raw_lines, noun.byte_start, verb.byte_end);
            finding.suggestion = light_verb_suggestion(sentence, raw_lines, particle, verb);
            findings.push(finding);
        }
    }
    findings
}

/// 抽象的な対象を物体になぞらえ、判断内容を曖昧にする可能性がある名詞句。
/// 候補語だけでは本来の意味での用例まで拾うため、述語として使われる場合か、
/// 抽象名詞から「の」で修飾される場合に限定する。
pub(super) fn abstract_metaphor_findings(
    tokenized: &[TokenizedSentence],
    raw_lines: &[&str],
) -> Vec<Finding> {
    let mut findings = Vec::new();
    for sentence in tokenized {
        for (index, token) in sentence.tokens.iter().enumerate() {
            if token.pos(0) != "名詞" || !ABSTRACT_METAPHOR_NOUNS.contains(&token.dictionary_form())
            {
                continue;
            }

            let abstract_genitive = index >= 2
                && sentence.tokens[index - 1].surface == "の"
                && sentence.tokens[index - 2].pos(0) == "名詞"
                && is_abstract_context_noun(&sentence.tokens[index - 2]);
            let predicate_end = metaphor_predicate_end(&sentence.tokens, index);
            if !abstract_genitive
                && (predicate_end.is_none()
                    || !has_abstract_context_before(&sentence.tokens, index))
            {
                continue;
            }

            let byte_start = if index >= 2 && sentence.tokens[index - 1].surface == "の" {
                sentence.tokens[index - 2].byte_start
            } else {
                token.byte_start
            };
            let byte_end = predicate_end
                .map(|end| sentence.tokens[end].byte_end)
                .unwrap_or(token.byte_end);
            let mut finding = Finding::new(
                sentence.line,
                "abstract_metaphor",
                sentence.excerpt(byte_start, byte_end),
                "info",
                format!(
                    "抽象比喩の可能性: 「{}」。判断対象・判断基準・具体的な効果を明記してください",
                    token.surface
                ),
            );
            finding.span = sentence.span(raw_lines, byte_start, byte_end);
            findings.push(finding);
        }
    }
    findings
}

fn is_abstract_context_noun(token: &Morpheme) -> bool {
    ABSTRACT_CONTEXT_NOUNS.iter().any(|candidate| {
        token.dictionary_form() == *candidate || token.surface.ends_with(candidate)
    })
}

fn has_abstract_context_before(tokens: &[Morpheme], noun_index: usize) -> bool {
    tokens[..noun_index]
        .iter()
        .rev()
        .take_while(|token| !matches!(token.pos(0), "記号" | "補助記号"))
        .take(12)
        .any(|token| token.pos(0) == "名詞" && is_abstract_context_noun(token))
}

fn metaphor_predicate_end(tokens: &[Morpheme], noun_index: usize) -> Option<usize> {
    let first = tokens.get(noun_index + 1)?;
    if matches!(first.dictionary_form(), "だ" | "です") {
        return Some(noun_index + 1);
    }

    let second = tokens.get(noun_index + 2)?;
    if matches!(first.surface.as_str(), "に" | "と") && second.dictionary_form() == "なる" {
        return Some(noun_index + 2);
    }
    if first.surface == "で" && second.dictionary_form() == "ある" {
        return Some(noun_index + 2);
    }
    if first.surface == "と" && second.dictionary_form() == "する" {
        let third = tokens.get(noun_index + 3)?;
        if third.surface == "て" {
            return Some(noun_index + 3);
        }
    }
    None
}

/// 「を+行う」の活用形ごとに機械的に安全な置換だけを提案する。
/// 行う→する、行い→し、行っ→し。それ以外の活用(行わ、行え等)は
/// 提案しない。preimageがraw行と一致しない場合も提案しない。
fn light_verb_suggestion(
    sentence: &TokenizedSentence,
    raw_lines: &[&str],
    particle: &Morpheme,
    verb: &Morpheme,
) -> Option<Suggestion> {
    let replacement = match verb.surface.as_str() {
        "行う" | "行なう" => "する",
        "行い" | "行ない" => "し",
        "行っ" | "行なっ" => "し",
        _ => return None,
    };
    let expected = format!("{}{}", particle.surface, verb.surface);
    let line_start = sentence.line_byte_start + particle.byte_start;
    let line_end = sentence.line_byte_start + verb.byte_end;
    let matches_raw = raw_lines
        .get(sentence.line - 1)
        .and_then(|raw_line| raw_line.get(line_start..line_end))
        .is_some_and(|slice| slice == expected);
    if !matches_raw {
        return None;
    }
    Some(Suggestion {
        span: sentence.span(raw_lines, particle.byte_start, verb.byte_end)?,
        preimage: expected,
        replacement: replacement.to_owned(),
    })
}

pub(super) fn inanimate_morph_findings(
    tokenized: &[TokenizedSentence],
    raw_lines: &[&str],
) -> Vec<Finding> {
    let mut findings = Vec::new();
    for sentence in tokenized {
        let mut skip_until = None;
        for index in 0..sentence.tokens.len() {
            if skip_until.is_some_and(|skip| index <= skip) {
                continue;
            }
            let token = &sentence.tokens[index];
            let mut subject_end = index;
            let mut abstract_subject =
                matches!(token.surface.as_str(), "これ" | "それ" | "あれ" | "それら")
                    || (token.pos(0) == "名詞"
                        && matches!(token.surface.as_str(), "こと" | "事実"));
            if !abstract_subject && let Some(next) = sentence.tokens.get(index + 1) {
                let phrase = format!("{}{}", token.surface, next.surface);
                if matches!(phrase.as_str(), "この事実" | "そのこと") {
                    abstract_subject = true;
                    subject_end = index + 1;
                }
            }
            if !abstract_subject {
                continue;
            }
            skip_until = Some(subject_end);
            let Some(particle) = sentence.tokens.get(subject_end + 1) else {
                continue;
            };
            if particle.pos(0) != "助詞" || !matches!(particle.surface.as_str(), "が" | "は") {
                continue;
            }
            let verb = sentence.tokens[subject_end + 2..].iter().find(|candidate| {
                candidate.pos(0) == "動詞"
                    && TRANSITIVE_SMELL_VERBS.contains(&candidate.dictionary_form())
            });
            let Some(verb) = verb else {
                continue;
            };
            let byte_start = sentence.tokens[index.saturating_sub(3)].byte_start;
            let subject = sentence.tokens[index..=subject_end]
                .iter()
                .map(|token| token.surface.as_str())
                .collect::<String>();
            let mut finding = Finding::new(
                sentence.line,
                "inanimate_subject_morph",
                sentence.excerpt(byte_start, verb.byte_end),
                "info",
                format!(
                    "品詞列マッチ: 抽象主語「{subject}」+ {} + 他動詞的述語「{}」(英語統語の直訳調の疑い)",
                    particle.surface,
                    verb.dictionary_form()
                ),
            );
            finding.span = sentence.span(raw_lines, byte_start, verb.byte_end);
            findings.push(finding);
        }
    }
    findings
}