Thesis Word Lottery Preview

A time-honored Reed tradition! To the best of my knowledge, no group was running the lottery this year, so the DataLab took over. One issue is, we were running off of my ancient memory of the lottery, which was simply: submit word, get word, use word. After talking to another alum, my memory was jogged that the word was supposed to be one you used prominently in your thesis. This omission led to some issues as you will see.

The word lottery submission guidelines are as follows:

By Tuesday, November 18th, there were submissions from 74 students. As of this posting on Thursday, we’re up to 77 and there’s still time for submission, so the response has been great!

Words were submitted in somewhat different formats, so a little clean up was required. Then we graphed the length of words and used an R package and a dictionary API to analyze things a little bit. Here’s how we did things.

Connecting R to outside websites: Google sheets and Mirriam-Webster

Pulling in data automatically instead of having to manually download it is always great. For this project, we used two packages to help us with that. First, the words were submitted via a Google form and populated a Google sheet. You can connect R to Google sheets with the package {googlesheets4}. This will use the function read_sheet() which takes the sheet URL as an argument. It will ask for authentication to hook up to your Google account and then you’re good to go. The only thing to note is that when your Google sheet updates, you will need to rerun the line that loads the data in order to stay up to date.

# pull data from google sheet
raw_lottery <- read_sheet("https://docs.google.com/spreadsheets/d/18fq2a0W2yWcQb13OZfXjBCrjEVMW1BPPFQicDgsHeDQ")

Then, in order to more easily look up information about the words, we used the package {httr2} for working with https requests via APIs. API stands for “application programming interface” and is just a helper program for accessing many sites that host open data that they allow the public to use. It’s a bridge between programs that helps the source handle the volume of web requests and helps the user pull data more easily. For accessing Mirriam-Webster’s API, you need an API key. This is a unique identifier, essentially a username and password in one, that MW can use to keep track of what you do and hunt you down if trying to do anything nefarious. You can request API keys from their website and they’ll email you the code. Then you can either store the key in your script as a string or in your user environment so that it is available for all scripts to use. We recommend adding to your user environment in case you need access in the future.

To make changes to your user environment you’ll use the following command to open up your .Renviron file.

usethis::edit_r_environ()

There you can add any keys by just putting something like MW_key="[the key]". Then in the script, when you access the API with the function request() from {httr2}, you’ll use your MW_key variable.

Cleaning up the data

When the words were submitted, some people submitted multiple words at one time and used different separators (, and 😉 between them. To make every word appear on its own line we used the function separate_longer_delim() from the {tidyr} package via {tidyverse}. Then we cleaned things up a little with str_squish() to remove white space, counted letters in words with str_length() (both from {tidytext}), and added a flag column for certain words we weren’t sure we’d allow.

# separate rows for each word, count letters, add flags
lottery <- raw_lottery |>
  separate_longer_delim(4, delim = regex("[,;]")) |>
  mutate(across(4, str_squish)) |>
  mutate(flag = replace_na(flag, "N")) |>
  mutate(n_letters = str_length(word)) 

Jerk-words

Rule 4 states “Words must be appropriate for a thesis. (i.e., no swear words; don’t be a jerk)” but we felt some people did not adhere to this rule. Some were too slangy, some were just not real words under anyone’s definition, and some were so difficult their only purpose was spite (see the graph to guess one of those). After talking with some students, they mentioned that they heard the old rules of the word lottery were that you were supposed to submit a word associated with your own thesis. Upon consulting another Olde Reedie, this was indeed the case, and, like it does with most information, my brain had failed to retain it. Oops; that probably would have partially solved the jerk-word issue.

Since I forgot this bit, we instead decided that all words would undergo evaluation by members of the DataLab and be given a consensus jerk-score of 0-2. Then students will receive a word of the same jerk-score that they submitted. So the flags were added and the DataLabbies are in the process of evaluation!

lottery <- lottery |>
  mutate(flag = if_else(word %in% add_flag, "Y", flag))

Finding date of word origins

Now we’ll use the Mirriam-Webster API to find the dates that the words were first used rather than having to look each one up by hand. To do this we wrote a function, but first we needed to know how the information was stored so we knew what to pull. You can generally find this on the same site you requested the API key from. Mirriam-Webster shows what information is available in a JSON file. It looks like this (image is cutoff because there’s a lot of available info, go to the site to see all):

Basically, everything that would be in the dictionary entry is spelled out here line-by-line. In this image you can see the other forms of the word “voluminous” like voluminously and you can see it is classified as an adjective.

After scrolling through that example on their site, we found that we needed to pull the variable labeled ‘date’. Here’s the function and we’ll go through each line.

# function to pull from Miriam Webster API
# key is requested and then stored as "MW_KEY"
get_mw_date <- function(word, key = Sys.getenv("MW_KEY")) {
  resp <- request("https://www.dictionaryapi.com/api/v3/references/collegiate/json") |>
    req_url_path_append(URLencode(word)) |>
    req_url_query(key = key) |>
    req_perform() |>
    resp_body_json()
  Sys.sleep(0.2)
  # not-found words come back as a list of suggestion strings, so keep only real entries
  hit <- keep(resp, \(x) is.list(x) && !is.null(x$date) &&
                str_to_lower(str_remove(x$meta$id, ":.*")) == str_to_lower(word))
  if (length(hit) == 0) return(NA_character_)
  hit[[1]]$date |> str_remove_all("\\{[^}]*\\}") |> str_squish()
}

The first line defines the name of the function as get_mw_date and shows that it needs the input variables word and the API key. Then we add the website we want the response to come from with the request() function. That is piped to a series of {httr2} functions that add the specific word to the request, state the API key, perform the actual request, and parse the JSON file. Then the Sys.sleep(0.2) is added so that we pause for 0.2 seconds between requests. This probably isn’t necessary for the number of words we are doing, but if you have a ton of requests, the pause between them will help ensure you don’t hit a rate limit the site has set to limit request overload.

Then keep() will keep only the arguments that we want and we want to keep things that pass three checks: they are a list (meaning the JSON parsing was successful), they have a date, and they are the word we want only, so “voluminous” not “voluminously” (that’s what the meta$id part refers to). If it passes these checks, it’s stored as a hit, if not we return an NA. Then the last line gets just the date and removes any tags that the JSON file has, example: circa 12th century{ds||1||} becomes just circa 12th century.

Now we’ll actually call the function when we go through a pipeline to add and clean the date.

# use the function to pull the dates and add column to make numeric
word_dates <- lottery |>
  distinct(word) |>
  mutate(first_use = map_chr(word, possibly(get_mw_date, NA_character_))) |>
  mutate(
    first_use_year = if_else(
      str_detect(first_use, "century"),
      (parse_number(first_use) - 1) * 100 + 50,  # "14th century" -> 1350
      parse_number(first_use)                     # "circa 1450" -> 1450
    ),
    specific = if_else(str_detect(first_use, "\\d{4}") %in% TRUE, "Y", "N")
  )

This will take distinct words (we do have one word submitted twice!) and run the function to set the date equal to first_use. The possibly() function is just a nice helper that makes things keep going if there’s an error. So even if one word throws an error, it’ll do the rest of the words before stopping. The second mutate just helps to clean up the dates a little more and change any dates that contain strings to just numbers.

Graphing the data

To graph the number of letters in each word, we could use a simple histogram, but to make the figure a little more interesting (and a little more New York Times-y), we’ll use geom_tile() for a tiled approach where each word will appear in the histogram as a little cube. We’ll color the flagged jerk-words in black and other words will be gray.

We first put the words in order by number of letters and then pipe to the ggplot. The tiling command just needs a length and height to set the cube size. Everything from there is just me fiddling with the plot to get it to look the way I want to. The best way to understand what each line does is just to change or remove, remove it, and see how the plot is affected.

(I am very particular about aesthetics and will waste as much time as the rest of the coding combined on just fiddling with the graph.)

# graph the word length with tiles
p_length <- lottery |>
  distinct(word, n_letters, flag) |>
  arrange(desc(flag == "Y")) |>
  group_by(n_letters) |>
  mutate(stack = row_number()) |>
  ungroup() |>
  ggplot(aes(x = n_letters, y = stack, fill = flag)) +
  geom_tile(width = 0.8, height = 0.8) +
  scale_fill_manual(values = c(N = "grey70", Y = "#000000"), guide = "none") +
  scale_x_continuous(breaks = 3:28, limits = c(2.5, 28.5),
                     expand = expansion(add = c(0.3, 0))) +
  scale_y_continuous(breaks = \(lim) {
    b <- scales::breaks_extended()(lim)
    b[b == fround(b)]
  }) +
  labs(
    title = "Length of submitted words",
    subtitle = "Each square is one word. Jerk-words in black.",
    x = "Letters in word", y = "Number of words"
  ) +
  theme_minimal(base_family = "serif", base_size = 20) +
  theme(
    text = element_text(color = "black"),
    axis.text = element_text(color = "black", size = 14),
    axis.title = element_text(color = "black", size = 20),
    panel.grid = element_blank(),
    axis.line = element_line(color = "black"),
    axis.ticks.y = element_line(color = "black"),
    axis.text.x = element_text(size = 12),
    plot.title = element_text(face = "bold", size = 26, hjust = 0.5),
    plot.subtitle = element_text(size = 18, hjust = 0.5),
    plot.title.position = "plot"
  )

For the date of first use of the words, we went with just a simpler histogram. So the code there is a little bit simpler.

# graph the word dates
p_years <- word_dates |>
  filter(!is.na(first_use_year)) |>
  ggplot(aes(x = first_use_year)) +
  geom_histogram(binwidth = 50, boundary = 0, fill = "grey70", color = "white") +
  scale_x_continuous(breaks = scales::breaks_width(100)) +
  scale_y_continuous(breaks = scales::breaks_width(2),
                     expand = expansion(mult = c(0, 0.05))) +
  labs(
    title = "When Miriam-Webster says the words originated",
    x = "Year of first known use", y = "Number of words"
  ) +
  theme_minimal(base_family = "serif", base_size = 20) +
  theme(
    text = element_text(color = "black"),
    axis.text = element_text(color = "black", size = 18),
    axis.title = element_text(color = "black", size = 20),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.line.x = element_line(color = "black"),
    plot.title = element_text(face = "bold", size = 26, hjust = 0.5),
    plot.subtitle = element_text(size = 18, hjust = 0.5),
    plot.title.position = "plot"
  )
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *