Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 1 | library(RKorAPClient) |
| 2 | library(httr) |
| 3 | library(httpuv) |
| 4 | library(tidyverse) |
| 5 | library(scales) |
| 6 | library(idsThemeR) |
| 7 | library(kableExtra) |
| 8 | library(DT) |
| 9 | |
| 10 | nkjp = new("KorAPConnection", KorAPUrl = "https://korap.ids-mannheim.de/instance/nkjp1m-sgjp") |
| 11 | wordsFromQuery <- function (query) { |
| 12 | v <- str_split(query, "([! )(\uc2\uab,.:?\u201e\u201c\'\"]+|")") %>% unlist() %>% unique() |
| 13 | v <- v[str_detect(v, '^[:alnum:]+-?[:alnum:]*$')] |
| 14 | v[order(nchar(v), v, decreasing = T)] |
| 15 | } |
| 16 | |
Marc Kupietz | 9087c77 | 2023-06-27 18:11:26 +0200 | [diff] [blame] | 17 | po2en <- 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 | |
| 26 | ro2en <- 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 K | 0f82bcc | 2023-06-28 07:56:35 +0200 | [diff] [blame^] | 35 | de2en <- 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 Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 44 | highliteSubstrings <- function (string, substrings) { |
| 45 | what = paste0('(', paste0(substrings, collapse="|"), ')') |
| 46 | with = '<b>\\1</b>' |
| 47 | str_replace_all(string, what, with) |
| 48 | } |
| 49 | |
| 50 | deleteFillers <- 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 | |
| 59 | show_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 K | 0f82bcc | 2023-06-28 07:56:35 +0200 | [diff] [blame^] | 64 | # rowwise() %>% |
Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 65 | # mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>% |
| 66 | mutate(Example=example) %>% |
Marc K | 0f82bcc | 2023-06-28 07:56:35 +0200 | [diff] [blame^] | 67 | select(Collocate, EN, Example, logDice, pmi) %>% |
Marc Kupietz | 9087c77 | 2023-06-27 18:11:26 +0200 | [diff] [blame] | 68 | # head(50) %>% |
Marc Kupietz | 91d5ba9 | 2023-06-27 15:27:58 +0200 | [diff] [blame] | 69 | datatable(escape = F, rownames = FALSE) %>% |
Marc K | 0f82bcc | 2023-06-28 07:56:35 +0200 | [diff] [blame^] | 70 | formatRound(columns=~logDice + pmi, digits=2) |
Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Marc Kupietz | 9087c77 | 2023-06-27 18:11:26 +0200 | [diff] [blame] | 73 | |
Marc K | 0f82bcc | 2023-06-28 07:56:35 +0200 | [diff] [blame^] | 74 | show_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 Kupietz | 9087c77 | 2023-06-27 18:11:26 +0200 | [diff] [blame] | 82 | show_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 K | 6626400 | 2023-06-27 13:29:38 +0200 | [diff] [blame] | 90 | show_simple_table <- function(df, pageLength = 20) { |
Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 91 | df %>% |
| 92 | mutate(Collocate=sprintf('<a href="%s">%s</a>', webUIRequestUrl, collocate)) %>% |
| 93 | select(Collocate, logDice, pmi, ll) %>% |
Marc K | 6626400 | 2023-06-27 13:29:38 +0200 | [diff] [blame] | 94 | datatable(options = list(pageLength = pageLength), escape = F) %>% |
Marc Kupietz | 686c431 | 2023-06-23 15:41:44 +0200 | [diff] [blame] | 95 | formatRound(columns=~logDice + pmi + ll, digits=2) |
| 96 | } |