akana-cli 0.9.0

CLI utility for Akana Turkish NLP toolkit
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
use akana_core::*;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "akana")]
#[command(about = "Akana: Modern and blazingly fast Turkish NLP CLI", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Tokenize Turkish text into words
    Tokenize {
        /// Text to tokenize
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Perform morphological analysis on a word (Zemberek-compatible format)
    Analyze {
        /// Word to analyze
        word: String,
    },
    /// Perform Google-style syntactic morphological analysis with Inflectional Groups (FSMNLP 2019)
    SyntacticAnalyze {
        /// Word to analyze
        word: String,
    },
    /// Generate a surface word from lemma and tags
    Generate {
        /// Base lemma
        lemma: String,
        /// Morpheme tags (e.g. A3pl, P1sg, Dat)
        #[arg(short, long, value_delimiter = ',')]
        tags: Vec<String>,
    },
    /// De-asciify Turkish text
    Deasciify {
        /// Text to restore diacritics
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Asciify Turkish text
    Asciify {
        /// Text to convert to ASCII
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Normalize informal text
    Normalize {
        /// Text to normalize
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Check and correct grammar and spelling rules (the default rules unless told otherwise)
    Grammar {
        /// Text to check
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Run only these rules (comma-separated ids; see --list-rules)
        #[arg(long, value_delimiter = ',')]
        rules: Option<Vec<String>>,
        /// Also run these rules
        #[arg(long, value_delimiter = ',')]
        enable: Vec<String>,
        /// Do not run these rules
        #[arg(long, value_delimiter = ',')]
        disable: Vec<String>,
        /// Also run every experimental rule (off by default: not precise enough on correct text)
        #[arg(long)]
        experimental: bool,
        /// Print only the corrected text
        #[arg(short, long)]
        correct: bool,
        /// Output the full result as JSON
        #[arg(short, long)]
        json: bool,
        /// List the rules, whether each is on by default, and its confidence (measured precision)
        #[arg(long)]
        list_rules: bool,
    },
    /// Check spelling and suggest corrections
    Spellcheck {
        /// Word to check
        word: String,
    },
    /// Parse sentence dependency tree
    Parse {
        /// Sentence to parse
        sentence: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Calculate modern and classical Turkish readability metrics
    Readability {
        /// Text to analyze
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Syllabify and hyphenate a Turkish word
    Syllabify {
        /// Word to syllabify
        word: String,
    },
    /// Convert number to Turkish written words
    Number {
        /// Number to convert
        num: i64,
    },
    /// Extract Named Entities (PER, LOC, ORG, DATE, MONEY, PERCENT)
    Ner {
        /// Text to analyze
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Extract top keywords from Turkish text
    Keywords {
        /// Text to extract keywords from
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Top K keywords
        #[arg(short, long, default_value_t = 10)]
        top_k: usize,
    },
    /// Extractive text summarization
    Summarize {
        /// Text to summarize
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Maximum number of sentences
        #[arg(short, long, default_value_t = 3)]
        sentences: usize,
    },
    /// Find morphological root / stem of a word
    Stem {
        /// Word to stem
        word: String,
    },
    /// Audit text for AI writing signatures, clichés, and punctuation anomalies
    AiAudit {
        /// Text to audit
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Generate an actionable LLM rewrite prompt to humanize Turkish text
    HumanizePrompt {
        /// Text to humanize
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Target register (e.g. blog, akademik, haber, edebi, hukuki)
        #[arg(short, long, default_value = "blog")]
        register: String,
    },
    /// Generate 256-dimensional Turkish sentence embedding vector
    Embed {
        /// Text to embed
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
    },
    /// Calculate semantic cosine similarity between two Turkish texts
    Similarity {
        /// First text
        text_a: String,
        /// Second text
        text_b: String,
    },
    /// Split Turkish text into chunks (semantic, sentence, or sdpm)
    Chunk {
        /// Text to chunk
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Chunking strategy: semantic, sentence, sdpm
        #[arg(short, long, default_value = "semantic")]
        strategy: String,
        /// Maximum tokens per chunk
        #[arg(short = 'c', long, default_value_t = 512)]
        chunk_size: usize,
        /// Overlap tokens between chunks (for sentence strategy)
        #[arg(short, long, default_value_t = 0)]
        overlap: usize,
        /// Minimum tokens before allowing a semantic split
        #[arg(short = 'k', long, default_value_t = 20)]
        min_chunk_size: usize,
        /// Similarity threshold (0.0 to 1.0) or cutoff value
        #[arg(short, long)]
        threshold: Option<f32>,
        /// Threshold calculation mode: similarity, percentile, stdev, iqr, auto
        #[arg(short = 'm', long, default_value = "percentile")]
        mode: String,
        /// Output formatted JSON
        #[arg(short, long)]
        json: bool,
    },
    /// Mask Turkish PII entities in text (KVKK aligned)
    PiiMask {
        /// Text to mask
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Masking mode: placeholder, surrogate, tag, anonymize
        #[arg(short, long, default_value = "placeholder")]
        mode: String,
        /// Output formatted JSON including entities and mapping
        #[arg(short, long)]
        json: bool,
    },
    /// Restore masked Turkish PII text using a JSON mapping dictionary
    PiiRestore {
        /// Masked text
        text: Option<String>,
        /// Read text from file
        #[arg(short, long)]
        file: Option<PathBuf>,
        /// Path to JSON mapping file
        #[arg(short, long)]
        mapping: PathBuf,
    },
}

fn resolve_input_text(text: Option<String>, file: Option<PathBuf>) -> Result<String, String> {
    if let Some(f) = file {
        std::fs::read_to_string(&f)
            .map_err(|e| format!("Failed to read file '{}': {}", f.display(), e))
    } else if let Some(t) = text {
        Ok(t)
    } else {
        Err("Please provide input text as argument or use -f/--file <PATH>".to_string())
    }
}

/// The grammar rules the `grammar` command runs.
fn grammar_rules(
    rules: Option<Vec<String>>,
    enable: Vec<String>,
    disable: Vec<String>,
    experimental: bool,
) -> Result<grammar::RuleSet, grammar::UnknownRule> {
    let mut set = match rules {
        Some(ids) => grammar::RuleSet::from_ids(ids)?,
        None => grammar::RuleSet::default(),
    };
    if experimental {
        set = set.with_experimental();
    }
    for rule in grammar::RuleSet::from_ids(enable)?.iter() {
        set.enable(rule);
    }
    for rule in grammar::RuleSet::from_ids(disable)?.iter() {
        set.disable(rule);
    }
    Ok(set)
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Tokenize { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let tokens = tokenization::TurkishTokenizer::tokenize_words(&input);
            println!("{}", serde_json::to_string_pretty(&tokens).unwrap());
        }
        Commands::Analyze { word } => {
            let morph = morphology::TurkishMorphology::new();
            let parses = morph.analyze(&word);
            println!("{}", serde_json::to_string_pretty(&parses).unwrap());
        }
        Commands::SyntacticAnalyze { word } => {
            let morph = syntactic_morphology::TurkishSyntacticMorphology::new();
            let parses = morph.analyze(&word);
            println!("{}", serde_json::to_string_pretty(&parses).unwrap());
        }
        Commands::Generate { lemma, tags } => {
            let gen = morphology::TurkishGenerator::new();
            let tag_refs: Vec<&str> = tags.iter().map(|s| s.as_str()).collect();
            if let Some(surface) = gen.generate(&lemma, &tag_refs) {
                println!("Generated surface: {surface}");
            } else {
                println!("Failed to generate surface form.");
            }
        }
        Commands::Deasciify { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let res = normalization::TurkishDeasciifier::deasciify(&input);
            println!("{res}");
        }
        Commands::Asciify { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let res = normalization::TurkishAsciifier::asciify(&input);
            println!("{res}");
        }
        Commands::Normalize { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let res = normalization::TurkishInformalNormalizer::normalize_text(&input);
            println!("{res}");
        }
        Commands::Grammar {
            text,
            file,
            rules,
            enable,
            disable,
            experimental,
            correct,
            json,
            list_rules,
        } => {
            if list_rules {
                for rule in grammar::GrammarRule::ALL {
                    println!(
                        "{:<24} {:<12} confidence {:.2}  {}",
                        rule.id(),
                        if rule.is_default() {
                            "default"
                        } else {
                            "experimental"
                        },
                        rule.confidence(),
                        rule.summary()
                    );
                }
                return;
            }
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let set = match grammar_rules(rules, enable, disable, experimental) {
                Ok(set) => set,
                Err(e) => {
                    eprintln!("Error: {e} (see --list-rules)");
                    std::process::exit(1);
                }
            };
            let checker = grammar::TurkishGrammarChecker::with_rules(set);
            let result = checker.check(&input);
            if json {
                println!("{}", serde_json::to_string_pretty(&result).unwrap());
            } else if correct {
                println!("{}", result.corrected);
            } else {
                for f in &result.findings {
                    println!(
                        "[{}] {:?} -> {:?}: {}",
                        f.rule, f.original_text, f.replacement, f.message_tr
                    );
                }
                println!("{}", result.corrected);
            }
        }
        Commands::Spellcheck { word } => {
            let checker = normalization::TurkishSpellChecker::new();
            let suggestions = checker.suggest(&word, 2, 5);
            println!("{}", serde_json::to_string_pretty(&suggestions).unwrap());
        }
        Commands::Parse { sentence, file } => {
            let input = match resolve_input_text(sentence, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let tokens = tokenization::TurkishTokenizer::tokenize_words(&input);
            let parser = parser::TurkishDependencyParser::new();
            let tree = parser.parse(&tokens);
            println!("{}", tree.to_conllu());
        }
        Commands::Readability { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let report = readability::analyze_readability(&input);
            println!("{}", serde_json::to_string_pretty(&report).unwrap());
        }
        Commands::Syllabify { word } => {
            let syllables = phonology::TurkishSyllabifier::syllabify(&word);
            let hyphenated = phonology::TurkishSyllabifier::hyphenate(&word, "-");
            println!(
                "Syllables: {:?} (Count: {}, Hyphenated: {})",
                syllables,
                syllables.len(),
                hyphenated
            );
        }
        Commands::Number { num } => {
            let words = normalization::TurkishNumberConverter::number_to_words(num);
            let ordinal = normalization::TurkishNumberConverter::ordinal_to_words(num);
            println!("Cardinal: {words}\nOrdinal:  {ordinal}");
        }
        Commands::Ner { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let entities = ner::TurkishNER::extract_entities(&input);
            println!("{}", serde_json::to_string_pretty(&entities).unwrap());
        }
        Commands::Keywords { text, file, top_k } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let extractor = analysis::TurkishKeywordExtractor::new();
            let kw = extractor.extract_keywords(&input, top_k);
            println!("{}", serde_json::to_string_pretty(&kw).unwrap());
        }
        Commands::Summarize {
            text,
            file,
            sentences,
        } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let summarizer = analysis::TurkishSummarizer::new();
            let summary = summarizer.summarize(&input, sentences);
            for (idx, sent) in summary.iter().enumerate() {
                println!("{}. {}", idx + 1, sent);
            }
        }
        Commands::Stem { word } => {
            let stemmer = morphology::TurkishStemmer::new();
            println!("Stem: {}", stemmer.stem(&word));
        }
        Commands::AiAudit { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let auditor = style::TurkishStyleAuditor::new();
            let report = auditor.audit(&input);
            println!("{}", serde_json::to_string_pretty(&report).unwrap());
        }
        Commands::HumanizePrompt {
            text,
            file,
            register,
        } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let prompt = style::TurkishHumanizer::generate_prompt(&input, &register);
            println!("{prompt}");
        }
        Commands::Embed { text, file } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let embeddings = embeddings::TurkishEmbeddings::new();
            let vec = embeddings.embed(&input);
            println!("{}", serde_json::to_string(&vec).unwrap());
        }
        Commands::Similarity { text_a, text_b } => {
            let embeddings = embeddings::TurkishEmbeddings::new();
            let score = embeddings.similarity(&text_a, &text_b);
            println!("{score:.4}");
        }
        Commands::Chunk {
            text,
            file,
            strategy,
            chunk_size,
            overlap,
            min_chunk_size,
            threshold,
            mode,
            json,
        } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };

            let thresh_mode = match mode.to_lowercase().as_str() {
                "similarity" | "sim" => {
                    chunking::ThresholdMode::Similarity(threshold.unwrap_or(0.32))
                }
                "percentile" | "perc" => {
                    chunking::ThresholdMode::Percentile(threshold.unwrap_or(0.75))
                }
                "stdev" | "std" => {
                    chunking::ThresholdMode::StandardDeviation(threshold.unwrap_or(0.80))
                }
                "iqr" => chunking::ThresholdMode::Interquartile(threshold.unwrap_or(1.0)),
                "auto" => chunking::ThresholdMode::Auto,
                _ => chunking::ThresholdMode::Percentile(threshold.unwrap_or(0.75)),
            };

            let chunks = match strategy.to_lowercase().as_str() {
                "sentence" => {
                    let chunker = chunking::SentenceChunker::new(chunk_size, overlap, 1);
                    chunker.chunk(&input)
                }
                "sdpm" => {
                    let chunker = chunking::SDPMChunker::new(
                        chunk_size,
                        thresh_mode,
                        threshold.unwrap_or(0.32),
                    )
                    .with_min_chunk_size(min_chunk_size);
                    chunker.chunk(&input)
                }
                _ => {
                    let chunker = chunking::SemanticChunker::new(chunk_size, thresh_mode)
                        .with_min_chunk_size(min_chunk_size);
                    chunker.chunk(&input)
                }
            };

            if json {
                println!("{}", serde_json::to_string_pretty(&chunks).unwrap());
            } else {
                println!("Generated {} chunk(s):\n", chunks.len());
                for (i, chunk) in chunks.iter().enumerate() {
                    println!(
                        "--- Chunk #{} (tokens: {}, chars: {}-{}) ---",
                        i + 1,
                        chunk.token_count,
                        chunk.start_index,
                        chunk.end_index
                    );
                    println!("{}\n", chunk.text);
                }
            }
        }
        Commands::PiiMask {
            text,
            file,
            mode,
            json,
        } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let engine = pii::TurkishPiiEngine::new();
            let pii_mode = match mode.to_lowercase().as_str() {
                "surrogate" => pii::PiiMode::SyntheticSurrogate,
                "tag" => pii::PiiMode::Tag,
                "anonymize" => pii::PiiMode::Anonymize,
                _ => pii::PiiMode::Placeholder,
            };
            let result = engine.mask(&input, pii_mode);
            if json {
                println!("{}", serde_json::to_string_pretty(&result).unwrap());
            } else {
                println!("{}", result.masked_text);
            }
        }
        Commands::PiiRestore {
            text,
            file,
            mapping,
        } => {
            let input = match resolve_input_text(text, file) {
                Ok(t) => t,
                Err(e) => {
                    eprintln!("Error: {e}");
                    std::process::exit(1);
                }
            };
            let map_str = match std::fs::read_to_string(&mapping) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("Failed to read mapping file: {e}");
                    std::process::exit(1);
                }
            };
            let map: std::collections::HashMap<String, String> =
                match serde_json::from_str(&map_str) {
                    Ok(m) => m,
                    Err(e) => {
                        eprintln!("Failed to parse mapping JSON: {e}");
                        std::process::exit(1);
                    }
                };
            let engine = pii::TurkishPiiEngine::new();
            let restored = engine.restore_with_mapping(&input, &map);
            println!("{}", restored);
        }
    }
}