An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

116 líneas
4.1KB

  1. fractions <- c("AFD" = "AfD",
  2. "AFD&FRAKTIONSLOS" = "AfD&Fraktionslos",
  3. "BÜNDNIS90/" = "BÜNDNIS 90 / DIE GRÜNEN",
  4. "BÜNDNIS90/DIEGRÜNEN" = "BÜNDNIS 90 / DIE GRÜNEN",
  5. "FRAKTIONSLOS" = "Fraktionslos",
  6. "DIELINKE" = "DIE LINKE",
  7. "SPD" = "SPD",
  8. "CDU/CSU" = "CDU/CSU",
  9. "FDP" = "FDP")
  10. repair_fraction <- function(fraction) {
  11. cleaned <- str_to_upper %$% str_replace_all(fraction, "\\s", "")
  12. fractions[cleaned]
  13. }
  14. # takes vector of titel and keeps longest
  15. longest_titel <- function(titel) {
  16. if (all(is.na(titel))) NA_character_
  17. else titel[which.max %$% str_length(titel)]
  18. }
  19. # takes character vector, removes duplicates and collapses
  20. collect_unique <- function(xs) xs %>% clear_na() %>% unique() %>% str_c(collapse="&") %>% na_if("")
  21. # expects a tibble of speaker and repairs
  22. repair_speaker <- function(speaker) {
  23. if (nrow(speaker) == 0) return(speaker)
  24. speaker %>%
  25. filter(id != "10000") %>% # invalid id's
  26. mutate(fraction = Vectorize(repair_fraction)(fraction)) %>% # fix fraction
  27. group_by(id) %>%
  28. summarize(vorname = head(vorname, 1),
  29. nachname = head(nachname, 1),
  30. fraction = collect_unique(fraction),
  31. titel = longest_titel(titel),
  32. rolle_kurz = collect_unique(str_squish(rolle_kurz)),
  33. rolle_lang = collect_unique(str_squish(rolle_lang))) %>%
  34. ungroup() #%>%
  35. # arrange(id) %>%
  36. # distinct(vorname, nachname, fraction, titel)
  37. }
  38. repair_speeches <- function(speeches) {
  39. if (nrow(speeches) == 0) return(speeches)
  40. # TODO: fill with content
  41. speeches
  42. }
  43. repair_talks <- function(talks) {
  44. if (nrow(talks) == 0) return(talks)
  45. # ignore all talks which have empty content
  46. filter(talks, str_length(content) > 0)
  47. }
  48. #' Lookup name in speakers table
  49. #'
  50. #' Tries to find the correct speaker id given a name.
  51. #' This is sufficient since every prename lastname combination in the bundestag is
  52. #' unique (luckily :D)
  53. #'
  54. #' @param tb tibble
  55. #' @param redner tibble
  56. #' @param name_variable name
  57. #'
  58. #' Tries to match the name_variable column with speaker names
  59. #'
  60. #' returns a lookup table
  61. lookup_speaker <- function(tb, speaker, name_variable) {
  62. tobereplaced <- "[-–—‑­­-­­­ ]"
  63. speaker %>%
  64. unite(name, vorname, nachname, sep=".*") %>%
  65. mutate(name = str_replace_all(name, tobereplaced, ".*")) ->
  66. rs
  67. find_match <- function(komm) {
  68. if (komm == "") return (NA_character_)
  69. # I tried with agrep (levensthein distance) but results are better that way
  70. matches <- str_which(komm, rs$name)
  71. if (length(matches) == 0) return(NA_character_)
  72. rs[head(matches, 1), ]$id
  73. }
  74. tb %>%
  75. distinct({{name_variable}}) %>%
  76. mutate(speaker = Vectorize(find_match)(str_replace_all({{name_variable}}, tobereplaced, "")))
  77. }
  78. repair_comments <- function(comments, redner) {
  79. cat(paste0("Looking up speaker id's for names in comments. This may take a while ...\n",
  80. "Use repair(, repair_commments = FALSE) to skip this.\n"))
  81. # try to find a redner id for each actual comment
  82. comments %>%
  83. filter(!is.na(kommentator)) %>%
  84. lookup_redner(redner, kommentator) %>%
  85. left_join(comments, ., by="kommentator") %>%
  86. select(-kommentator)
  87. }
  88. #' Repair parsed tables
  89. #'
  90. #' TODO: Explain repair_comments argument
  91. #' (if TRUE, we try to lookup redner names in redner table)
  92. #'
  93. #' Possible test: check identical(repair(res), repair(repair(res))) == TRUE
  94. #' Since repaired tables should be a fixpoint of repair.
  95. #' @export
  96. repair <- function(parse_output, repair_comments = FALSE) {
  97. list(redner = repair_speaker(parse_output$speaker),
  98. reden = repair_speeches(parse_output$speeches),
  99. talks = repair_talks(parse_output$talks),
  100. comments = if(repair_comments) repair_comments(parse_output$comments,
  101. parse_output$speaker)
  102. else parse_output$comments,
  103. applause = parse_output$applause
  104. )
  105. }