Isoraqathedh

wordgen main.rs

Nov 15th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.43 KB | None | 0 0
  1. extern crate clap;
  2. extern crate toml;
  3. extern crate rand;
  4.  
  5. mod data;
  6.  
  7. // We need the thing that converts strings to our new datatype.
  8. use std::str::FromStr;
  9. // and the TOML parser and so on:
  10. use toml::{Parser, Value};
  11. use rand::{thread_rng, Rng};
  12. use clap::{App, Arg};
  13. use std::collections::HashMap;
  14.  
  15. // Just a bit of abbreviation because we are going to use this a lot:
  16. type WordList = Vec<(PartOfSpeech, String)>;
  17.  
  18.  
  19. fn main() {
  20.     let rng = thread_rng(); // Make an RNG
  21.     let structures_obj = Parser::new(data::structures).parse()
  22.         .unwrap() // Parse and get the POS data – type ref to type toml::Table, so
  23.         .get(&"structures".to_string()).unwrap() // returns &toml::Value::Array(Vec<Value>), which is a reference, so
  24.         .clone() // we need to copy it in order to comply with the lifetimes – removes teh &
  25.         .as_slice().unwrap() // Take the slice – type now &[Values]
  26.         .to_vec(); // Transform it back into Vec<Value>, and changes the thing it names to boot.
  27.     // Or to_owned: the important thing is that
  28.     // the name now refers to a vector of values that used be inside the slice.
  29.     // (Note that things expire in a method chain after execution passes through it!)
  30.     let structures = structures_obj.as_slice() // extract array
  31.         .unwrap();
  32.    
  33.     // Metadata
  34.     let matches = App::new("wordgen")
  35.         .version("1.0")
  36.         .about("Generate Acronyms")
  37.         .arg(Arg::with_name("CHARS") // The argument: string of characters to be expanded
  38.              .help("The acronym to generate from")
  39.              .required(true)
  40.              .index(1))
  41.         .get_matches(); // Don't care about the app itself, just the args it returns
  42.     // The word list
  43.     let word_list: WordList = data::mobyposi.split('\n')
  44.         .map(|line| { // For each line on the file, do:
  45.             // The split operation:
  46.             // Split on the × symbol, (as per spec),
  47.             // Then put it back together as a vector of whatever
  48.             let temp = line.split('×').collect::<Vec<_>>();
  49.              // Finally split it apart and return
  50.             [temp[1].parse().unwrap(), temp[0].to_string()]})
  51.         .collect();
  52.     let word_list_hashmap: HashMap<PartOfSpeech, Vec<String>> = HashMap::new();
  53.    
  54.     let input = matches.value_of("CHARS")
  55.         .unwrap(); // safe to unwrap because CLAP will die if CHARS is absent because it's required.
  56.  
  57. }
  58.  
  59. fn gen_acronym<S1 ,S2, R>(init: &S1,
  60.                           characters: &S2,
  61.                           rng: &mut R,
  62.                           words: &WordList,
  63.                           structures: Vec<&Value>) -> String
  64.     where S1: ToString, S2: ToString, R: Rng
  65. {
  66.     // Coerce to stuff you can work with. Shadows previous bindings.
  67.     let init = init.to_string();
  68.    
  69.     let char_take = rng.gen_range(1, structures.len() + 1);
  70.  
  71.     let characters = characters.to_string()[..char_take].to_string(); // Cut out a bit
  72.  
  73.     // More to do…
  74.  
  75.     String::new()
  76. }
  77.  
  78. // Define Part-of-speech datatype.
  79. #[derive(Debug, PartialEq, Eq, Hash)]
  80. enum PartOfSpeech {
  81.     Nouns,              // Noun                            N
  82.     Plurals,            // Plural                          p
  83.     NounPhrases,        // Noun Phrase                     h
  84.     Verbs,              // Verb (usu participle)           V
  85.     TransitiveVerbs,    // Verb (transitive)               t
  86.     IntransitiveVerbs,  // Verb (intransitive)             i
  87.     Adjectives,         // Adjective                       A
  88.     Adverbs,            // Adverb                          v
  89.     Conjunctions,       // Conjunction                     C
  90.     Prepositions,       // Preposition                     P
  91.     Interjections,      // Interjection                    !
  92.     Pronouns,           // Pronoun                         r
  93.     DefiniteArticles,   // Definite Article                D
  94.     IndefiniteArticles, // Indefinite Article              I
  95.     Nominatives,        // Nominative                      o
  96.     Other(String)       // all other possible values have type string
  97. }
  98. impl FromStr for PartOfSpeech {
  99.     // There is no way parsing can go wrong
  100.     // because anything that's not in the list becomes Other.
  101.     type Err = ();
  102.     // The method:
  103.     fn from_str(s:&str) -> Result<Self, Self::Err> {
  104.         // For ease of typing
  105.         // we add everything in PartOfSpeech into the global.
  106.         // This will go away after we leave the scope.
  107.         use PartOfSpeech::*;
  108.         // Run
  109.         Ok(match s {
  110.             "nouns"              | "N" => Nouns,
  111.             "plurals"            | "p" => Plurals,
  112.             "noun-phrases"       | "h" => NounPhrases,
  113.             "verbs"              | "V" => Verbs,
  114.             "transitive-verbs"   | "t" => TransitiveVerbs,
  115.             "intransitive-verbs" | "i" => IntransitiveVerbs,
  116.             "adjectives"         | "A" => Adjectives,
  117.             "adverbs"            | "v" => Adverbs,
  118.             "conjunctions"       | "C" => Conjunctions,
  119.             "prepositions"       | "P" => Prepositions,
  120.             "interjections"      | "!" => Interjections,
  121.             "pronouns"           | "r" => Pronouns,
  122.             "definite-articles"  | "D" => DefiniteArticles,
  123.             "indef-articles"     | "I" => IndefiniteArticles,
  124.             "nominatives"        | "o" => Nominatives,
  125.             //
  126.             other => Other(other.to_string())      
  127.         })
  128.            
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment