add masterdata from bundestag.de and use this for genderequality #16

Merged
christian merged 10 commits from genderequality-alternative into master 2021-08-11 08:30:30 +02:00
2 changed files with 109011 additions and 50 deletions
Showing only changes of commit f753920d34 - Show all commits
+108989
View File
File diff suppressed because it is too large Load Diff
+21 -49
View File
@@ -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,27 @@ 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) { x <- read_xml("../inst/masterdata.xml")
html %>% mdbs <- xml_find_all(x, "MDB")
html_node(sel) %>%
html_attr("href") ids <- c()
genders <- c()
for (mdb in mdbs) {
xml_get(mdb, "ID") -> mdb_id
xml_find_first(mdb, "BIOGRAFISCHE_ANGABEN") %>%
xml_get("GESCHLECHT") ->
mdb_gender
ids <- c(ids, mdb_id)
genders <- c(genders, if (mdb_gender == "männlich") "male" else "female")
} }
first_content_p_text <- function(url) { gender <- tibble(id = ids, gender = genders)
res <- NA speaker_with_gender <- left_join(res$speaker, gender)
i <- 1
while(is.na(res)) {
read_html(url) %>%
html_node(str_glue("#mw-content-text > div.mw-parser-output > p:nth-child({i})")) %>%
html_text() -> res
i <- i + 1
}
res
}
abgeordneten_list_html <- read_html(
"https://de.wikipedia.org/wiki/Liste_der_Mitglieder_des_Deutschen_Bundestages_(19._Wahlperiode)")
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 +152,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")) %>%