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
|
||||
|
||||
## 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
|
||||
|
||||
```
|
||||
cargo watch -x run
|
||||
```
|
||||
|
||||
## Build de production
|
||||
|
||||
```
|
||||
cargo build -r
|
||||
```
|
||||
|
||||
## Remerciements
|
||||
|
||||
|
|
|
@ -5,12 +5,14 @@ meta {
|
|||
}
|
||||
|
||||
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
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
gender: f
|
||||
dictionaries: prenom patronyme article commun adjectif
|
||||
sequence: prenom patronyme virgule article commun adjectif ppresent ppasse
|
||||
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};
|
||||
|
||||
static ARTICLE: &str = "<article>";
|
||||
static GENDER_M: &str = "masculins";
|
||||
static GENDER_F: &str = "feminins";
|
||||
static RANDOM: &str = "random";
|
||||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(description = "arguments")]
|
||||
|
@ -23,9 +26,10 @@ struct Dictionaries;
|
|||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct NameGenQuery {
|
||||
dictionaries: Option<String>,
|
||||
sequence: Option<String>,
|
||||
gender: Option<String>,
|
||||
qty: Option<u8>,
|
||||
sep: Option<String>,
|
||||
}
|
||||
|
||||
#[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 gender = match query.0.gender.unwrap_or("m".to_string()).as_str() {
|
||||
"m" => "masculins",
|
||||
"f" => "feminins",
|
||||
_ => "masculins",
|
||||
let sep = query.0.sep.unwrap_or(" ".to_string());
|
||||
let gender = match query.0.gender.as_ref().map(String::as_ref) {
|
||||
Some("m") => GENDER_M,
|
||||
Some("f") => GENDER_F,
|
||||
_ => RANDOM,
|
||||
};
|
||||
|
||||
// Limit to 10 parts max
|
||||
let q_dicts = query
|
||||
.0
|
||||
.dictionaries
|
||||
.unwrap_or("prenom patronyme".to_string());
|
||||
let q_dicts = query.0.sequence.unwrap_or("prenom patronyme".to_string());
|
||||
let mut chosen_dicts = q_dicts.split(" ").collect::<Vec<_>>();
|
||||
chosen_dicts.truncate(10);
|
||||
|
||||
|
@ -62,6 +64,15 @@ async fn index(query: web::Query<NameGenQuery>) -> HttpResponse {
|
|||
for _ in 0..qty {
|
||||
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() {
|
||||
let filename = match *dict {
|
||||
"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 next.starts_with(|c| {
|
||||
vec!['a', 'e', 'i', 'o', 'u', 'é', 'è', 'ê', 'h'].contains(&c)
|
||||
}) {
|
||||
} && !next.starts_with("hy")) {
|
||||
to_replace.push((i, "l'"));
|
||||
} else {
|
||||
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
|
||||
.join(" ")
|
||||
.join(&sep)
|
||||
// Remove space after "l'"
|
||||
.replace("l' ", "l'")
|
||||
// Remove space before comma
|
||||
|
|
Loading…
Reference in New Issue
Block a user