Add queryStringToLabel to package misc functions

Change-Id: I300b05c5d379ce868ce71665000471379345cbb5
diff --git a/DESCRIPTION b/DESCRIPTION
index 7218d88..9f0b936 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -27,7 +27,8 @@
     htmlwidgets,
     utils,
     httr,
-    methods
+    methods,
+    PTXQC
 Collate: 
     'KorAPConnection.R'
     'KorAPCorpusStats.R'
diff --git a/NAMESPACE b/NAMESPACE
index 404af0c..86db8dc 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -12,6 +12,7 @@
 export(group_by)
 export(ipm)
 export(mutate)
+export(queryStringToLabel)
 export(select)
 export(summarise)
 export(tidy)
@@ -36,6 +37,8 @@
 import(keyring)
 import(methods)
 import(utils)
+importFrom(PTXQC,lcpCount)
+importFrom(PTXQC,lcsCount)
 importFrom(broom,tidy)
 importFrom(dplyr,.data)
 importFrom(dplyr,bind_cols)
diff --git a/R/misc.R b/R/misc.R
index b90841e..26ae123 100644
--- a/R/misc.R
+++ b/R/misc.R
@@ -21,6 +21,35 @@
     mutate(ipm = .data$f * 10^6, conf.low = .data$conf.low * 10^6, conf.high = .data$conf.high * 10^6)
 }
 
+#' Convert query or vc strings to plot labels
+#'
+#' Converts a vector of query or vc strings to typically appropriate legend labels
+#' by clipping off prefixes and suffixes that are common to all query strings.
+#'
+#' @param data string or vector of query or vc definition strings
+#' @return string or vector of strings with clipped off common prefixes and suffixes
+#'
+#' @examples
+#' queryStringToLabel(paste("textType = /Zeit.*/ & pubDate in", c(2010:2019)))
+#' queryStringToLabel(c("[marmot/m=mood:subj]", "[marmot/m=mood:ind]"))
+#' queryStringToLabel(c("wegen dem [tt/p=NN]", "wegen des [tt/p=NN]"))
+#'
+#' @importFrom PTXQC lcpCount
+#' @importFrom PTXQC lcsCount
+#'
+#' @export
+queryStringToLabel <- function(data) {
+  leftCommon = lcpCount(data)
+  while (leftCommon > 0 && grepl("[[:alnum:]]", substring(data[1], leftCommon, leftCommon))) {
+    leftCommon <- leftCommon - 1
+  }
+  rightCommon = lcsCount(data)
+  while (rightCommon > 0 && grepl("[[:alnum:]]", substring(data[1], rightCommon, rightCommon))) {
+    rightCommon <- rightCommon - 1
+  }
+  substring(data, leftCommon + 1, nchar(data) - rightCommon)
+}
+
 
 ## Mute notes: "Undefined global functions or variables:"
 globalVariables(c("conf.high", "conf.low", "onRender", "webUIRequestUrl"))
diff --git a/demo/mosaicplot.R b/demo/mosaicplot.R
index d0d16ae..c8bf16b 100644
--- a/demo/mosaicplot.R
+++ b/demo/mosaicplot.R
@@ -7,19 +7,6 @@
 library(vcd)
 library(tibble)
 library(dplyr)
-library(PTXQC)
-
-queryStringToLabel <- function(data) {
-  leftCommon = lcpCount(data)
-  while (leftCommon > 0 && grepl("[[:alpha:]]", substring(data[1], leftCommon, leftCommon))) {
-    leftCommon <- leftCommon - 1
-  }
-  rightCommon = lcsCount(data)
-  while (rightCommon > 0 && grepl("[[:alpha:]]", substring(data[1], rightCommon, rightCommon))) {
-    rightCommon <- rightCommon - 1
-  }
-  substring(data, leftCommon + 1, nchar(data) - rightCommon)
-}
 
 mosaicplot <- function(query, vc, kco = new("KorAPConnection", verbose = TRUE)) {
   frequencyQuery(
diff --git a/man/queryStringToLabel.Rd b/man/queryStringToLabel.Rd
new file mode 100644
index 0000000..2381651
--- /dev/null
+++ b/man/queryStringToLabel.Rd
@@ -0,0 +1,24 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/misc.R
+\name{queryStringToLabel}
+\alias{queryStringToLabel}
+\title{Convert query or vc strings to plot labels}
+\usage{
+queryStringToLabel(data)
+}
+\arguments{
+\item{data}{string or vector of query or vc definition strings}
+}
+\value{
+string or vector of strings with clipped off common prefixes and suffixes
+}
+\description{
+Converts a vector of query or vc strings to typically appropriate legend labels
+by clipping off prefixes and suffixes that are common to all query strings.
+}
+\examples{
+queryStringToLabel(paste("textType = /Zeit.*/ & pubDate in", c(2010:2019)))
+queryStringToLabel(c("[marmot/m=mood:subj]", "[marmot/m=mood:ind]"))
+queryStringToLabel(c("wegen dem [tt/p=NN]", "wegen des [tt/p=NN]"))
+
+}