lawkit 2.6.1

Statistical law analysis CLI toolkit with international number support
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
use clap::{Arg, Command};
use lawkit_core::common::{memory::MemoryConfig, parallel::ParallelConfig};

/// 全サブコマンドで共通のオプションを定義
pub fn add_common_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("format")
            .long("format")
            .short('f')
            .value_name("FORMAT")
            .help("Output format: text, csv, json, yaml, toml, xml")
            .default_value("text"),
    )
    .arg(
        Arg::new("quiet")
            .long("quiet")
            .short('q')
            .help("Minimal output")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("verbose")
            .long("verbose")
            .short('v')
            .help("Detailed output")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("filter")
            .long("filter")
            .value_name("RANGE")
            .help("Filter numbers by range (e.g., >=100, <1000, 50-500)"),
    )
    .arg(
        Arg::new("min-count")
            .long("min-count")
            .short('c')
            .value_name("NUMBER")
            .help("Minimum number of data points required for analysis")
            .default_value("10"), // 統一されたデフォルト値
    )
    .arg(
        Arg::new("no-color")
            .long("no-color")
            .help("Disable colored output")
            .action(clap::ArgAction::SetTrue),
    )
}

/// generateサブコマンド用の共通オプション(--format, --filter, --min-count は除外)
pub fn add_generate_common_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("quiet")
            .long("quiet")
            .short('q')
            .help("Suppress progress messages")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("verbose")
            .long("verbose")
            .short('v')
            .help("Show generation details")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("no-color")
            .long("no-color")
            .help("Disable colored output")
            .action(clap::ArgAction::SetTrue),
    )
}

/// input引数を追加(位置引数)
pub fn add_input_arg(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("input")
            .help("Input data (file path, URL, or '-' for stdin)")
            .index(1),
    )
}

/// サブコマンド固有のオプション:ベンフォード法則
pub fn add_benf_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("threshold")
            .long("threshold")
            .short('t')
            .value_name("LEVEL")
            .help("Anomaly detection threshold: low, medium, high, critical")
            .default_value("auto"),
    )
    .arg(
        Arg::new("confidence")
            .long("confidence")
            .value_name("LEVEL")
            .help("Statistical confidence level for tests (0.01-0.99)")
            .default_value("0.95"),
    )
    .arg(
        Arg::new("sample-size")
            .long("sample-size")
            .value_name("NUMBER")
            .help("Maximum sample size for large datasets (improves performance)"),
    )
    .arg(
        Arg::new("min-value")
            .long("min-value")
            .value_name("VALUE")
            .help("Minimum value to include in analysis (filters small values that add noise)"),
    )
}

/// サブコマンド固有のオプション:パレート法則  
pub fn add_pareto_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("concentration")
            .long("concentration")
            .short('C')
            .value_name("THRESHOLD")
            .help("Concentration threshold (0.0-1.0)")
            .default_value("0.8"),
    )
    .arg(
        Arg::new("gini-coefficient")
            .long("gini-coefficient")
            .help("Calculate Gini coefficient for inequality measurement")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("percentiles")
            .long("percentiles")
            .value_name("PERCENTILES")
            .help("Custom percentiles to calculate (e.g., 70,80,90)"),
    )
    .arg(
        Arg::new("business-analysis")
            .long("business-analysis")
            .help("Enable business analysis insights")
            .action(clap::ArgAction::SetTrue),
    )
}

/// サブコマンド固有のオプション:Zipf法則
pub fn add_zipf_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("text")
            .long("text")
            .short('T')
            .help("Enable text analysis mode")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("words")
            .long("words")
            .short('w')
            .value_name("NUMBER")
            .help("Maximum number of words to analyze in text mode")
            .default_value("1000"),
    )
}

/// サブコマンド固有のオプション:正規分布
pub fn add_normal_options(cmd: Command) -> Command {
    cmd
        .arg(
            Arg::new("test")
                .long("test")
                .short('T')
                .value_name("METHOD")
                .help("Normality test method: shapiro, anderson, ks, all")
                .default_value("all"),
        )
        .arg(
            Arg::new("outliers")
                .long("outliers")
                .short('O')
                .help("Enable outlier detection")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("outlier-method")
                .long("outlier-method")
                .value_name("METHOD")
                .help("Outlier detection method: zscore, modified_zscore, iqr, lof, isolation, dbscan, ensemble")
                .default_value("zscore"),
        )
        .arg(
            Arg::new("quality-control")
                .long("quality-control")
                .short('Q')
                .help("Enable quality control analysis")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("spec-limits")
                .long("spec-limits")
                .value_name("LOWER,UPPER")
                .help("Specification limits for quality control (e.g., 9.5,10.5)"),
        )
        .arg(
            Arg::new("enable-timeseries")
                .long("enable-timeseries")
                .help("Enable time series analysis")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("timeseries-window")
                .long("timeseries-window")
                .value_name("SIZE")
                .help("Time series analysis window size")
                .default_value("10"),
        )
}

/// サブコマンド固有のオプション:ポアソン分布
pub fn add_poisson_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("test")
            .long("test")
            .short('T')
            .value_name("METHOD")
            .help("Goodness-of-fit test method: chi_square, ks, variance, all")
            .default_value("all"),
    )
    .arg(
        Arg::new("predict")
            .long("predict")
            .short('p')
            .help("Enable probability prediction")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("max-events")
            .long("max-events")
            .value_name("NUMBER")
            .help("Maximum number of events for analysis")
            .default_value("20"),
    )
    .arg(
        Arg::new("rare-events")
            .long("rare-events")
            .short('R')
            .help("Focus on rare event analysis")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("confidence")
            .long("confidence")
            .value_name("LEVEL")
            .help("Statistical confidence level for tests (0.01-0.99)")
            .default_value("0.95"),
    )
}

/// サブコマンド固有のオプション:データ生成
pub fn add_generate_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("samples")
            .long("samples")
            .short('s')
            .value_name("NUMBER")
            .help("Number of samples to generate")
            .default_value("1000"),
    )
    .arg(
        Arg::new("seed")
            .long("seed")
            .value_name("NUMBER")
            .help("Random seed for reproducible generation"),
    )
    .arg(
        Arg::new("output-file")
            .long("output-file")
            .short('o')
            .value_name("FILE")
            .help("Output file path (default: stdout)"),
    )
    .arg(
        Arg::new("fraud-rate")
            .long("fraud-rate")
            .value_name("RATE")
            .help("Fraud injection rate (0.0-1.0) for testing")
            .default_value("0.0"),
    )
}

/// Generate用法則固有オプション:ベンフォード法則
pub fn add_generate_benf_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("range")
            .long("range")
            .value_name("MIN,MAX")
            .help("Number range for generation (e.g., 1,10000)")
            .default_value("1,100000"),
    )
}

/// Generate用法則固有オプション:パレート法則
pub fn add_generate_pareto_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("concentration")
            .long("concentration")
            .short('C')
            .value_name("RATIO")
            .help("Concentration ratio (0.0-1.0, default: 0.8 for 80/20)")
            .default_value("0.8"),
    )
    .arg(
        Arg::new("scale")
            .long("scale")
            .value_name("NUMBER")
            .help("Scale parameter for Pareto distribution")
            .default_value("1.0"),
    )
}

/// Generate用法則固有オプション:Zipf法則  
pub fn add_generate_zipf_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("exponent")
            .long("exponent")
            .short('e')
            .value_name("NUMBER")
            .help("Zipf exponent (default: 1.0)")
            .default_value("1.0"),
    )
    .arg(
        Arg::new("vocabulary-size")
            .long("vocabulary-size")
            .short('V')
            .value_name("NUMBER")
            .help("Vocabulary size for text generation")
            .default_value("10000"),
    )
}

/// Generate用法則固有オプション:正規分布
pub fn add_generate_normal_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("mean")
            .long("mean")
            .short('m')
            .value_name("NUMBER")
            .help("Mean of normal distribution")
            .default_value("0.0"),
    )
    .arg(
        Arg::new("stddev")
            .long("stddev")
            .short('d')
            .value_name("NUMBER")
            .help("Standard deviation of normal distribution")
            .default_value("1.0"),
    )
}

/// Generate用法則固有オプション:ポアソン分布
pub fn add_generate_poisson_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("lambda")
            .long("lambda")
            .short('l')
            .value_name("NUMBER")
            .help("Lambda parameter (rate) for Poisson distribution")
            .default_value("2.0"),
    )
    .arg(
        Arg::new("time-series")
            .long("time-series")
            .short('T')
            .help("Generate time-series event data")
            .action(clap::ArgAction::SetTrue),
    )
}

/// サブコマンド固有のオプション:統合分析
pub fn add_integration_options(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("laws")
            .long("laws")
            .short('l') // 統合分析専用で-lを使用
            .help("Laws to analyze (benf,pareto,zipf,normal,poisson)")
            .value_name("LAWS"),
    )
    .arg(
        Arg::new("focus")
            .long("focus")
            .short('F') // -f から -F に変更(formatと区別)
            .help("Analysis focus area")
            .value_name("FOCUS")
            .value_parser(["quality", "concentration", "distribution", "anomaly"]),
    )
    .arg(
        Arg::new("threshold")
            .long("threshold")
            .short('t')
            .help("Analysis threshold for anomaly detection (0.0-1.0)")
            .value_name("THRESHOLD")
            .value_parser(clap::value_parser!(f64))
            .default_value("0.5"),
    )
    .arg(
        Arg::new("recommend")
            .long("recommend")
            .short('r')
            .help("Enable recommendation mode")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("report")
            .long("report")
            .help("Analysis report type")
            .value_name("TYPE")
            .value_parser(["summary", "detailed", "anomalies"])
            .default_value("summary"),
    )
    .arg(
        Arg::new("consistency-check")
            .long("consistency-check")
            .help("Enable consistency check")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("cross-validation")
            .long("cross-validation")
            .help("Enable cross-validation analysis")
            .action(clap::ArgAction::SetTrue),
    )
    .arg(
        Arg::new("confidence-level")
            .long("confidence-level")
            .help("Confidence level")
            .value_name("LEVEL")
            .value_parser(clap::value_parser!(f64))
            .default_value("0.95"),
    )
    .arg(
        Arg::new("purpose")
            .long("purpose")
            .short('p')
            .help("Analysis purpose")
            .value_name("PURPOSE")
            .value_parser([
                "quality",
                "fraud",
                "concentration",
                "anomaly",
                "distribution",
                "general",
            ]),
    )
}

/// 自動最適化設定をセットアップ(データ特性に基づく)
pub fn setup_automatic_optimization_config() -> (ParallelConfig, MemoryConfig) {
    // 常に最適化設定を使用(自動最適化)
    let parallel_config = ParallelConfig {
        num_threads: 0, // auto-detect
        chunk_size: 1000,
        enable_parallel: true,
    };
    let memory_config = MemoryConfig {
        chunk_size: 10000,
        max_memory_mb: 512,
        enable_streaming: true,
        enable_compression: false,
    };
    (parallel_config, memory_config)
}

/// 最適化されたファイルリーダーを取得
pub fn get_optimized_reader(input: Option<&String>) -> Result<String, Box<dyn std::error::Error>> {
    // 自動最適化された読み込み方法を使用
    if let Some(input_path) = input {
        if input_path == "-" {
            use std::io::Read;
            let mut buffer = String::new();
            std::io::stdin().read_to_string(&mut buffer)?;
            Ok(buffer)
        } else {
            std::fs::read_to_string(input_path).map_err(Into::into)
        }
    } else {
        use std::io::Read;
        let mut buffer = String::new();
        std::io::stdin().read_to_string(&mut buffer)?;
        Ok(buffer)
    }
}