Import

Change-Id: I3e94bfa960151d26d2420393133cdce0d1f69797
diff --git a/common.R b/common.R
new file mode 100644
index 0000000..7b3fca1
--- /dev/null
+++ b/common.R
@@ -0,0 +1,53 @@
+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)
+}