blob: 7b3fca174c91d6ac49a9d2618d116b2203215cfe [file] [log] [blame]
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\'\"]+|&quot;)") %>% 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) {
df %>%
mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
select(Collocate, logDice, pmi, ll) %>%
head(50) %>%
datatable(options = list(pageLength = 20), escape = F) %>%
formatRound(columns=~logDice + pmi + ll, digits=2)
}