traceRy
a tracery implementation in RTracery is a neat way of generating text from JSON files. I didn't write it but you can find out more here. You can use tracery to make bots with cheapbotsdonequick. I do that a lot, so I thought it would be nice to have something written in R that I can use to check that my bots work (also because I'm goofy and I don't understand javascript).
traceRy
comes with some properly formatted JSON to help you get started but let's start with something really simple to begin with...
my_json <- '{
"origin": ["Today I saw a #bird#"],
"bird": ["mallard", "gannet", "swan", "puffin"]
}
'
# write that out to a temporary file
tmp <- tempfile()
writeLines(my_json, tmp)
with this simple story we can generate stuff...
# load traceRy
library(traceRy)
# load the JSON
jason <- read_tracery(tmp)
# write a story
make_tracery(jason)
## [1] "Today I saw a mallard"
Okay, well that was cute, but what about something more fancy?
my_json <- '{
"origin": ["Today I saw a #bird##andsome#"],
"bird": ["mallard", "gannet", "swan", "puffin"],
"andsome": ["", " and some #bird.s#"]
}
'
tmp <- tempfile()
writeLines(my_json, tmp)
jason <- read_tracery(tmp)
set.seed(23)
make_tracery(jason)
## [1] "Today I saw a swan"
make_tracery(jason)
## [1] "Today I saw a gannet and some puffins"
Maybe sometimes we get really excited...
my_json <- '{
"origin": ["Today I saw a #bird##andsome#"],
"bird": ["mallard", "gannet", "swan", "puffin"],
"andsome": ["", " and some #bird.s#", " and some #bird.s.capitalize#"]
}
'
tmp <- tempfile()
writeLines(my_json, tmp)
jason <- read_tracery(tmp)
set.seed(12)
make_tracery(jason)
## [1] "Today I saw a mallard and some PUFFINS"
Okay that's probably enough for now...
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.