blob: e207647372ad7e27d68685f8609213b2870ac0c2 [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)]
}
po2en <- function(text) {
deeplr::translate2(
text,
target_lang = "EN",
source_lang = "PO",
auth_key = "87372412-770c-73c5-cdb9-2c6bee43afc4:fx"
)
}
ro2en <- function(text) {
deeplr::translate2(
text,
target_lang = "EN",
source_lang = "RO",
auth_key = "87372412-770c-73c5-cdb9-2c6bee43afc4:fx"
)
}
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, rownames = FALSE) %>%
formatRound(columns=~logDice + pmi + ll, digits=2)
}
show_simple_table_tr <- function(df, pageLength = 20) {
df %>%
mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
select(Collocate, en, logDice, pmi, ll) %>%
datatable(options = list(pageLength = pageLength), 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)
}