-
Notifications
You must be signed in to change notification settings - Fork 0
/
wine_ngram_shiny.R
96 lines (71 loc) · 2.19 KB
/
wine_ngram_shiny.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# --- libs --- #
library(shiny)
library(tidyverse)
library(ngram)
# --- source --- #
texts = read_tsv('wine_ngram_texts.tsv')
# This is based on a kaggle dataset.
# Only varieties with "enough" reviews were kept.
# Source:
# https://www.kaggle.com/zynicide/wine-reviews
# the possible varieties
variety_choices = texts %>%
ungroup %>%
distinct(variety) %>%
pull(variety) %>%
sort()
# get category (good or bad wine) and variety (see above), return review
writeNotes = function(my_category, my_variety){
my_text = texts %>%
filter(category == my_category, variety == my_variety) %>%
pull(text)
# build 4grams from texts
my_ngram = ngram(my_text, n = 4, sep = ' ')
# throw a die
my_seed = round(runif(1, 1, 1000), 0)
# make 4gram babble
my_notes = babble(my_ngram, genlen = 120, seed = my_seed) %>%
str_replace('[^\\.]*. ', '') %>% # drop first sentence
str_replace('[\\.]([^\\.]*)$', '.') %>% # drop last sentence
str_replace_all(' ,', ',') %>% # put back comma
str_replace_all(' \\.', '.') # put back dot
return(my_notes)
}
# the ui consists of a title, a side panel with two drop-downs and a main panel that displays text
ui <- fluidPage(
titlePanel("Wine tasting notes generator"),
sidebarLayout(
sidebarPanel(
helpText("Choose a variety:"),
selectInput("variety",
label = "Choose a variety",
choices = variety_choices,
selected = "Chardonnay"),
helpText("Is this wine good or bad:"),
selectInput("category",
label = "Is this wine good or bad?",
choices = c('good', 'bad'),
selected = "bad")
),
mainPanel(
strong(textOutput("choices")),
p(textOutput('notes')),
em('Credit: https://www.kaggle.com/zynicide/wine-reviews')
)
)
)
server <- function(input, output) {
output$choices <- renderText({
paste0(
"Your picked a ",
input$category,
' ',
input$variety,
'. Here are your notes:'
)
})
output$notes <- renderText({
writeNotes(input$category,input$variety)
})
}
shinyApp(ui, server)