blob: d7759641041ff2aea2d791b6780b9e3bf77af956 [file] [log] [blame]
Marc Kaa2ebf62023-05-26 13:16:01 +02001library(RKorAPClient)
2library(httr)
3library(httpuv)
4library(tidyverse)
5library(scales)
6library(idsThemeR)
7
8icc_base_url = "https://korap.ids-mannheim.de/instance/icc";
9
10connections = list()
11
12icc <- tibble(
13 lang = c("eng", "ger", "nor"),
14 app_id = c(
15 "mTTTnJ6f6hGrPh6dRhbJhJ",
16 "TMLPTJfP7rHb93bpFp39mL",
17 "TMFtPJnbb7f4MRmd76Rb34"
18 )
19)
20
21
22icc_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
40icc_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
48icc <- icc %>%
49 rowwise() %>%
50 mutate(token = icc_token(lang, app_id)) %>%
51 add_column(tokens=0)
52
53
54genre <- 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
70icc_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
76icc_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
81year <- c(1988:2022)
82
83icc_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
89icc_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()