Introduce common.h

Change-Id: Iaa2a583deafd22ab17df62e827754799a5b0ca87
diff --git a/R/common.R b/R/common.R
new file mode 100644
index 0000000..15ac425
--- /dev/null
+++ b/R/common.R
@@ -0,0 +1,102 @@
+library(RKorAPClient)
+library(httr)
+library(httpuv)
+library(tidyverse)
+library(scales)
+library(idsThemeR)
+library(kableExtra)
+library(DT)
+
+icc_base_url = "https://korap.ids-mannheim.de/instance/icc";
+
+connections = list()
+
+icc <- tibble(
+  lang = c("eng", "ger", "nor"),
+  app_id = c(
+    "mTTTnJ6f6hGrPh6dRhbJhJ",
+    "TMLPTJfP7rHb93bpFp39mL",
+    "TMFtPJnbb7f4MRmd76Rb34"
+  )
+)
+
+
+icc_token <- function(lang,  app_id, url = paste0(icc_base_url, '/', lang)) {
+  token_key =  paste0("KORAP_ICC_TOKEN_", lang)
+  token = Sys.getenv(token_key)
+  if (token != "")
+    return(token)
+
+  korap_app <- oauth_app("icc-iclc10-contribution", key = app_id, secret = NULL)
+
+  korap_endpoint <- oauth_endpoint(NULL,
+                                   "settings/oauth/authorize",
+                                   "api/v1.0/oauth2/token",
+                                   base_url = url)
+  token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = T)
+  token = token_bundle[["credentials"]][["access_token"]]
+  do.call(Sys.setenv, as.list(setNames(token, token_key)))
+  return(token)
+}
+
+icc_con <- function(lang, token = Sys.getenv(paste0("KORAP_ICC_TOKEN_", lang))) {
+  if ((! lang %in% names(connections)) || is_empty(connections[[lang]])) {
+    url <- paste0(icc_base_url, '/', lang)
+    connections[[lang]] <<- new("KorAPConnection", KorAPUrl = url, accessToken = token, cache = T)
+  }
+  return(connections[[lang]])
+}
+
+icc <- icc %>%
+  rowwise() %>%
+  mutate(token = icc_token(lang, app_id))
+
+genre <- c("Blog",
+             "Creative:Novels_ShortStories",
+             "Informational:Learned:Humanities",
+             "Informational:Learned:NaturalSciences",
+             "Informational:Learned:SocialSciences",
+             "Informational:Learned:Technology",
+             "Informational:Popular:Humanities",
+             "Informational:Popular:NaturalSciences",
+             "Informational:Popular:SocialSciences",
+             "Informational:Popular:Technology",
+             "Informational:Reportage",
+             "Instructional:AdministrativeRegulatoryProse",
+             "Instructional:Skills_Hobbies",
+             "Persuasive"
+)
+
+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)) %>%
+    rowwise() %>%
+    mutate(Example=highliteSubstrings(example, wordsFromQuery(query))) %>%
+    select(Collocate, Example, logDice, pmi, ll) %>%
+    head(50) %>%
+    datatable(escape = F) %>%
+    formatRound(columns=~logDice + pmi + ll, digits=2)
+}
+
+
diff --git a/R/icc_stats.R b/R/icc_stats.R
index 9d8f4a1..f7bdda6 100644
--- a/R/icc_stats.R
+++ b/R/icc_stats.R
@@ -1,69 +1,4 @@
-library(RKorAPClient)
-library(httr)
-library(httpuv)
-library(tidyverse)
-library(scales)
-library(idsThemeR)
-
-icc_base_url = "https://korap.ids-mannheim.de/instance/icc";
-
-connections = list()
-
-icc <- tibble(
-  lang = c("eng", "ger", "nor"),
-  app_id = c(
-    "mTTTnJ6f6hGrPh6dRhbJhJ",
-    "TMLPTJfP7rHb93bpFp39mL",
-    "TMFtPJnbb7f4MRmd76Rb34"
-  )
-)
-
-
-icc_token <- function(lang,  app_id, url = paste0(icc_base_url, '/', lang)) {
-  token_key =  paste0("KORAP_ICC_TOKEN_", lang)
-  token = Sys.getenv(token_key)
-  if (token != "")
-    return(token)
-
-  korap_app <- oauth_app("icc-iclc10-contribution", key = app_id, secret = NULL)
-
-  korap_endpoint <- oauth_endpoint(NULL,
-                                   "settings/oauth/authorize",
-                                   "api/v1.0/oauth2/token",
-                                   base_url = url)
-  token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = FALSE)
-  token = token_bundle[["credentials"]][["access_token"]]
-  do.call(Sys.setenv, as.list(setNames(token, token_key)))
-  return(token)
-}
-
-icc_con <- function(lang, token = Sys.getenv(paste0("KORAP_ICC_TOKEN_", lang))) {
-  if ((! lang %in% names(connections)) || is_empty(connections[[lang]])) {
-    url <- paste0(icc_base_url, '/', lang)
-    connections[[lang]] <<- new("KorAPConnection", KorAPUrl = url, accessToken = token, cache = F)
-  }
-  return(connections[[lang]])
-}
-
-icc <- icc %>%
-  rowwise() %>%
-  mutate(token = icc_token(lang, app_id))
-
-genre <- c("Blog",
-             "Creative:Novels_ShortStories",
-             "Informational:Learned:Humanities",
-             "Informational:Learned:NaturalSciences",
-             "Informational:Learned:SocialSciences",
-             "Informational:Learned:Technology",
-             "Informational:Popular:Humanities",
-             "Informational:Popular:NaturalSciences",
-             "Informational:Popular:SocialSciences",
-             "Informational:Popular:Technology",
-             "Informational:Reportage",
-             "Instructional:AdministrativeRegulatoryProse",
-             "Instructional:Skills_Hobbies",
-             "Persuasive"
-)
+source("R/common.R")
 
 icc_genre <- icc %>%
   expand_grid(genre) %>%