Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoughtfulbloke/ebfb1865acbc1bb5c7c482d6ebfef2b8 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/ebfb1865acbc1bb5c7c482d6ebfef2b8 to your computer and use it in GitHub Desktop.
converts wordle share to an image and autogenerates accompanying alt text
library(ggplot2)
library(ggthemes)
library(dplyr)
colour_choice <- colorblind_pal()(3)
pasted_text = "PasteYourGameShareHre"
pasted_title <- gsub("\n\n.*", "", pasted_text)
title_short <- substr(pasted_title, 1, nchar(pasted_title) - 4)
pasted_grid <- gsub(".*[^/ ]/[^\\\n]+", "", pasted_text)
decomposed_grid <- unlist(strsplit(pasted_grid, split = ""))
squares_only <- decomposed_grid[decomposed_grid != "\n"]
grid_row <- ceiling(1:length(squares_only) / 5)
grid_col <- (1:length(squares_only)) - (grid_row - 1) * 5
grid_result <- rep(colour_choice[1], length(squares_only))
grid_result[squares_only == "\U0001f7e9" | squares_only == "\U0001f7e7"] <- colour_choice[2]
grid_result[squares_only == "\U0001f7e8" | squares_only == "\U0001f7e6"] <- colour_choice[3]
grid_df <- data.frame(grow = grid_row * -1,
gcol = grid_col,
gresult = grid_result)
wimg <- ggplot(grid_df, aes(x = gcol, y = grow, fill = gresult)) +
geom_tile(width = 0.9, height = 0.9) +
scale_fill_identity() + coord_equal() + theme_void() +
labs(title = title_short, subtitle = "blue = misplaced, orange = correct") +
theme(
plot.title = element_text(hjust = 0.5, size = 30),
plot.subtitle = element_text(hjust = 0.5, size = 20),
plot.margin = margin(60, 60, 60, 60, "pt")
)
savepath <-
paste0(
"~/Desktop/",
gsub(" ", "_", title_short),
strftime(Sys.time(), format = "_%Y_%m_%d.png")
)
ggsave(
filename = savepath,
plot = wimg,
dpi = 72,
units = "in",
bg = "white",
height = 5.556,
width = 9.877
)
autotext <- grid_df %>%
mutate(
grow = grow * -1,
gtext = case_when(
gresult == colour_choice[2] ~ "correct",
gresult == colour_choice[3] ~ "misplaced",
TRUE ~ "wrong"
)
) %>%
arrange(grow, gcol) %>%
group_by(grow) %>%
summarise(tresult = paste(gtext, collapse = ", ")) %>%
ungroup() %>%
mutate(outtext = paste0("Line ", grow, " guesses were ", tresult, "."))
alttest <-
c(paste0("Results grid for ", title_short, "."), autotext$outtext)
writeLines(alttest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment