initial import

Change-Id: I46029b5f36a4711c32b8bcd14a7255cd9f495853
diff --git a/R/icc_stats.R b/R/icc_stats.R
new file mode 100644
index 0000000..d775964
--- /dev/null
+++ b/R/icc_stats.R
@@ -0,0 +1,91 @@
+library(RKorAPClient)
+library(httr)
+library(httpuv)
+library(tidyverse)
+library(scales)
+library(idsThemeR)
+
+icc_base_url = "https://korap.ids-mannheim.de/instance/icc";
+
+connections = list()
+
+icc <- tibble(
+  lang = c("eng", "ger", "nor"),
+  app_id = c(
+    "mTTTnJ6f6hGrPh6dRhbJhJ",
+    "TMLPTJfP7rHb93bpFp39mL",
+    "TMFtPJnbb7f4MRmd76Rb34"
+  )
+)
+
+
+icc_token <- function(lang,  app_id, url = paste0(icc_base_url, '/', lang)) {
+  token_key =  paste0("KORAP_ICC_TOKEN_", lang)
+  token = Sys.getenv(token_key)
+  if (token != "")
+    return(token)
+
+  korap_app <- oauth_app("icc-iclc10-contribution", key = app_id, secret = NULL)
+
+  korap_endpoint <- oauth_endpoint(NULL,
+                                   "settings/oauth/authorize",
+                                   "api/v1.0/oauth2/token",
+                                   base_url = url)
+  token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = FALSE)
+  token = token_bundle[["credentials"]][["access_token"]]
+  do.call(Sys.setenv, as.list(setNames(token, token_key)))
+  return(token)
+}
+
+icc_con <- function(lang, token = Sys.getenv(paste0("KORAP_ICC_TOKEN_", lang))) {
+  if ((! lang %in% names(connections)) || is_empty(connections[[lang]])) {
+    url <- paste0(icc_base_url, '/', lang)
+    connections[[lang]] <<- new("KorAPConnection", KorAPUrl = url, accessToken = token, cache = F)
+  }
+  return(connections[[lang]])
+}
+
+icc <- icc %>%
+  rowwise() %>%
+  mutate(token = icc_token(lang, app_id)) %>%
+  add_column(tokens=0)
+
+
+genre <- c("Blog",
+             "Creative:Novels_ShortStories",
+             "Informational:Learned:Humanities",
+             "Informational:Learned:NaturalSciences",
+             "Informational:Learned:SocialSciences",
+             "Informational:Learned:Technology",
+             "Informational:Popular:Humanities",
+             "Informational:Popular:NaturalSciences",
+             "Informational:Popular:SocialSciences",
+             "Informational:Popular:Technology",
+             "Informational:Reportage",
+             "Instructional:AdministrativeRegulatoryProse",
+             "Instructional:Skills_Hobbies",
+             "Persuasive"
+)
+
+icc_genre <- icc %>%
+  expand_grid(genre) %>%
+  mutate(vc = paste0("iccGenre=", genre)) %>%
+  rowwise() %>%
+  mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
+
+icc_genre %>% ggplot(aes(x=lang, fill=genre, y=tokens)) +
+  geom_col() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
+  theme_ids() +
+  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")
+
+year <- c(1988:2022)
+
+icc_year <- icc %>%
+  expand_grid(year) %>%
+  mutate(vc = paste0("pubDate in ", year)) %>%
+  rowwise() %>%
+  mutate(tokens= corpusStats(icc_con(lang, token), vc = vc)@tokens)
+
+icc_year %>% ggplot(aes(x=year, fill=lang, color=lang, y=tokens)) +
+  geom_line() + geom_point() + geom_area() + scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
+  theme_ids()
diff --git a/icc-iclc10.Rproj b/icc-iclc10.Rproj
new file mode 100644
index 0000000..b42a3cc
--- /dev/null
+++ b/icc-iclc10.Rproj
@@ -0,0 +1,16 @@
+Version: 1.0
+
+RestoreWorkspace: Default
+SaveWorkspace: Default
+AlwaysSaveHistory: Default
+
+EnableCodeIndexing: Yes
+UseSpacesForTab: Yes
+NumSpacesForTab: 2
+Encoding: UTF-8
+
+RnwWeave: knitr
+LaTeX: XeLaTeX
+
+AutoAppendNewline: Yes
+StripTrailingWhitespace: Yes