random gender, separator, readme
This commit is contained in:
parent
e6112df427
commit
1088b94577
41
README.md
41
README.md
|
@ -1,8 +1,49 @@
|
||||||
# Générateur de noms français
|
# Générateur de noms français
|
||||||
|
|
||||||
|
## Utilisation
|
||||||
|
|
||||||
|
Les deux arguments sont facultatifs
|
||||||
|
```sh
|
||||||
|
./rust-name-gen --host 127.0.0.1 --port 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
### Query params pour générer les noms
|
||||||
|
|
||||||
|
- `qty` - Nombre de noms générés, entre `1` et `50`. Défaut `10`.
|
||||||
|
- `sep` - Le caractère de séparation entre les sections des noms générés. Utilise l'espace par défaut.
|
||||||
|
- `gender` - Genre des noms générés, `m` ou `f`. Défaut au hasard.
|
||||||
|
- `sequence` - Une suite de mots-clés pour déterminer la structure du nom, séparés par un espace. Valeurs acceptés : `prenom patronyme commun adjectif ppasse ppresent article virgule`
|
||||||
|
- `ppasse`/`ppresent` pour "participe passé/présent".
|
||||||
|
- `article` sera transformé en `le`/`la`/`l'`.
|
||||||
|
- `virgule` est... une virgule.
|
||||||
|
|
||||||
|
Exemple :
|
||||||
|
```http
|
||||||
|
GET 127.0.0.1:8080?qty=5&sequence=prenom%20patronyme%20virgule%20article%20commun%20adjectif%20ppresent%20ppasse
|
||||||
|
```
|
||||||
|
|
||||||
|
Résultat :
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
"Henriette Camino, la connectabilité rubanière glénante matée",
|
||||||
|
"Léa Spinosi, l'exalgine sulfurée rempaquetante dignifiée",
|
||||||
|
"Julie Fauville, la millénariste manipulatrice rabiautante resympathisée",
|
||||||
|
"Eloi Nuns, l'abruzzais stéarique droguant corrélé",
|
||||||
|
"Donat Lemardele, l'écourgeon violet insularisant rebrossé"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
## Live reload
|
## Live reload
|
||||||
|
|
||||||
|
```
|
||||||
cargo watch -x run
|
cargo watch -x run
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build de production
|
||||||
|
|
||||||
|
```
|
||||||
|
cargo build -r
|
||||||
|
```
|
||||||
|
|
||||||
## Remerciements
|
## Remerciements
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,14 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
get {
|
||||||
url: 127.0.0.1:8080?gender=f&dictionaries=prenom patronyme article commun adjectif
|
url: 127.0.0.1:8080?sequence=prenom patronyme virgule article commun adjectif ppresent ppasse&qty=5
|
||||||
body: json
|
body: json
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
gender: f
|
sequence: prenom patronyme virgule article commun adjectif ppresent ppasse
|
||||||
dictionaries: prenom patronyme article commun adjectif
|
qty: 5
|
||||||
|
~gender: f
|
||||||
|
~sep: _
|
||||||
}
|
}
|
||||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -7,6 +7,9 @@ use rust_embed::Embed;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
static ARTICLE: &str = "<article>";
|
static ARTICLE: &str = "<article>";
|
||||||
|
static GENDER_M: &str = "masculins";
|
||||||
|
static GENDER_F: &str = "feminins";
|
||||||
|
static RANDOM: &str = "random";
|
||||||
|
|
||||||
#[derive(FromArgs)]
|
#[derive(FromArgs)]
|
||||||
#[argh(description = "arguments")]
|
#[argh(description = "arguments")]
|
||||||
|
@ -23,9 +26,10 @@ struct Dictionaries;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct NameGenQuery {
|
struct NameGenQuery {
|
||||||
dictionaries: Option<String>,
|
sequence: Option<String>,
|
||||||
gender: Option<String>,
|
gender: Option<String>,
|
||||||
qty: Option<u8>,
|
qty: Option<u8>,
|
||||||
|
sep: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -43,17 +47,15 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
let qty = query.0.qty.unwrap_or(10).min(50).max(1);
|
let qty = query.0.qty.unwrap_or(10).min(50).max(1);
|
||||||
let gender = match query.0.gender.unwrap_or("m".to_string()).as_str() {
|
let sep = query.0.sep.unwrap_or(" ".to_string());
|
||||||
"m" => "masculins",
|
let gender = match query.0.gender.as_ref().map(String::as_ref) {
|
||||||
"f" => "feminins",
|
Some("m") => GENDER_M,
|
||||||
_ => "masculins",
|
Some("f") => GENDER_F,
|
||||||
|
_ => RANDOM,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Limit to 10 parts max
|
// Limit to 10 parts max
|
||||||
let q_dicts = query
|
let q_dicts = query.0.sequence.unwrap_or("prenom patronyme".to_string());
|
||||||
.0
|
|
||||||
.dictionaries
|
|
||||||
.unwrap_or("prenom patronyme".to_string());
|
|
||||||
let mut chosen_dicts = q_dicts.split(" ").collect::<Vec<_>>();
|
let mut chosen_dicts = q_dicts.split(" ").collect::<Vec<_>>();
|
||||||
chosen_dicts.truncate(10);
|
chosen_dicts.truncate(10);
|
||||||
|
|
||||||
|
@ -62,6 +64,15 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
||||||
for _ in 0..qty {
|
for _ in 0..qty {
|
||||||
let mut parts = vec![];
|
let mut parts = vec![];
|
||||||
|
|
||||||
|
let gender = if gender == RANDOM {
|
||||||
|
vec![GENDER_M, GENDER_F]
|
||||||
|
.choose(&mut rand::thread_rng())
|
||||||
|
.unwrap()
|
||||||
|
} else {
|
||||||
|
gender
|
||||||
|
};
|
||||||
|
println!("{gender}");
|
||||||
|
|
||||||
for dict in chosen_dicts.iter() {
|
for dict in chosen_dicts.iter() {
|
||||||
let filename = match *dict {
|
let filename = match *dict {
|
||||||
"adjectif" | "commun" | "ppasse" | "ppresent" | "prenom" => {
|
"adjectif" | "commun" | "ppasse" | "ppresent" | "prenom" => {
|
||||||
|
@ -90,7 +101,7 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
||||||
if let Some(next) = parts.get(i + 1) {
|
if let Some(next) = parts.get(i + 1) {
|
||||||
if next.starts_with(|c| {
|
if next.starts_with(|c| {
|
||||||
vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c)
|
vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c)
|
||||||
}) {
|
} && !next.starts_with("hy")) {
|
||||||
to_replace.push((i, "l'"));
|
to_replace.push((i, "l'"));
|
||||||
} else {
|
} else {
|
||||||
to_replace.push((i, (if gender == "masculins" { "le" } else { "la" })))
|
to_replace.push((i, (if gender == "masculins" { "le" } else { "la" })))
|
||||||
|
@ -103,7 +114,7 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
let joined = parts
|
let joined = parts
|
||||||
.join(" ")
|
.join(&sep)
|
||||||
// Remove space after "l'"
|
// Remove space after "l'"
|
||||||
.replace("l' ", "l'")
|
.replace("l' ", "l'")
|
||||||
// Remove space before comma
|
// Remove space before comma
|
||||||
|
|
Loading…
Reference in New Issue
Block a user