The namegen5 website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.9 KiB

3 years ago
3 years ago
3 years ago
  1. use std::process;
  2. use std::env;
  3. use std::fs::{read_dir, File};
  4. use std::collections::HashMap;
  5. use namegen_compiler::manifest::Manifest;
  6. use std::env::join_paths;
  7. use std::path::Path;
  8. use namegen_compiler::input::SourceCollection;
  9. use glob::glob;
  10. use std::io::{Read};
  11. use namegen::{Name, NamePart};
  12. use namegen_compiler::output::{Output, Collection, CollectionItem};
  13. const COMMON_FORMAT_NAMES: &'static [&str] = &[
  14. "full_name", "full_name:female", "full_name:male",
  15. ];
  16. fn main() {
  17. let source_dir = match env::var("SOURCE_DIR") {
  18. Ok(v) => v,
  19. Err(_) => {
  20. eprintln!("SOURCE_DIR missing");
  21. process::exit(1);
  22. }
  23. };
  24. let dest_dir = match env::var("DESTINATION_DIR") {
  25. Ok(v) => v,
  26. Err(_) => {
  27. eprintln!("DESTINATION_DIR missing");
  28. process::exit(1);
  29. }
  30. };
  31. let mut data = String::with_capacity(2048);
  32. let mut collection = Collection{items: Vec::new()};
  33. for entry in read_dir(&source_dir).unwrap() {
  34. let entry = entry.unwrap();
  35. if !entry.file_type().unwrap().is_file() {
  36. continue;
  37. }
  38. let path = Path::new(&source_dir).join(entry.file_name());
  39. if path.to_str().unwrap().contains(".drone.yml") {
  40. continue;
  41. }
  42. eprintln!("Loading manifest {}", path.to_str().unwrap());
  43. let manifest: Manifest = serde_yaml::from_reader(File::open(path.clone()).unwrap()).unwrap();
  44. let mut coll = SourceCollection::default();
  45. for source in manifest.sources.iter() {
  46. let path = path.with_file_name(&source.files);
  47. for entry in glob(path.to_str().unwrap()).expect("Failed to read glob pattern") {
  48. let path = entry.expect("Invalid file entry.");
  49. data.clear();
  50. File::open(path.clone()).unwrap().read_to_string(&mut data);
  51. eprintln!("Loading source {} (size: {})", path.clone().to_str().unwrap(), data.len());
  52. match source.kind.as_str() {
  53. "full_names" => { coll.load_full_names(&data, source).unwrap() }
  54. "labeled_groups" => { coll.load_labeled_groups(&data, source).unwrap() }
  55. _ => {
  56. eprintln!("Unknown source file kind {}", source.kind);
  57. process::exit(1);
  58. }
  59. }
  60. }
  61. }
  62. let mut name = Name::new();
  63. for part in manifest.parts.iter() {
  64. eprintln!("Building part {}...", &part.name);
  65. let source = coll.source(&part.source).expect("Source not found.");
  66. let mut part = match part.kind.as_str() {
  67. "cfgrammar" => NamePart::new_cfgrammar(
  68. &part.name, &part.format_rules, &part.initial_tokens,
  69. part.rules.rlf, part.rules.ral,
  70. ),
  71. "markov" => NamePart::new_markov(
  72. &part.name, &part.format_rules, &part.initial_tokens,
  73. part.rules.lrs, part.rules.lrm, part.rules.lre, part.rules.rlf,
  74. ),
  75. "wordlist" => NamePart::new_wordlist(
  76. &part.name, &part.format_rules
  77. ),
  78. _ => {
  79. eprintln!("Unknown part kind {}", &part.kind);
  80. process::exit(1);
  81. },
  82. };
  83. for set in source.sets() {
  84. if set.labels().len() == 0 {
  85. eprintln!("\tSample list: {} samples", set.samples().len());
  86. } else {
  87. eprintln!("\tSample group: {}", set.labels().join(" "));
  88. }
  89. part.learn(set).unwrap()
  90. }
  91. name.add_part(part);
  92. }
  93. for format in manifest.formats.iter() {
  94. name.add_format(&format.name, &format.template)
  95. }
  96. let mut examples = HashMap::new();
  97. for format in name.formats() {
  98. if let Some(gen) = name.generate(format.name()) {
  99. examples.insert(format.name().to_owned(), Vec::with_capacity(40));
  100. for (i, result) in gen.enumerate().take(40) {
  101. examples.get_mut(format.name()).unwrap().push(result);
  102. }
  103. }
  104. }
  105. eprint!("\n");
  106. eprintln!("Sample output");
  107. for format_name in COMMON_FORMAT_NAMES.iter() {
  108. if let Some(gen) = name.generate(format_name) {
  109. eprint!("\n ");
  110. for (i, result) in gen.enumerate().take(72) {
  111. eprint!("{result:<width$} ", result = result, width = 19);
  112. if i > 0 && (i % 4 == 3) {
  113. eprint!("\n ");
  114. }
  115. }
  116. eprint!("\n");
  117. }
  118. }
  119. if let Some(gen) = name.generate("long_full_name:male") {
  120. eprint!("\n ");
  121. for (i, result) in gen.enumerate().take(16) {
  122. eprint!("{result:<width$} ", result = result, width = 52);
  123. if i > 0 && (i % 2 == 1) {
  124. eprint!("\n ");
  125. }
  126. }
  127. eprint!("\n");
  128. }
  129. let path = Path::new(&dest_dir).join(format!("{}.json", &manifest.name));
  130. let mut output = Output{
  131. name: manifest.name.clone(),
  132. metadata: manifest.metadata.clone(),
  133. data: name,
  134. examples: examples,
  135. };
  136. eprintln!("Writing {}", path.clone().to_str().unwrap());
  137. serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &output).expect("Writing output failed.");
  138. collection.items.push(CollectionItem{name: output.name.clone(), metadata: output.metadata.clone()});
  139. eprint!("\n\n");
  140. }
  141. let path = Path::new(&dest_dir).join("_collection.json");
  142. serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &collection).expect("Writing collection failed.");
  143. }