Merge pull request 'add masterdata from bundestag.de and use this for genderequality' (#16) from genderequality-alternative into master

This commit was merged in pull request #16.
This commit is contained in:
2021-08-11 08:30:27 +02:00
2 changed files with 109017 additions and 50 deletions
+108989
View File
File diff suppressed because it is too large Load Diff
+27 -49
View File
@@ -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,8 +33,7 @@ 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 load the tables from csv files.
```{r} ```{r}
@@ -48,53 +47,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 +158,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")) %>%