Marc K | aa2ebf6 | 2023-05-26 13:16:01 +0200 | [diff] [blame^] | 1 | library(RKorAPClient) |
| 2 | library(httr) |
| 3 | library(httpuv) |
| 4 | library(tidyverse) |
| 5 | library(scales) |
| 6 | library(idsThemeR) |
| 7 | |
| 8 | icc_base_url = "https://korap.ids-mannheim.de/instance/icc"; |
| 9 | |
| 10 | connections = list() |
| 11 | |
| 12 | icc <- tibble( |
| 13 | lang = c("eng", "ger", "nor"), |
| 14 | app_id = c( |
| 15 | "mTTTnJ6f6hGrPh6dRhbJhJ", |
| 16 | "TMLPTJfP7rHb93bpFp39mL", |
| 17 | "TMFtPJnbb7f4MRmd76Rb34" |
| 18 | ) |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | icc_token <- function(lang, app_id, url = paste0(icc_base_url, '/', lang)) { |
| 23 | token_key = paste0("KORAP_ICC_TOKEN_", lang) |
| 24 | token = Sys.getenv(token_key) |
| 25 | if (token != "") |
| 26 | return(token) |
| 27 | |
| 28 | korap_app <- oauth_app("icc-iclc10-contribution", key = app_id, secret = NULL) |
| 29 | |
| 30 | korap_endpoint <- oauth_endpoint(NULL, |
| 31 | "settings/oauth/authorize", |
| 32 | "api/v1.0/oauth2/token", |
| 33 | base_url = url) |
| 34 | token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = FALSE) |
| 35 | token = token_bundle[["credentials"]][["access_token"]] |
| 36 | do.call(Sys.setenv, as.list(setNames(token, token_key))) |
| 37 | return(token) |
| 38 | } |
| 39 | |
| 40 | icc_con <- function(lang, token = Sys.getenv(paste0("KORAP_ICC_TOKEN_", lang))) { |
| 41 | if ((! lang %in% names(connections)) || is_empty(connections[[lang]])) { |
| 42 | url <- paste0(icc_base_url, '/', lang) |
| 43 | connections[[lang]] <<- new("KorAPConnection", KorAPUrl = url, accessToken = token, cache = F) |
| 44 | } |
| 45 | return(connections[[lang]]) |
| 46 | } |
| 47 | |
| 48 | icc <- icc %>% |
| 49 | rowwise() %>% |
| 50 | mutate(token = icc_token(lang, app_id)) %>% |
| 51 | add_column(tokens=0) |
| 52 | |
| 53 | |
| 54 | genre <- c("Blog", |
| 55 | "Creative:Novels_ShortStories", |
| 56 | "Informational:Learned:Humanities", |
| 57 | "Informational:Learned:NaturalSciences", |
| 58 | "Informational:Learned:SocialSciences", |
| 59 | "Informational:Learned:Technology", |
| 60 | "Informational:Popular:Humanities", |
| 61 | "Informational:Popular:NaturalSciences", |
| 62 | "Informational:Popular:SocialSciences", |
| 63 | "Informational:Popular:Technology", |
| 64 | "Informational:Reportage", |
| 65 | "Instructional:AdministrativeRegulatoryProse", |
| 66 | "Instructional:Skills_Hobbies", |
| 67 | "Persuasive" |
| 68 | ) |
| 69 | |
| 70 | icc_genre <- icc %>% |
| 71 | expand_grid(genre) %>% |
| 72 | mutate(vc = paste0("iccGenre=", genre)) %>% |
| 73 | rowwise() %>% |
| 74 | mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens) |
| 75 | |
| 76 | icc_genre %>% ggplot(aes(x=lang, fill=genre, y=tokens)) + |
| 77 | geom_col() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) + |
| 78 | theme_ids() + |
| 79 | geom_text(aes(label=if_else(tokens > 0, as.character(tokens), ""), y=tokens), position= position_stack(reverse = F, vjust = 0.5), color="white", size=3.2, family="Fira Sans Condensed") |
| 80 | |
| 81 | year <- c(1988:2022) |
| 82 | |
| 83 | icc_year <- icc %>% |
| 84 | expand_grid(year) %>% |
| 85 | mutate(vc = paste0("pubDate in ", year)) %>% |
| 86 | rowwise() %>% |
| 87 | mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens) |
| 88 | |
| 89 | icc_year %>% ggplot(aes(x=year, fill=lang, color=lang, y=tokens)) + |
| 90 | geom_line() + geom_point() + geom_area() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) + |
| 91 | theme_ids() |