blob: e207647372ad7e27d68685f8609213b2870ac0c2 [file] [log] [blame]
Marc Kupietz686c4312023-06-23 15:41:44 +02001library(RKorAPClient)
2library(httr)
3library(httpuv)
4library(tidyverse)
5library(scales)
6library(idsThemeR)
7library(kableExtra)
8library(DT)
9
10nkjp = new("KorAPConnection", KorAPUrl = "https://korap.ids-mannheim.de/instance/nkjp1m-sgjp")
11wordsFromQuery <- function (query) {
12 v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|&quot;)") %>% unlist() %>% unique()
13 v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')]
14 v[order(nchar(v), v, decreasing = T)]
15}
16
Marc Kupietz9087c772023-06-27 18:11:26 +020017po2en <- function(text) {
18 deeplr::translate2(
19 text,
20 target_lang = "EN",
21 source_lang = "PO",
22 auth_key = "87372412-770c-73c5-cdb9-2c6bee43afc4:fx"
23 )
24}
25
26ro2en <- function(text) {
27 deeplr::translate2(
28 text,
29 target_lang = "EN",
30 source_lang = "RO",
31 auth_key = "87372412-770c-73c5-cdb9-2c6bee43afc4:fx"
32 )
33}
34
Marc Kupietz686c4312023-06-23 15:41:44 +020035highliteSubstrings <- function (string, substrings) {
36 what = paste0('(', paste0(substrings, collapse="|"), ')')
37 with = '<b>\\1</b>'
38 str_replace_all(string, what, with)
39}
40
41deleteFillers <- function (string) {
42 string %>%
43 str_replace_all('</b> +<b>', ' ') %>%
44 str_replace_all('</b>[^<]+<b>', ' ... ') %>%
45 str_replace_all('^[^<]*<b>', '') %>%
46 str_replace_all('</b>[^<]*$', '')
47
48}
49
50show_table <- function(df) {
51 df %>%
52 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
53 mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>%
54 mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>%
55 rowwise() %>%
56# mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
57 mutate(Example=example) %>%
58 select(Collocate, Example, logDice, pmi, ll) %>%
Marc Kupietz9087c772023-06-27 18:11:26 +020059# head(50) %>%
Marc Kupietz91d5ba92023-06-27 15:27:58 +020060 datatable(escape = F, rownames = FALSE) %>%
Marc Kupietz686c4312023-06-23 15:41:44 +020061 formatRound(columns=~logDice + pmi + ll, digits=2)
62}
63
Marc Kupietz9087c772023-06-27 18:11:26 +020064
65show_simple_table_tr <- function(df, pageLength = 20) {
66 df %>%
67 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
68 select(Collocate, en, logDice, pmi, ll) %>%
69 datatable(options = list(pageLength = pageLength), escape = F) %>%
70 formatRound(columns=~logDice + pmi + ll, digits=2)
71}
72
Marc K66264002023-06-27 13:29:38 +020073show_simple_table <- function(df, pageLength = 20) {
Marc Kupietz686c4312023-06-23 15:41:44 +020074 df %>%
75 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
76 select(Collocate, logDice, pmi, ll) %>%
Marc K66264002023-06-27 13:29:38 +020077 datatable(options = list(pageLength = pageLength), escape = F) %>%
Marc Kupietz686c4312023-06-23 15:41:44 +020078 formatRound(columns=~logDice + pmi + ll, digits=2)
79}