blob: a1d83748830afb4d226499ffbab6665513718214 [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() %>%
Marc Kupietz1f5969c2023-05-27 11:36:08 +020050 mutate(token = icc_token(lang, app_id))
Marc Kaa2ebf62023-05-26 13:16:01 +020051
52genre <- c("Blog",
53 "Creative:Novels_ShortStories",
54 "Informational:Learned:Humanities",
55 "Informational:Learned:NaturalSciences",
56 "Informational:Learned:SocialSciences",
57 "Informational:Learned:Technology",
58 "Informational:Popular:Humanities",
59 "Informational:Popular:NaturalSciences",
60 "Informational:Popular:SocialSciences",
61 "Informational:Popular:Technology",
62 "Informational:Reportage",
63 "Instructional:AdministrativeRegulatoryProse",
64 "Instructional:Skills_Hobbies",
65 "Persuasive"
66)
67
68icc_genre <- icc %>%
69 expand_grid(genre) %>%
70 mutate(vc = paste0("iccGenre=", genre)) %>%
71 rowwise() %>%
72 mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
73
Marc Kupietz1f5969c2023-05-27 11:36:08 +020074plot <- icc_genre %>% ggplot(aes(x=lang, fill=genre, y=tokens)) +
Marc Kaa2ebf62023-05-26 13:16:01 +020075 geom_col() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
76 theme_ids() +
77 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")
78
Marc Kupietz1f5969c2023-05-27 11:36:08 +020079ggsave("target/tokens_per_genre.png", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
80ggsave("target/tokens_per_genre.svg", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
81ggsave("target/tokens_per_genre.pdf", device = cairo_pdf, width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
82
83if(rstudioapi::isAvailable()) {
84 print(plot)
85}
86
Marc Kaa2ebf62023-05-26 13:16:01 +020087year <- c(1988:2022)
88
89icc_year <- icc %>%
90 expand_grid(year) %>%
91 mutate(vc = paste0("pubDate in ", year)) %>%
92 rowwise() %>%
93 mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
94
Marc Kupietz1f5969c2023-05-27 11:36:08 +020095plot <- icc_year %>% ggplot(aes(x=year, fill=lang, color=lang, y=tokens)) +
96 geom_line() + geom_point() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
Marc Kaa2ebf62023-05-26 13:16:01 +020097 theme_ids()
Marc Kupietz1f5969c2023-05-27 11:36:08 +020098
99ggsave("target/tokens_per_year.png", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
100ggsave("target/tokens_per_year.svg", width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
101ggsave("target/tokens_per_year.pdf", device = cairo_pdf, width = 70 * .pt, height = 45 *.pt, units = "mm", dpi = 800)
102
103if(rstudioapi::isAvailable()) {
104 print(plot)
105}
106