Переглянути джерело

combine duplicate redner by collapsing all data

package
flavis 4 роки тому
джерело
коміт
a7e9ba9655
2 змінених файлів з 19 додано та 4 видалено
  1. +17
    -4
      scraping/repair.R
  2. +2
    -0
      utils/helpers.R

+ 17
- 4
scraping/repair.R Переглянути файл

@@ -8,16 +8,28 @@ fraktionen <- c("AFD" = "AfD",
"CDU/CSU" = "CDU/CSU",
"FDP" = "FDP")


# expects a tibble of redner and repairs
repair_fraktion <- function(fraktion) {
cleaned <- str_to_upper %$% str_replace_all(fraktion, "\\s", "")
fraktionen[cleaned]
}

# takes vector of titel and keeps longest
longest_titel <- function(titel) {
if (all(is.na(titel))) NA_character_
else titel[which.max %$% str_length(titel)]
}

# takes character vector, removes duplicates and collapses
collect_unique <- function(xs) xs %>% clear_na() %>% unique() %>% str_c(collapse="&") %>% na_if("")

# expects a tibble of redner and repairs
repair_redner <- function(redner) {
# fix fraktionsnames
redner %>% mutate(fraktion = Vectorize(repair_fraktion)(fraktion))
redner %>% mutate(fraktion = Vectorize(repair_fraktion)(fraktion)) %>% # fix fraktion
group_by(id, vorname, nachname) %>%
summarize(fraktion = collect_unique(fraktion),
titel = longest_titel(titel),
rolle_kurz = collect_unique(str_squish(rolle_kurz)),
rolle_lang = collect_unique(str_squish(rolle_lang)))
}

repair_reden <- function(reden) {
@@ -36,3 +48,4 @@ repair <- function(parse_output) {
reden = repair_reden(parse_output$reden),
talks = repair_talks(parse_output$talks))
}


+ 2
- 0
utils/helpers.R Переглянути файл

@@ -1,2 +1,4 @@
`%$%` <- function(f, x) f(x)
`%.%` <- function(f, g) function(...) f(g(...))

clear_na <- function(xs) xs[!is.na(xs)]

Завантаження…
Відмінити
Зберегти