Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7166d1578 | ||
|
|
896ba1b3b0 | ||
|
|
ecb5677703 | ||
|
|
9d456bfa60 | ||
|
|
62fe9d497d | ||
|
|
e31ccabf18 | ||
|
|
622fd4db07 | ||
|
|
605e5e976a | ||
|
|
489254dadf | ||
|
|
b09742815a | ||
|
|
7daf9e553c | ||
|
|
d657ca3fbe | ||
|
|
c53d842a1e | ||
|
|
f753920d34 |
@@ -7,6 +7,7 @@ export(join_speaker)
|
|||||||
export(party_colors)
|
export(party_colors)
|
||||||
export(read_all)
|
export(read_all)
|
||||||
export(read_from_csv)
|
export(read_from_csv)
|
||||||
|
export(read_from_csv_or_fetch)
|
||||||
export(repair)
|
export(repair)
|
||||||
export(word_usage_by_date)
|
export(word_usage_by_date)
|
||||||
export(write_to_csv)
|
export(write_to_csv)
|
||||||
|
|||||||
@@ -271,3 +271,22 @@ read_from_csv <- function(path="inst/csv/") {
|
|||||||
is_valid_res(res)
|
is_valid_res(res)
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#' @param path directory of csv files to read
|
||||||
|
#' read data from csv files if they exist already
|
||||||
|
#' otherwise fetch protocols and then write the data into csv files
|
||||||
|
#'
|
||||||
|
#' @export
|
||||||
|
read_from_csv_or_fetch <- function(path="inst/") {
|
||||||
|
path <- make_directory_path(path)
|
||||||
|
res <- tryCatch(read_from_csv(str_c(path, "csv/")),
|
||||||
|
error = function(c) NULL)
|
||||||
|
if (!is.null(res)) return(res)
|
||||||
|
|
||||||
|
fetch_all(str_c(path, "records/"), create=T)
|
||||||
|
read_all(str_c(path, "records/")) %>%
|
||||||
|
repair() ->
|
||||||
|
res
|
||||||
|
write_to_csv(res, str_c(path, "csv/"), create=T)
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,26 +9,7 @@ Using the `remotes` package, this is easily installed via:
|
|||||||
```r
|
```r
|
||||||
remotes::install_url("https://git.flavigny.de/christian/hateimparlament/archive/master.zip")
|
remotes::install_url("https://git.flavigny.de/christian/hateimparlament/archive/master.zip")
|
||||||
```
|
```
|
||||||
Since the fetching and reading is very slow and depends on an internet connection, all vignettes
|
If you want to build the vignettes, pass `build_vignettes = TRUE`.
|
||||||
use `read_from_csv` to read already parsed tibbles from `.csv` files.
|
|
||||||
|
|
||||||
That's why, if you want to build the vignettes yourself, you need to
|
|
||||||
download the source code, e.g. on Linux
|
|
||||||
```
|
|
||||||
git clone https://git.flavigny.de/christian/hateimparlament
|
|
||||||
cd hateimparlament
|
|
||||||
```
|
|
||||||
then start `R` and do
|
|
||||||
```r
|
|
||||||
devtools::load_all()
|
|
||||||
fetch_all(create = TRUE)
|
|
||||||
read_all() %>% repair() -> res
|
|
||||||
write_to_csv(res, create = TRUE)
|
|
||||||
```
|
|
||||||
Then finally, do:
|
|
||||||
```r
|
|
||||||
devtools::install(build_vignettes = TRUE)
|
|
||||||
```
|
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
|
|
||||||
|
|||||||
+108989
File diff suppressed because it is too large
Load Diff
@@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res
|
|||||||
```
|
```
|
||||||
We also used `repair` to fix a bunch of formatting issues in the records.
|
We also used `repair` to fix a bunch of formatting issues in the records.
|
||||||
|
|
||||||
For development purposes, we load the tables from csv files.
|
For development purposes, we only fetch records if they are not already
|
||||||
|
stored as csv files:
|
||||||
```{r}
|
```{r}
|
||||||
res <- read_from_csv('../inst/csv/')
|
res <- read_from_csv_or_fetch('../inst/')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Analysis
|
## Analysis
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
---
|
---
|
||||||
title: "genderequality"
|
title: "Differences in gender"
|
||||||
output: rmarkdown::html_vignette
|
output: rmarkdown::html_vignette
|
||||||
vignette: >
|
vignette: >
|
||||||
%\VignetteIndexEntry{genderequality}
|
%\VignetteIndexEntry{Differences in gender}
|
||||||
%\VignetteEngine{knitr::rmarkdown}
|
%\VignetteEngine{knitr::rmarkdown}
|
||||||
%\VignetteEncoding{UTF-8}
|
%\VignetteEncoding{UTF-8}
|
||||||
---
|
---
|
||||||
@@ -20,7 +20,7 @@ library(dplyr)
|
|||||||
library(ggplot2)
|
library(ggplot2)
|
||||||
library(stringr)
|
library(stringr)
|
||||||
library(tidyr)
|
library(tidyr)
|
||||||
library(rvest)
|
library(xml2)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Preparation of data
|
## Preparation of data
|
||||||
@@ -33,13 +33,15 @@ Second, those `.xml` files, need to be parsed into `R` `tibbles`. This is accomp
|
|||||||
```r
|
```r
|
||||||
read_all("../records/") %>% repair() -> res
|
read_all("../records/") %>% repair() -> res
|
||||||
```
|
```
|
||||||
We also used `repair` to fix a bunch of formatting issues in the records and unpacked
|
We also used `repair` to fix a bunch of formatting issues in the records.
|
||||||
the result into more descriptive variables.
|
|
||||||
|
|
||||||
For development purposes, we load the tables from csv files.
|
For development purposes, we only fetch records if they are not already
|
||||||
|
stored as csv files:
|
||||||
```{r}
|
```{r}
|
||||||
res <- read_from_csv('../inst/csv/')
|
res <- read_from_csv_or_fetch('../inst/')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
and unpack our tibbles
|
and unpack our tibbles
|
||||||
```{r}
|
```{r}
|
||||||
comments <- res$comments
|
comments <- res$comments
|
||||||
@@ -48,53 +50,33 @@ speaker <- res$speaker
|
|||||||
talks <- res$talks
|
talks <- res$talks
|
||||||
```
|
```
|
||||||
|
|
||||||
Bevor we can do our analysis, we have to assign a gender to our politicans.
|
Bevor we can do our analysis, we have to assign a gender to our politicans. We do this
|
||||||
|
by reading the gender from the master data of all members of parliament, which is
|
||||||
|
fetched from bundestag.de.
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
extract_href <- function(sel, html) {
|
xml_get <- function(node, name) {
|
||||||
html %>%
|
res <- xml_text(xml_find_all(node, name))
|
||||||
html_node(sel) %>%
|
if (length(res) == 0) NA_character_
|
||||||
html_attr("href")
|
else res
|
||||||
}
|
}
|
||||||
|
|
||||||
first_content_p_text <- function(url) {
|
x <- read_xml("../inst/masterdata.xml")
|
||||||
res <- NA
|
mdbs <- xml_find_all(x, "MDB")
|
||||||
i <- 1
|
|
||||||
while(is.na(res)) {
|
ids <- c()
|
||||||
read_html(url) %>%
|
genders <- c()
|
||||||
html_node(str_glue("#mw-content-text > div.mw-parser-output > p:nth-child({i})")) %>%
|
for (mdb in mdbs) {
|
||||||
html_text() -> res
|
xml_get(mdb, "ID") -> mdb_id
|
||||||
i <- i + 1
|
xml_find_first(mdb, "BIOGRAFISCHE_ANGABEN") %>%
|
||||||
}
|
xml_get("GESCHLECHT") ->
|
||||||
res
|
mdb_gender
|
||||||
|
ids <- c(ids, mdb_id)
|
||||||
|
genders <- c(genders, if (mdb_gender == "männlich") "male" else "female")
|
||||||
}
|
}
|
||||||
|
|
||||||
abgeordneten_list_html <- read_html(
|
gender <- tibble(id = ids, gender = genders)
|
||||||
"https://de.wikipedia.org/wiki/Liste_der_Mitglieder_des_Deutschen_Bundestages_(19._Wahlperiode)")
|
speaker_with_gender <- left_join(res$speaker, gender)
|
||||||
|
|
||||||
selectors <- str_glue("#mw-content-text > div.mw-parser-output > table:nth-child(20) > tbody > tr:nth-child({2:709}) > td:nth-child(2) > a")
|
|
||||||
link_part2 <- sapply(selectors, extract_href, abgeordneten_list_html)
|
|
||||||
link <- str_c("https://de.wikipedia.org", link_part2)
|
|
||||||
|
|
||||||
text <- sapply(link, first_content_p_text)
|
|
||||||
text %>%
|
|
||||||
str_extract(" ist ein.") %>%
|
|
||||||
str_replace(" ist eine", "female") %>%
|
|
||||||
str_replace(" ist ein ", "male") ->
|
|
||||||
gender
|
|
||||||
|
|
||||||
text %>%
|
|
||||||
str_extract("^([:upper:]?[:lower:]+[\\s\\-]?)*") %>%
|
|
||||||
str_trim() ->
|
|
||||||
names
|
|
||||||
|
|
||||||
gender <- tibble(speaker = names,
|
|
||||||
gender = gender)
|
|
||||||
|
|
||||||
speaker %>%
|
|
||||||
unite("speaker", vorname, nachname, sep = " ") %>%
|
|
||||||
right_join(gender, by = "speaker") ->
|
|
||||||
speaker_with_gender
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Analyse
|
## Analyse
|
||||||
@@ -179,9 +161,8 @@ speeches %>%
|
|||||||
summarize(n = n()) %>%
|
summarize(n = n()) %>%
|
||||||
ungroup() %>%
|
ungroup() %>%
|
||||||
arrange(-n) %>%
|
arrange(-n) %>%
|
||||||
left_join(speaker, by=c("speaker" = "id")) %>%
|
join_speaker(res) %>%
|
||||||
unite(name, vorname, nachname, sep = " ") %>%
|
left_join(gender, by=c("speaker"="id")) %>%
|
||||||
inner_join(gender, by=c("name"= "speaker")) %>%
|
|
||||||
group_by(gender) %>%
|
group_by(gender) %>%
|
||||||
summarise(absolute=sum(n)) %>%
|
summarise(absolute=sum(n)) %>%
|
||||||
filter(gender %in% c("female", "male")) %>%
|
filter(gender %in% c("female", "male")) %>%
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res
|
|||||||
```
|
```
|
||||||
We also used `repair` to fix a bunch of formatting issues in the records.
|
We also used `repair` to fix a bunch of formatting issues in the records.
|
||||||
|
|
||||||
For development purposes, we load the tables from csv files.
|
For development purposes, we only fetch records if they are not already
|
||||||
|
stored as csv files:
|
||||||
```{r}
|
```{r}
|
||||||
res <- read_from_csv('../inst/csv/')
|
res <- read_from_csv_or_fetch('../inst/')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Analysis
|
## Analysis
|
||||||
|
|||||||
@@ -38,9 +38,10 @@ talks <- res$talks
|
|||||||
We also used `repair` to fix a bunch of formatting issues in the records and unpacked
|
We also used `repair` to fix a bunch of formatting issues in the records and unpacked
|
||||||
the result into more descriptive variables.
|
the result into more descriptive variables.
|
||||||
|
|
||||||
For development purposes, we load the tables from csv files.
|
For development purposes, we only fetch records if they are not already
|
||||||
|
stored as csv files:
|
||||||
```{r}
|
```{r}
|
||||||
tables <- read_from_csv('../inst/csv/')
|
tables <- read_from_csv_or_fetch('../inst/')
|
||||||
|
|
||||||
comments <- tables$comments
|
comments <- tables$comments
|
||||||
speeches <- tables$speeches
|
speeches <- tables$speeches
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ read_all("../inst/records/") %>% repair() -> res
|
|||||||
```
|
```
|
||||||
We also used `repair` to fix a bunch of formatting issues in the records.
|
We also used `repair` to fix a bunch of formatting issues in the records.
|
||||||
|
|
||||||
For development purposes, we load the tables from csv files.
|
For development purposes, we only fetch records if they are not already
|
||||||
|
stored as csv files:
|
||||||
```{r}
|
```{r}
|
||||||
res <- read_from_csv('../inst/csv/')
|
res <- read_from_csv_or_fetch('../inst/')
|
||||||
```
|
```
|
||||||
|
|
||||||
## Analysis
|
## Analysis
|
||||||
|
|||||||
Reference in New Issue
Block a user