blob: 8d5be80456d054668242a14113b8eb097c99a179 [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) {
Marc Kupietz202836a2023-06-28 20:58:17 +020077 what = paste0('(', paste0(substrings, collapse="(?=\\W)|"), '(?=\\W))')
Marc Kupietza0868ec2023-06-02 17:59:40 +020078 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
Marc Kupietz042dade2023-06-26 20:30:05 +020091show_table <- function(df, max=50) {
Marc Kupietza0868ec2023-06-02 17:59:40 +020092 df %>%
93 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
Marc Kupietza32baeb2023-06-13 22:39:27 +020094 mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>%
95 mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>%
Marc Kupietza0868ec2023-06-02 17:59:40 +020096 rowwise() %>%
97 mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
98 select(Collocate, Example, logDice, pmi, ll) %>%
Marc Kupietz042dade2023-06-26 20:30:05 +020099 head(max) %>%
Marc Kupietza0868ec2023-06-02 17:59:40 +0200100 datatable(escape = F) %>%
101 formatRound(columns=~logDice + pmi + ll, digits=2)
102}
103
Marc Kupietz042dade2023-06-26 20:30:05 +0200104show__full_table <- function(df, max=5000) {
105 df %>%
106 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
107 mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>%
108 mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>%
109 rowwise() %>%
110 mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
111 mutate(url=paste0("=HYPERLINK('", webUIRequestUrl,"')")) %>%
112 select(Collocate, Example, url, logDice, pmi, ll) %>%
113 head(max) %>%
114 datatable(escape = F, extensions = 'Buttons', options = list(
115 paging = TRUE,
116 searching = TRUE,
117 fixedColumns = TRUE,
118 autoWidth = TRUE,
119 ordering = TRUE,
120 dom = 'tB',
121 buttons = c('copy', 'csv', 'excel')
122 )) %>%
123 formatRound(columns=~logDice + pmi + ll, digits=2)
124}
Marc Kupietza0868ec2023-06-02 17:59:40 +0200125