| library(RKorAPClient) |
| library(httr) |
| library(httpuv) |
| library(tidyverse) |
| library(scales) |
| library(idsThemeR) |
| library(kableExtra) |
| library(DT) |
| |
| nkjp = new("KorAPConnection", KorAPUrl = "https://korap.ids-mannheim.de/instance/nkjp1m-sgjp") |
| wordsFromQuery <- function (query) { |
| v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|")") %>% unlist() %>% unique() |
| v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')] |
| v[order(nchar(v), v, decreasing = T)] |
| } |
| |
| highliteSubstrings <- function (string, substrings) { |
| what = paste0('(', paste0(substrings, collapse="|"), ')') |
| with = '<b>\\1</b>' |
| str_replace_all(string, what, with) |
| } |
| |
| deleteFillers <- function (string) { |
| string %>% |
| str_replace_all('</b> +<b>', ' ') %>% |
| str_replace_all('</b>[^<]+<b>', ' ... ') %>% |
| str_replace_all('^[^<]*<b>', '') %>% |
| str_replace_all('</b>[^<]*$', '') |
| |
| } |
| |
| show_table <- function(df) { |
| df %>% |
| mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
| mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>% |
| mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>% |
| rowwise() %>% |
| # mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>% |
| mutate(Example=example) %>% |
| select(Collocate, Example, logDice, pmi, ll) %>% |
| head(50) %>% |
| datatable(escape = F) %>% |
| formatRound(columns=~logDice + pmi + ll, digits=2) |
| } |
| |
| show_simple_table <- function(df, pageLength = 20) { |
| df %>% |
| mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
| select(Collocate, logDice, pmi, ll) %>% |
| datatable(options = list(pageLength = pageLength), escape = F) %>% |
| formatRound(columns=~logDice + pmi + ll, digits=2) |
| } |