Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +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 | nkjp = new("KorAPConnection", KorAPUrl = "https://korap.ids-mannheim.de/instance/nkjp1m-sgjp") |
| 11 | wordsFromQuery <- function (query) { |
| 12 | v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|")") %>% unlist() %>% unique() |
| 13 | v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')] |
| 14 | v[order(nchar(v), v, decreasing = T)] |
| 15 | } |
| 16 | |
| 17 | highliteSubstrings <- function (string, substrings) { |
| 18 | what = paste0('(', paste0(substrings, collapse="|"), ')') |
| 19 | with = '<b>\\1</b>' |
| 20 | str_replace_all(string, what, with) |
| 21 | } |
| 22 | |
| 23 | deleteFillers <- function (string) { |
| 24 | string %>% |
| 25 | str_replace_all('</b> +<b>', ' ') %>% |
| 26 | str_replace_all('</b>[^<]+<b>', ' ... ') %>% |
| 27 | str_replace_all('^[^<]*<b>', '') %>% |
| 28 | str_replace_all('</b>[^<]*$', '') |
| 29 | |
| 30 | } |
| 31 | |
| 32 | show_table <- function(df) { |
| 33 | df %>% |
| 34 | mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
| 35 | mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>% |
| 36 | mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>% |
| 37 | rowwise() %>% |
| 38 | # mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>% |
| 39 | mutate(Example=example) %>% |
| 40 | select(Collocate, Example, logDice, pmi, ll) %>% |
| 41 | head(50) %>% |
| 42 | datatable(escape = F) %>% |
| 43 | formatRound(columns=~logDice + pmi + ll, digits=2) |
| 44 | } |
| 45 | |
| 46 | show_simple_table <- function(df) { |
| 47 | df %>% |
| 48 | mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
| 49 | select(Collocate, logDice, pmi, ll) %>% |
| 50 | head(50) %>% |
| 51 | datatable(options = list(pageLength = 20), escape = F) %>% |
| 52 | formatRound(columns=~logDice + pmi + ll, digits=2) |
| 53 | } |