Marc Kupietz | a0868ec | 2023-06-02 17:59:40 +0200 | [diff] [blame] | 1 | library(RKorAPClient) |
| 2 | library(httr) |
| 3 | library(httpuv) |
| 4 | library(tidyverse) |
| 5 | library(scales) |
| 6 | library(idsThemeR) |
| 7 | library(kableExtra) |
| 8 | library(DT) |
| 9 | |
| 10 | icc_base_url = "https://korap.ids-mannheim.de/instance/icc"; |
| 11 | |
| 12 | connections = list() |
| 13 | |
| 14 | icc <- tibble( |
| 15 | lang = c("eng", "ger", "nor"), |
| 16 | app_id = c( |
| 17 | "mTTTnJ6f6hGrPh6dRhbJhJ", |
| 18 | "TMLPTJfP7rHb93bpFp39mL", |
| 19 | "TMFtPJnbb7f4MRmd76Rb34" |
| 20 | ) |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | icc_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 | |
| 42 | icc_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 | |
| 50 | icc <- icc %>% |
| 51 | rowwise() %>% |
| 52 | mutate(token = icc_token(lang, app_id)) |
| 53 | |
| 54 | genre <- 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 | |
| 70 | wordsFromQuery <- function (query) { |
| 71 | v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|")") %>% unlist() %>% unique() |
| 72 | v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')] |
| 73 | v[order(nchar(v), v, decreasing = T)] |
| 74 | } |
| 75 | |
| 76 | highliteSubstrings <- function (string, substrings) { |
| 77 | what = paste0('(', paste0(substrings, collapse="|"), ')') |
| 78 | with = '<b>\\1</b>' |
| 79 | str_replace_all(string, what, with) |
| 80 | } |
| 81 | |
| 82 | deleteFillers <- 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 Kupietz | 042dade | 2023-06-26 20:30:05 +0200 | [diff] [blame] | 91 | show_table <- function(df, max=50) { |
Marc Kupietz | a0868ec | 2023-06-02 17:59:40 +0200 | [diff] [blame] | 92 | df %>% |
| 93 | mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
Marc Kupietz | a32baeb | 2023-06-13 22:39:27 +0200 | [diff] [blame] | 94 | 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 Kupietz | a0868ec | 2023-06-02 17:59:40 +0200 | [diff] [blame] | 96 | rowwise() %>% |
| 97 | mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>% |
| 98 | select(Collocate, Example, logDice, pmi, ll) %>% |
Marc Kupietz | 042dade | 2023-06-26 20:30:05 +0200 | [diff] [blame] | 99 | head(max) %>% |
Marc Kupietz | a0868ec | 2023-06-02 17:59:40 +0200 | [diff] [blame] | 100 | datatable(escape = F) %>% |
| 101 | formatRound(columns=~logDice + pmi + ll, digits=2) |
| 102 | } |
| 103 | |
Marc Kupietz | 042dade | 2023-06-26 20:30:05 +0200 | [diff] [blame] | 104 | show__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 Kupietz | a0868ec | 2023-06-02 17:59:40 +0200 | [diff] [blame] | 125 | |