Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate clap;
- extern crate toml;
- extern crate rand;
- mod data;
- // We need the thing that converts strings to our new datatype.
- use std::str::FromStr;
- // and the TOML parser and so on:
- use toml::{Parser, Value};
- use rand::{thread_rng, Rng};
- use clap::{App, Arg};
- use std::collections::HashMap;
- // Just a bit of abbreviation because we are going to use this a lot:
- type WordList = Vec<(PartOfSpeech, String)>;
- fn main() {
- let rng = thread_rng(); // Make an RNG
- let structures_obj = Parser::new(data::structures).parse()
- .unwrap() // Parse and get the POS data – type ref to type toml::Table, so
- .get(&"structures".to_string()).unwrap() // returns &toml::Value::Array(Vec<Value>), which is a reference, so
- .clone() // we need to copy it in order to comply with the lifetimes – removes teh &
- .as_slice().unwrap() // Take the slice – type now &[Values]
- .to_vec(); // Transform it back into Vec<Value>, and changes the thing it names to boot.
- // Or to_owned: the important thing is that
- // the name now refers to a vector of values that used be inside the slice.
- // (Note that things expire in a method chain after execution passes through it!)
- let structures = structures_obj.as_slice() // extract array
- .unwrap();
- // Metadata
- let matches = App::new("wordgen")
- .version("1.0")
- .about("Generate Acronyms")
- .arg(Arg::with_name("CHARS") // The argument: string of characters to be expanded
- .help("The acronym to generate from")
- .required(true)
- .index(1))
- .get_matches(); // Don't care about the app itself, just the args it returns
- // The word list
- let word_list: WordList = data::mobyposi.split('\n')
- .map(|line| { // For each line on the file, do:
- // The split operation:
- // Split on the × symbol, (as per spec),
- // Then put it back together as a vector of whatever
- let temp = line.split('×').collect::<Vec<_>>();
- // Finally split it apart and return
- [temp[1].parse().unwrap(), temp[0].to_string()]})
- .collect();
- let word_list_hashmap: HashMap<PartOfSpeech, Vec<String>> = HashMap::new();
- let input = matches.value_of("CHARS")
- .unwrap(); // safe to unwrap because CLAP will die if CHARS is absent because it's required.
- }
- fn gen_acronym<S1 ,S2, R>(init: &S1,
- characters: &S2,
- rng: &mut R,
- words: &WordList,
- structures: Vec<&Value>) -> String
- where S1: ToString, S2: ToString, R: Rng
- {
- // Coerce to stuff you can work with. Shadows previous bindings.
- let init = init.to_string();
- let char_take = rng.gen_range(1, structures.len() + 1);
- let characters = characters.to_string()[..char_take].to_string(); // Cut out a bit
- // More to do…
- String::new()
- }
- // Define Part-of-speech datatype.
- #[derive(Debug, PartialEq, Eq, Hash)]
- enum PartOfSpeech {
- Nouns, // Noun N
- Plurals, // Plural p
- NounPhrases, // Noun Phrase h
- Verbs, // Verb (usu participle) V
- TransitiveVerbs, // Verb (transitive) t
- IntransitiveVerbs, // Verb (intransitive) i
- Adjectives, // Adjective A
- Adverbs, // Adverb v
- Conjunctions, // Conjunction C
- Prepositions, // Preposition P
- Interjections, // Interjection !
- Pronouns, // Pronoun r
- DefiniteArticles, // Definite Article D
- IndefiniteArticles, // Indefinite Article I
- Nominatives, // Nominative o
- Other(String) // all other possible values have type string
- }
- impl FromStr for PartOfSpeech {
- // There is no way parsing can go wrong
- // because anything that's not in the list becomes Other.
- type Err = ();
- // The method:
- fn from_str(s:&str) -> Result<Self, Self::Err> {
- // For ease of typing
- // we add everything in PartOfSpeech into the global.
- // This will go away after we leave the scope.
- use PartOfSpeech::*;
- // Run
- Ok(match s {
- "nouns" | "N" => Nouns,
- "plurals" | "p" => Plurals,
- "noun-phrases" | "h" => NounPhrases,
- "verbs" | "V" => Verbs,
- "transitive-verbs" | "t" => TransitiveVerbs,
- "intransitive-verbs" | "i" => IntransitiveVerbs,
- "adjectives" | "A" => Adjectives,
- "adverbs" | "v" => Adverbs,
- "conjunctions" | "C" => Conjunctions,
- "prepositions" | "P" => Prepositions,
- "interjections" | "!" => Interjections,
- "pronouns" | "r" => Pronouns,
- "definite-articles" | "D" => DefiniteArticles,
- "indef-articles" | "I" => IndefiniteArticles,
- "nominatives" | "o" => Nominatives,
- //
- other => Other(other.to_string())
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment