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.

144 lines
4.0 KiB

4 years ago
  1. use std::collections::BTreeMap;
  2. use namegen::{SampleSet, Sample};
  3. use crate::manifest::SourceManifest;
  4. #[derive(Default)]
  5. pub struct SourceCollection {
  6. sources: BTreeMap<String, Source>
  7. }
  8. impl SourceCollection {
  9. pub fn sources(&self) -> impl Iterator<Item = (&str, &Source)> {
  10. self.sources.iter().map(|(k, v)| (k.as_str(), v))
  11. }
  12. pub fn source(&self, name: &str) -> Option<&Source> {
  13. self.sources.get(name)
  14. }
  15. pub fn source_mut(&mut self, name: &str) -> &mut Source {
  16. if !self.sources.contains_key(name) {
  17. self.sources.insert(name.to_owned(), Source::default());
  18. }
  19. self.sources.get_mut(name).unwrap()
  20. }
  21. pub fn load_full_names(&mut self, data: &str, source: &SourceManifest) -> Result<(), &'static str> {
  22. for line in data.lines() {
  23. if line.len() < 1 {
  24. continue
  25. }
  26. if let Some(prefix) = &source.comment_prefix {
  27. if line.starts_with(prefix) {
  28. continue;
  29. }
  30. }
  31. for (i, token) in line.split(' ').enumerate() {
  32. if let Some(empty_token) = &source.empty_token {
  33. if token == empty_token.as_str() {
  34. continue;
  35. }
  36. }
  37. let mut target = self.source_mut(&source.targets[i]);
  38. let mut token = if source.to_lower {
  39. token.to_owned().to_lowercase()
  40. } else {
  41. token.to_owned()
  42. };
  43. target.set_mut("list").add_sample(Sample::Word(token))
  44. }
  45. }
  46. Ok(())
  47. }
  48. pub fn load_labeled_groups(&mut self, data: &str, source: &SourceManifest) -> Result<(), &'static str> {
  49. let mut target_index = 0usize;
  50. let mut new_group = true;
  51. let mut current_key = String::new();
  52. for line in data.lines() {
  53. if line.len() < 1 {
  54. new_group = true;
  55. continue;
  56. }
  57. if let Some(separator) = &source.separator {
  58. if line.starts_with(separator) {
  59. target_index += 1;
  60. new_group;
  61. continue;
  62. }
  63. }
  64. if let Some(prefix) = &source.comment_prefix {
  65. if line.starts_with(prefix) {
  66. continue;
  67. }
  68. }
  69. let mut target = self.source_mut(&source.targets[target_index]);
  70. if new_group {
  71. current_key = line.to_owned();
  72. let labels: Vec<String> = line.split(' ').map(|s| s.to_owned()).collect();
  73. target.set_labeled_mut(line, labels.as_slice());
  74. new_group = false;
  75. continue;
  76. }
  77. let line = if source.to_lower {
  78. line.to_owned().to_lowercase()
  79. } else {
  80. line.to_owned()
  81. };
  82. let tokens: Vec<String> = line.split(' ').map(|s| s.to_owned()).collect();
  83. let set = target.set_mut(current_key.as_str());
  84. if tokens.len() != set.labels().len() {
  85. return Err("Token length != label count");
  86. }
  87. set.add_sample(
  88. Sample::Tokens(tokens)
  89. );
  90. }
  91. Ok(())
  92. }
  93. }
  94. #[derive(Default)]
  95. pub struct Source {
  96. sets: BTreeMap<String, SampleSet>
  97. }
  98. impl Source {
  99. pub fn sets(&self) -> impl Iterator<Item = &SampleSet> {
  100. self.sets.values()
  101. }
  102. pub fn set_mut(&mut self, name: &str) -> &mut SampleSet {
  103. if !self.sets.contains_key(name) {
  104. self.sets.insert(name.to_owned(), SampleSet::new());
  105. }
  106. self.sets.get_mut(name).unwrap()
  107. }
  108. pub fn set_labeled_mut<S: AsRef<str>>(&mut self, name: &str, labels: &[S]) -> &mut SampleSet {
  109. if !self.sets.contains_key(name) {
  110. self.sets.insert(name.to_owned(), SampleSet::with_labels(labels));
  111. }
  112. self.sets.get_mut(name).unwrap()
  113. }
  114. }