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
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,
/// Whether 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 = 'f', long, verbatim_doc_comment)]
train_from_file: Option<String>,
/// Provide temperature scaling for further creativity of the model
///
/// This is similar to `smoothing`
/// Temperature Scaling works in the following way-
/// temperature > 1.0: More Creative/Random Names
/// temperature < 1.0: More Predictable/Repetitive Names
/// temperature = 1.0: No change
#[arg(short = 't', long, default_value_t = 1.0, verbatim_doc_comment)]
temperature: f64,
/// The maximum length a name can have
#[arg(long, default_value_t = 10)]
max: u8,
/// The minimum length a name can have
#[arg(long, default_value_t = 2)]
min: u8,
/// Number of allowed reruns to generate a single name
#[arg(long, default_value_t = 10)]
rerun: usize,
}
fn main() -> Result<(), String> {
let args = Args::parse();
if let Err(e) = utils::download_precomputed_dataset() {
eprintln!("error downloading precomputed dataset: {e}");
return Err(e.to_string());
};
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 (very small) names list");
vec![
"alice".to_string(),
"alina".to_string(),
"alex".to_string(),
"anna".to_string(),
"amelia".to_string(),
"aria".to_string(),
]
}),
None => {
vec!["nothing".to_string()]
}
};
if names.len() == 1 && names[0] == "nothing" {
markov = match order2::Markov::read_transitions_from(
utils::get_default_dataset_path()
.to_str()
.expect("invalid path"),
) {
Ok(data) => data,
Err(e) => {
eprintln!("can't read from file due to the following error:");
return Err(e.to_string());
}
}
} else {
markov = order2::Markov::train(&names);
}
}
}
// 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(args.smoothing, args.temperature);
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 >= args.rerun {
break;
};
let name = markov.generate(&mut rng, &distributions);
if generated_names.contains(&name) {
reruns += 1;
continue;
}
if name.len() as u8 > args.max {
reruns += 1;
continue;
}
if (name.len() as u8) < args.min {
reruns += 1;
continue;
}
reruns = 0;
generated_names.push(name.clone());
println!("{}", name);
i += 1;
}
}
Ok(())
}