Skip to content

Instantly share code, notes, and snippets.

@dbrby
Last active September 29, 2021 14:51
Show Gist options
  • Save dbrby/967287a860155e1604a352de9b9c8770 to your computer and use it in GitHub Desktop.
Save dbrby/967287a860155e1604a352de9b9c8770 to your computer and use it in GitHub Desktop.
Scaling Labour 21 Conference Speeches
require(quanteda)
require(readtext)
require(tidyverse)
require(quanteda.textmodels)
require(quanteda.textplots)
path_data <- system.file("/Users/danbraby/labour2021/", package = "readtext")
# first save txt files from Github
dat <- readtext("/Users/danbraby/labour2021/txt/*",
docvarsfrom = "filenames",
docvarnames = c("name", "date"),
dvsep = "_",
encoding = "latin1")
# Read in flat-file from Github repo
# dat <- read.csv("https://raw.githubusercontent.com/dbrby/lab21_conference/main/lab21_speeches.csv")
dat_corp <- corpus(dat, docid_field = "name", text_field = "text")
dat_toks <- tokens(dat_corp, remove_punct = T,
remove_symbols = T, remove_numbers = T,
remove_url = T, remove_separators = T) %>%
tokens_remove(stopwords("en")) %>%
tokens_select(min_nchar = 2L)
dat_dfm <- dfm(dat_toks) %>% dfm_trim(min_docfreq = 2)
tmod_wf <- textmodel_wordfish(dat_dfm, dir = c(4, 5))
textplot_scale1d(tmod_wf)
textplot_scale1d(tmod_wf, margin = "features",
highlighted = c("fuel", "hgv",
"aid", "rights",
"women", "scotland",
"keir", "tories",
"boris"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment