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.

172 lines
5.8 KiB

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. eprintln!("Loading manifest {}", path.to_str().unwrap());
  40. let manifest: Manifest = serde_yaml::from_reader(File::open(path.clone()).unwrap()).unwrap();
  41. let mut coll = SourceCollection::default();
  42. for source in manifest.sources.iter() {
  43. let path = path.with_file_name(&source.files);
  44. for entry in glob(path.to_str().unwrap()).expect("Failed to read glob pattern") {
  45. let path = entry.expect("Invalid file entry.");
  46. data.clear();
  47. File::open(path.clone()).unwrap().read_to_string(&mut data);
  48. eprintln!("Loading source {} (size: {})", path.clone().to_str().unwrap(), data.len());
  49. match source.kind.as_str() {
  50. "full_names" => { coll.load_full_names(&data, source).unwrap() }
  51. "labeled_groups" => { coll.load_labeled_groups(&data, source).unwrap() }
  52. _ => {
  53. eprintln!("Unknown source file kind {}", source.kind);
  54. process::exit(1);
  55. }
  56. }
  57. }
  58. }
  59. let mut name = Name::new();
  60. for part in manifest.parts.iter() {
  61. eprintln!("Building part {}...", &part.name);
  62. let source = coll.source(&part.source).expect("Source not found.");
  63. let mut part = match part.kind.as_str() {
  64. "cfgrammar" => NamePart::new_cfgrammar(
  65. &part.name, &part.format_rules, &part.initial_tokens,
  66. part.rules.rlf, part.rules.ral,
  67. ),
  68. "markov" => NamePart::new_markov(
  69. &part.name, &part.format_rules, &part.initial_tokens,
  70. part.rules.lrs, part.rules.lrm, part.rules.lre, part.rules.rlf,
  71. ),
  72. "wordlist" => NamePart::new_wordlist(
  73. &part.name, &part.format_rules
  74. ),
  75. _ => {
  76. eprintln!("Unknown part kind {}", &part.kind);
  77. process::exit(1);
  78. },
  79. };
  80. for set in source.sets() {
  81. if set.labels().len() == 0 {
  82. eprintln!("\tSample list: {} samples", set.samples().len());
  83. } else {
  84. eprintln!("\tSample group: {}", set.labels().join(" "));
  85. }
  86. part.learn(set).unwrap()
  87. }
  88. name.add_part(part);
  89. }
  90. for format in manifest.formats.iter() {
  91. name.add_format(&format.name, &format.template)
  92. }
  93. let mut examples = HashMap::new();
  94. for format in name.formats() {
  95. if let Some(gen) = name.generate(format.name()) {
  96. examples.insert(format.name().to_owned(), Vec::with_capacity(40));
  97. for (i, result) in gen.enumerate().take(40) {
  98. examples.get_mut(format.name()).unwrap().push(result);
  99. }
  100. }
  101. }
  102. eprint!("\n");
  103. eprintln!("Sample output");
  104. for format_name in COMMON_FORMAT_NAMES.iter() {
  105. if let Some(gen) = name.generate(format_name) {
  106. eprint!("\n ");
  107. for (i, result) in gen.enumerate().take(72) {
  108. eprint!("{result:<width$} ", result = result, width = 19);
  109. if i > 0 && (i % 4 == 3) {
  110. eprint!("\n ");
  111. }
  112. }
  113. eprint!("\n");
  114. }
  115. }
  116. if let Some(gen) = name.generate("long_full_name:male") {
  117. eprint!("\n ");
  118. for (i, result) in gen.enumerate().take(16) {
  119. eprint!("{result:<width$} ", result = result, width = 52);
  120. if i > 0 && (i % 2 == 1) {
  121. eprint!("\n ");
  122. }
  123. }
  124. eprint!("\n");
  125. }
  126. let path = Path::new(&dest_dir).join(format!("{}.json", &manifest.name));
  127. let mut output = Output{
  128. name: manifest.name.clone(),
  129. metadata: manifest.metadata.clone(),
  130. data: name,
  131. examples: examples,
  132. };
  133. eprintln!("Writing {}", path.clone().to_str().unwrap());
  134. serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &output).expect("Writing output failed.");
  135. collection.items.push(CollectionItem{name: output.name.clone(), metadata: output.metadata.clone()});
  136. eprint!("\n\n");
  137. }
  138. let path = Path::new(&dest_dir).join("_collection.json");
  139. serde_json::to_writer(File::create(path).expect("Opening output filed failed"), &collection).expect("Writing collection failed.");
  140. }