blob: 9bd2116369da47efdbb746b00ba5036255fd8b99 [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 K0f82bcc2023-06-28 07:56:35 +020035de2en <- function(text) {
36 deeplr::translate2(
37 text,
38 target_lang = "EN",
39 source_lang = "DE",
40 auth_key = "87372412-770c-73c5-cdb9-2c6bee43afc4:fx"
41 )
42}
43
Marc Kupietz686c4312023-06-23 15:41:44 +020044highliteSubstrings <- function (string, substrings) {
45 what = paste0('(', paste0(substrings, collapse="|"), ')')
46 with = '<b>\\1</b>'
47 str_replace_all(string, what, with)
48}
49
50deleteFillers <- function (string) {
51 string %>%
52 str_replace_all('</b> +<b>', ' ') %>%
53 str_replace_all('</b>[^<]+<b>', ' ... ') %>%
54 str_replace_all('^[^<]*<b>', '') %>%
55 str_replace_all('</b>[^<]*$', '')
56
57}
58
59show_table <- function(df) {
60 df %>%
61 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
62 mutate(example=str_replace(example, ".*(\\W+\\w+\\W+\\w+\\W+<mark.*/mark>.*)", "\\1")) %>%
63 mutate(example=str_replace(example, "(.*<mark.*/mark>\\W+\\w+\\W+\\w+).*", "\\1")) %>%
Marc K0f82bcc2023-06-28 07:56:35 +020064# rowwise() %>%
Marc Kupietz686c4312023-06-23 15:41:44 +020065# mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
66 mutate(Example=example) %>%
Marc K0f82bcc2023-06-28 07:56:35 +020067 select(Collocate, EN, Example, logDice, pmi) %>%
Marc Kupietz9087c772023-06-27 18:11:26 +020068# head(50) %>%
Marc Kupietz91d5ba92023-06-27 15:27:58 +020069 datatable(escape = F, rownames = FALSE) %>%
Marc K0f82bcc2023-06-28 07:56:35 +020070 formatRound(columns=~logDice + pmi, digits=2)
Marc Kupietz686c4312023-06-23 15:41:44 +020071}
72
Marc Kupietz9087c772023-06-27 18:11:26 +020073
Marc K0f82bcc2023-06-28 07:56:35 +020074show_lvc_table <- function(df, pageLength = 10) {
75 df %>%
76# mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
77 select(LVC, EN, logDice, pmi, ll) %>%
78 datatable(options = list(pageLength = pageLength), escape = F, rownames = FALSE) %>%
79 formatRound(columns=~logDice + pmi + ll, digits=2)
80}
81
Marc Kupietz9087c772023-06-27 18:11:26 +020082show_simple_table_tr <- function(df, pageLength = 20) {
83 df %>%
84 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
85 select(Collocate, en, logDice, pmi, ll) %>%
86 datatable(options = list(pageLength = pageLength), escape = F) %>%
87 formatRound(columns=~logDice + pmi + ll, digits=2)
88}
89
Marc K66264002023-06-27 13:29:38 +020090show_simple_table <- function(df, pageLength = 20) {
Marc Kupietz686c4312023-06-23 15:41:44 +020091 df %>%
92 mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>%
93 select(Collocate, logDice, pmi, ll) %>%
Marc K66264002023-06-27 13:29:38 +020094 datatable(options = list(pageLength = pageLength), escape = F) %>%
Marc Kupietz686c4312023-06-23 15:41:44 +020095 formatRound(columns=~logDice + pmi + ll, digits=2)
96}