String Kernel implementation.

Classes

StringKernel

class algorithms.svm.kernels.string.StringKernel(CachedKernel)

String Subsequence Kernel (SSK) for text and sequence data.

The String Kernel measures similarity between strings by counting weighted common subsequences. Subsequences need not be contiguous --- gaps are penalized by a decay factor \lambda, allowing the kernel to capture long-range dependencies in text.
Constructor
__init__(
    self,
    subsequence_length: int = 3,
    lambda_decay: float = 0.5,
    normalize: bool = True,
    cache_size: int = 250007,
)

Overview

The kernel evaluation proceeds as follows:

  1. Enumerate all common subsequences of length up to subsequence_length
  2. Weight each subsequence occurrence by \lambda^{\ell} where \ell accounts for gap penalties
  3. Sum the weighted counts to produce the raw kernel value
  4. Optionally normalize by dividing by \sqrt{K(s,s) \cdot K(t,t)}
An efficient O(n \cdot m \cdot k) dynamic programming algorithm is used, where n, m are string lengths and k is the maximum subsequence length.

Theory

The string subsequence kernel is defined as:

K(s, t) = \sum_{u \in \Sigma^{\leq k}} \sum_{\mathbf{i}: s[\mathbf{i}]=u} \sum_{\mathbf{j}: t[\mathbf{j}]=u} \lambda^{|\mathbf{i}| + |\mathbf{j}|}
where:
  • \Sigma^{\leq k} --- Set of all subsequences up to length k
  • \mathbf{i}, \mathbf{j} --- Index tuples locating the subsequence in each string
  • \lambda \in (0, 1) --- Decay factor penalizing gaps between matched characters
  • |\mathbf{i}| --- Span of the index tuple (accounts for non-contiguous matches)
The normalized version is:
\hat{K}(s, t) = \frac{K(s, t)}{\sqrt{K(s, s) \cdot K(t, t)}}

Parameters

subsequence_length
int = 3
Maximum length of subsequences to consider.
lambda_decay
float = 0.5
Decay factor for gaps in subsequences. Must be in (0, 1].
normalize
bool = True
Whether to normalize kernel values to :math:`[0, 1]`.
cache_size
int = 250007
Maximum number of cached kernel evaluations.

Attributes

n_samples\_
int
Number of training strings stored after build().

Notes

Complexity:

  • Single evaluation: O(n \cdot m \cdot k) where n, m are string lengths, k = subsequence length
  • Matrix computation: O(N^2 \cdot \bar{n}^2 \cdot k) where N = number of strings, \bar{n} = average string length
When to use StringKernel:
  • Text classification (spam detection, sentiment analysis)
  • Biological sequence analysis (protein or DNA similarity)
  • When bag-of-words representations lose important sequential information
  • When subsequence-level similarity is more informative than exact matching

References

Lodhi2002
Lodhi, H., Saunders, C., Shawe-Taylor, J., Cristianini, N. and Watkins, C. (2002). Text Classification Using String Kernels. Journal of Machine Learning Research, 2, pp. 419-444.
Leslie2002
Leslie, C., Eskin, E. and Noble, W.S. (2002). The Spectrum Kernel: A String Kernel for SVM Protein Classification. Pacific Symposium on Biocomputing, pp. 564-575.

Basic usage for text similarity:

python
>>> from tuiml.algorithms.svm.kernels import StringKernel
>>>
>>> kernel = StringKernel(subsequence_length=3, lambda_decay=0.5)
>>> kernel.build(["hello world", "hello there", "goodbye world"])
StringKernel(...)
>>> value = kernel.compute(0, 1)

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

build (self, X) -> 'StringKernel'

Build kernel with string data.

Parameters
X
array-like or list of str
List of strings or array where each row represents a sequence.
Returns
self
StringKernel
Returns the built instance.
evaluate (self, x1, x2) -> float

Evaluate string kernel between two strings.

Parameters
x1
str or int
First string or its index in the built data.
x2
str or int
Second string or its index in the built data.
Returns
val
float
String kernel value.
compute (self, i: int, j: int) -> float

Compute kernel between training strings i and j.

Parameters
i
int
Index of first string.
j
int
Index of second string.
Returns
val
float
Kernel value for strings at indices i and j.
__repr__ (self) -> str

String representation.