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
mod markov_chain;
mod utils;
use clap::Parser;
use markov_chain::order2;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about=None)]
struct Args {
/// Defines how much smoothing to use for the Markov Chain
///
/// Higher Smoothing = More Creativity
/// Lower Smoothing = More Accuracy
///
/// Remember that more accuracy with lower smoothing
/// is entirely dependent on the quality of the provided dataset.
#[arg(short, long, default_value_t = 0.0, verbatim_doc_comment)]
smoothing: f64,
/// Weather to generate a name or not
///
/// [default: false]
#[arg(short, long, default_value_t = false, verbatim_doc_comment)]
generate: bool,
/// How many names to generate
#[arg(short, long, default_value_t = 1, verbatim_doc_comment)]
count: usize,
/// Whether to write transitions to a file for better performance
/// in the next run
///
/// This flag requires you to specify the name of the file to
/// write to. Note that the file extension should be `.zst`
/// but you are free to choose whatever you want.
#[arg(short, long, verbatim_doc_comment)]
write_transitions: Option<String>,
/// Whether to read transitions from a file for better performance
///
/// This flag requires you to specify the name of the file
/// which contains the data.
#[arg(short, long, verbatim_doc_comment)]
read_transitions: Option<String>,
/// Provide a file from which to train the model
///
/// The provided file must be a `.txt` or `.csv`
/// which should just contain the names on which to
/// train the model and should only contain newlines,
/// and commas as separators.
/// One thing to note here is that more priority
/// is given to commas, i.e. only commas are necessary
/// for separating different names.
#[arg(short, long, verbatim_doc_comment)]
train_from_file: Option<String>,
}
fn main() -> Result<(), String> {
let args = Args::parse();
let markov: order2::Markov;
match args.read_transitions {
Some(file_name) => match order2::Markov::read_transitions_from(&file_name) {
Ok(data) => markov = data,
Err(e) => {
eprintln!("can't read from file due to the following error:");
return Err(e.to_string());
}
},
None => {
let names = match args.train_from_file {
Some(file_name) => utils::parse_file(&file_name).unwrap_or_else(|err| {
eprintln!("can't read from the file due to the following error: {err}");
eprintln!("reverting back to the default names list");
vec![
"alice".to_string(),
"alina".to_string(),
"alex".to_string(),
"anna".to_string(),
"amelia".to_string(),
"aria".to_string(),
]
}),
None => vec![
"alice".to_string(),
"alina".to_string(),
"alex".to_string(),
"anna".to_string(),
"amelia".to_string(),
"aria".to_string(),
],
};
markov = order2::Markov::train(&names, args.smoothing);
}
}
// write transitions to a file
if let Some(file_name) = args.write_transitions {
if let Err(e) = markov.write_transitions_to_file(&file_name) {
eprintln!("can't write to file because of the following error:");
return Err(e.to_string());
}
}
// generate a name/names
if args.generate {
let distributions = markov.precompute_distributions();
let mut rng = rand::rng();
let mut generated_names = Vec::with_capacity(args.count);
let mut i = 0;
let mut reruns = 0;
while i < args.count {
if reruns >= 10 {
break;
};
let name = markov.generate(&mut rng, &distributions);
if generated_names.contains(&name) {
reruns += 1;
continue;
}
reruns = 0;
generated_names.push(name.clone());
println!("{}", name);
i += 1;
}
}
Ok(())
}