add more types of text generator

main
xenofem 2 years ago
parent 1ff66780c7
commit 86b02a7533
  1. 184
      Cargo.lock
  2. 6
      Cargo.toml
  3. 8
      config.toml.example
  4. 97
      src/main.rs

184
Cargo.lock generated

@ -185,6 +185,12 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b"
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
[[package]]
name = "encoding_rs"
version = "0.8.23"
@ -200,6 +206,12 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]]
name = "fixedbitset"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33"
[[package]]
name = "flate2"
version = "1.0.14"
@ -218,6 +230,12 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
@ -342,6 +360,18 @@ dependencies = [
"winapi 0.3.8",
]
[[package]]
name = "generator-bot"
version = "0.2.0"
dependencies = [
"markov",
"purrchance",
"serde",
"serenity",
"tokio",
"toml",
]
[[package]]
name = "generic-array"
version = "0.12.3"
@ -351,6 +381,15 @@ dependencies = [
"typenum",
]
[[package]]
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
"unicode-width",
]
[[package]]
name = "getrandom"
version = "0.1.14"
@ -362,17 +401,6 @@ dependencies = [
"wasi",
]
[[package]]
name = "grammar-bot"
version = "0.1.0"
dependencies = [
"purrchance",
"serde",
"serenity",
"tokio",
"toml",
]
[[package]]
name = "h2"
version = "0.2.5"
@ -497,6 +525,15 @@ dependencies = [
"libc",
]
[[package]]
name = "itertools"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "0.4.6"
@ -547,6 +584,12 @@ version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "log"
version = "0.4.8"
@ -567,6 +610,21 @@ dependencies = [
"scoped-tls",
]
[[package]]
name = "markov"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb8fec2128b3f71ba0fcc136f18b1f20568eb18aa3a08e80b5d65c9fbf33d08"
dependencies = [
"getopts",
"itertools",
"petgraph",
"rand 0.3.23",
"serde",
"serde_derive",
"serde_yaml",
]
[[package]]
name = "matches"
version = "0.1.8"
@ -721,12 +779,28 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "ordermap"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "petgraph"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f"
dependencies = [
"fixedbitset",
"ordermap",
]
[[package]]
name = "pin-project"
version = "0.4.22"
@ -788,12 +862,12 @@ dependencies = [
[[package]]
name = "purrchance"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "865ca962cfe1c073586b84b6b5cfd8247ea95e74273907b184b2354d7a3de31b"
checksum = "1b314e5ee146272547b6f106f36aa7ff3c1756f7858e5cd91488b527cd48888b"
dependencies = [
"nom",
"rand",
"rand 0.7.3",
]
[[package]]
@ -805,6 +879,29 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
dependencies = [
"libc",
"rand 0.4.6",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi 0.3.8",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -814,7 +911,7 @@ dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_core 0.5.1",
"rand_hc",
]
@ -825,9 +922,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
@ -843,7 +955,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
@ -1018,6 +1139,18 @@ dependencies = [
"url",
]
[[package]]
name = "serde_yaml"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef8099d3df28273c99a1728190c7a9f19d444c941044f64adf986bee7ec53051"
dependencies = [
"dtoa",
"linked-hash-map",
"serde",
"yaml-rust",
]
[[package]]
name = "serenity"
version = "0.8.6"
@ -1216,7 +1349,7 @@ dependencies = [
"httparse",
"input_buffer",
"log",
"rand",
"rand 0.7.3",
"sha-1",
"url",
"utf-8",
@ -1255,6 +1388,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
[[package]]
name = "unicode-xid"
version = "0.2.0"
@ -1464,3 +1603,12 @@ dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]]
name = "yaml-rust"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d"
dependencies = [
"linked-hash-map",
]

@ -1,6 +1,6 @@
[package]
name = "grammar-bot"
version = "0.1.0"
name = "generator-bot"
version = "0.2.0"
authors = ["xenofem <xenofem@xeno.science>"]
license = "MIT"
edition = "2018"
@ -8,9 +8,11 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
markov = "1.0.3"
purrchance = "0.4.1"
serde = "1.0.112"
toml = "0.5.6"
# tracery = "0.1.0"
[dependencies.serenity]
git = "https://github.com/Lakelezz/serenity/"

@ -1,6 +1,8 @@
grammar_path = "grammar"
grammar_type = "perchance"
grammar_symbol = "output"
token = "t0k3n"
channel_id = 0000000000000000
interval = 7200
[generator]
type = "perchance"
path = "grammar"
symbol = "output"

@ -1,10 +1,12 @@
extern crate markov;
extern crate purrchance;
extern crate serde;
extern crate serenity;
extern crate tokio;
extern crate toml;
// extern crate tracery;
use purrchance::{Grammar, Purrchance, Symbol, parser::load_grammar};
use purrchance::Purrchance;
use serde::Deserialize;
use serenity::{
async_trait,
@ -15,32 +17,64 @@ use std::fs::read_to_string;
use std::sync::Arc;
use std::time::Duration;
enum TextGenerator {
Perchance { grammar: purrchance::Grammar, symbol: purrchance::Symbol },
// Tracery(tracery::Grammar),
Markov(markov::Chain<String>),
}
impl TextGenerator {
fn generate(&self) -> String {
match self {
TextGenerator::Perchance { grammar, symbol } => symbol.eval(&grammar).unwrap(),
// TextGenerator::Tracery(grammar) => grammar.flatten(),
TextGenerator::Markov(chain) => chain.generate_str(),
}
}
}
#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
enum GrammarType {
Perchance,
#[serde(tag = "type", rename_all = "lowercase")]
enum GeneratorSpec {
Perchance { path: String, symbol: String },
// Tracery { path: String, rule: String },
Markov { path: String, order: usize },
}
impl GeneratorSpec {
fn load(&self) -> TextGenerator {
match self {
GeneratorSpec::Perchance { path: p, symbol: s } => {
let grammar = purrchance::parser::load_grammar(&read_to_string(p).unwrap()).unwrap();
let symbol = purrchance::Symbol::NonTerminal(String::from(s));
TextGenerator::Perchance { grammar, symbol }
}
/*
GeneratorSpec::Tracery { path, rule } => {
let mut grammar = tracery::from_json(read_to_string(path).unwrap()).unwrap();
grammar.default_rule(rule);
TextGenerator::Tracery(grammar)
}
*/
GeneratorSpec::Markov { path, order } => {
let mut chain = markov::Chain::of_order(*order);
for line in read_to_string(path).unwrap().lines() {
chain.feed_str(line);
}
TextGenerator::Markov(chain)
}
}
}
}
#[derive(Deserialize)]
struct Config {
grammar_path: String,
grammar_type: GrammarType,
grammar_symbol: String,
generator: GeneratorSpec,
token: String,
channel_id: ChannelId,
interval: u64,
}
struct TextGenerator {
grammar: Grammar,
symbol: Symbol,
}
impl TextGenerator {
pub fn generate(&self) -> String {
self.symbol.eval(&self.grammar).unwrap()
}
}
impl TypeMapKey for TextGenerator {
type Value = TextGenerator;
@ -118,24 +152,19 @@ impl EventHandler for Handler {
async fn main() {
let config: Config = toml::from_str(&read_to_string("config.toml").unwrap()).unwrap();
match config.grammar_type {
GrammarType::Perchance => {
let grammar = load_grammar(&read_to_string(config.grammar_path).unwrap()).unwrap();
let symbol = Symbol::NonTerminal(config.grammar_symbol);
let generator = config.generator.load();
let mut client = Client::new(&config.token)
.event_handler(Handler)
.await
.expect("err creating client");
let mut client = Client::new(&config.token)
.event_handler(Handler)
.await
.expect("err creating client");
{
let mut data = client.data.write().await;
data.insert::<TextGenerator>(TextGenerator { grammar, symbol });
data.insert::<Interval>(config.interval);
data.insert::<ChanId>(config.channel_id);
data.insert::<LoopStatus>(false);
}
client.start().await.unwrap();
}
{
let mut data = client.data.write().await;
data.insert::<TextGenerator>(generator);
data.insert::<Interval>(config.interval);
data.insert::<ChanId>(config.channel_id);
data.insert::<LoopStatus>(false);
}
client.start().await.unwrap();
}

Loading…
Cancel
Save