---
title: "Mueller Report"
output: html_notebook
---

Lets download the Mueller report and do some text mining.

Here is a very nice blog post to follow to get started [Text Crunching and Data Munging](https://www.jlukito.com/blog/2019/4/20/using-r-to-analyze-the-redacted-mueller-report) by Josephine Lukito.


```{r}
library(pacman)
p_load(tidyverse, pdftools, ggpage, inspectdf)
```


Download the Mueller report.

The mode "wb" seems to be needed for Windows.  This might need to curl on Mac.

```{r}
# download.file("https://viewfromll2.files.wordpress.com/2019/04/mueller-report.pdf", "mueller-report.pdf", mode = "wb")

# Table of contents
toc <- pdf_toc("mueller-report.pdf")
toc

jsonlite::toJSON(toc, auto_unbox = TRUE, pretty = TRUE)

# Author, version, etc
info <- pdf_info("mueller-report.pdf")
info

# Table with fonts
fonts <- pdf_fonts("mueller-report.pdf")
fonts


report <- pdf_text("mueller-report.pdf")

# first page text
cat(report[1])

# second page text
cat(report[9])
```


```{r}
fileConn <- file("mueller-report.txt")
writeLines(report, fileConn)
close(fileConn)
```



```{r}
# renders pdf to bitmap array
bitmap <- pdf_render_page("mueller-report.pdf", page = 10)

# save bitmap image
png::writePNG(bitmap, "page.png")
jpeg::writeJPEG(bitmap, "page.jpeg")

```


```{r}

mueller_report_csv <- "https://raw.githubusercontent.com/gadenbuie/mueller-report/ab74012b0532ffa34f3a45196d2b28004e11b9c2/mueller_report.csv"

mueller_report <- read_csv(mueller_report_csv)

mueller_report


mueller_pages <- 
  mueller_report %>% 
  # pad pages with fewer lines than expected
  complete(
    page, 
    line = 1:max(mueller_report$line),
    fill = list(text = "")
  ) %>% 
  # Pre-process for {ggpage}
  ggpage_build(
    ncol = 30, 
    bycol = FALSE, 
    page.col = "page", 
    wtl = FALSE, 
    x_space_pages = 10,
    y_space_pages = 100
  ) %>% 
  mutate(
    color = case_when(
      str_detect(word, "trump|president") ~ "Trump",
      str_detect(word, "russia")     ~ "Russia",
      str_detect(word, "cohen")      ~ "Cohen",
      str_detect(word, "co(m|rn)ey") ~ "Comey",
      str_detect(word, "flynn")      ~ "Flynn",
      str_detect(word, "manafort")   ~ "Manafort",
      str_detect(word, "sessions")   ~ "Sessions",
      str_detect(word, "mcgahn")     ~ "McGahn",
      TRUE ~ "normal"
    ),
    color = factor(color, c(
      "Trump", "Russia", "Cohen", "Comey",
      "Flynn", "Manafort", "Sessions", "McGahn", "normal"
    ))
  )

mueller_pages

# manually assigned colors from ggthemes::pal_calc()
colors <- rep("", length(levels(mueller_pages$color)))
names(colors) <- levels(mueller_pages$color)
colors["Trump"]    <- "#FF4023"
colors["Russia"]   <- "#004983"
colors["Cohen"]    <- "#FF922E"
colors["Comey"]    <- "#559B30"
colors["Flynn"]    <- "#4D276D"
colors["Manafort"] <- "#7BCAFD"
colors["Sessions"] <- "#7F1327"
colors["McGahn"]   <- "#FFD040"
colors["normal"]   <- "#d0d0d0"


ggpage_plot(mueller_pages) +
  aes(fill = color) +
  scale_fill_manual(
    values = colors, 
    breaks = setdiff(names(colors), "normal")
  ) +
  labs(fill = NULL, caption = "@grrrck") +
  guides(fill = guide_legend(nrow = 1)) +
  theme(legend.position = "bottom")

```



Check out this nice github

https://github.com/batpigandme/tidymueller


Topic Modeling

https://www.christopheryee.org/blog/text-mining-the-redacted-mueller-report/







