blob: 15ac4256da787e0fd2399d538d23eeee203f928e [file] [log] [blame]
Marc Kupietza0868ec2023-06-02 17:59:40 +02001library(RKorAPClient)
2library(httr)
3library(httpuv)
4library(tidyverse)
5library(scales)
6library(idsThemeR)
7library(kableExtra)
8library(DT)
9
10icc_base_url = "https://korap.ids-mannheim.de/instance/icc";
11
12connections = list()
13
14icc <- tibble(
15 lang = c("eng", "ger", "nor"),
16 app_id = c(
17 "mTTTnJ6f6hGrPh6dRhbJhJ",
18 "TMLPTJfP7rHb93bpFp39mL",
19 "TMFtPJnbb7f4MRmd76Rb34"
20 )
21)
22
23
24icc_token <- function(lang, app_id, url = paste0(icc_base_url, '/', lang)) {
25 token_key = paste0("KORAP_ICC_TOKEN_", lang)
26 token = Sys.getenv(token_key)
27 if (token != "")
28 return(token)
29
30 korap_app <- oauth_app("icc-iclc10-contribution", key = app_id, secret = NULL)
31
32 korap_endpoint <- oauth_endpoint(NULL,
33 "settings/oauth/authorize",
34 "api/v1.0/oauth2/token",
35 base_url = url)
36 token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = T)
37 token = token_bundle[["credentials"]][["access_token"]]
38 do.call(Sys.setenv, as.list(setNames(token, token_key)))
39 return(token)
40}
41
42icc_con <- function(lang, token = Sys.getenv(paste0("KORAP_ICC_TOKEN_", lang))) {
43 if ((! lang %in% names(connections)) || is_empty(connections[[lang]])) {
44 url <- paste0(icc_base_url, '/', lang)
45 connections[[lang]] <<- new("KorAPConnection", KorAPUrl = url, accessToken = token, cache = T)
46 }
47 return(connections[[lang]])
48}
49
50icc <- icc %>%
51 rowwise() %>%
52 mutate(token = icc_token(lang, app_id))
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
70wordsFromQuery <- function (query) {
71 v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|&quot;)") %>% unlist() %>% unique()
72 v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')]
73 v[order(nchar(v), v, decreasing = T)]
74}
75
76highliteSubstrings <- function (string, substrings) {
77 what = paste0('(', paste0(substrings, collapse="|"), ')')
78 with = '<b>\\1</b>'
79 str_replace_all(string, what, with)
80}
81
82deleteFillers <- function (string) {
83 string %>%
84 str_replace_all('</b> +<b>', ' ') %>%
85 str_replace_all('</b>[^<]+<b>', ' ... ') %>%
86 str_replace_all('^[^<]*<b>', '') %>%
87 str_replace_all('</b>[^<]*$', '')
88
89}
90
91show_table <- function(df) {
92 df %>%
93 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
94 rowwise() %>%
95 mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
96 select(Collocate, Example, logDice, pmi, ll) %>%
97 head(50) %>%
98 datatable(escape = F) %>%
99 formatRound(columns=~logDice + pmi + ll, digits=2)
100}
101
102