CmpStr - v3.2.1
    Preparing search index...

    Class Metric<R>Abstract

    Abstract class representing a generic string metric.

    Type Parameters

    • R = MetricRaw

      The type of the raw result, defaulting to MetricRaw.

    Hierarchy (View Summary)

    Index

    Constructors

    • Constructor for the Metric class. Initializes the metric with two inputs (strings or arrays of strings) and options.

      Type Parameters

      • R = MetricRaw

        The type of the raw result, defaulting to MetricRaw.

      Parameters

      • metric: string

        The name of the metric (e.g. 'levenshtein')

      • a: MetricInput

        First input string or array of strings

      • b: MetricInput

        Second input string or array of strings

      • Optionalopt: MetricOptions = {}

        Options for the metric computation

      • Optionalsymmetric: boolean = false

        Whether the metric is symmetric (same result for inputs in any order)

      Returns Metric<R>

      • If inputs a or b are empty

    Properties

    a: string[]

    Inputs for the metric computation, transformed into arrays

    b: string[]
    metric: string

    Metric name for identification

    options: MetricOptions

    Options for the metric computation, such as performance tracking

    optKey: string
    origA: string[] = []

    Store original inputs for result mapping

    origB: string[] = []
    results: MetricResult<R> | undefined

    Result of the metric computation, which can be a single result or an array of results. This will be populated after running the metric.

    symmetric: boolean

    Indicates whether the metric is symmetric (same result for inputs in any order)

    cache: HashTable<string, MetricCompute<any>> = ...

    Cache for metric computations to avoid redundant calculations

    Methods

    • Clear the cached results of the metric.

      This method resets the results property to undefined, effectively clearing any previously computed results. It can be useful for re-running the metric with new inputs or options.

      Returns void

    • Abstract method to be implemented by subclasses to perform the metric computation. This method should contain the logic for computing the metric between two strings.

      Parameters

      • a: string

        First string

      • b: string

        Second string

      • m: number

        Length of the first string

      • n: number

        Length of the second string

      • maxLen: number

        Maximum length of the strings

      Returns MetricCompute<R>

      • The result of the metric computation
      • If not overridden in a subclass
    • Get the name of the metric.

      Returns string

      • The name of the metric
    • Check if the inputs are in batch mode.

      This method checks if either a or b contains more than one string, indicating that the metric is being run in batch mode.

      Returns boolean

      • True if either input is an array with more than one element
    • Check if the inputs are in pairwise mode.

      This method checks if both a and b are arrays of the same length, indicating that the metric is being run on corresponding pairs of strings.

      Parameters

      • Optionalsafe: boolean = false

        If true, does not throw an error if lengths are not equal

      Returns boolean

      • True if both inputs are arrays of equal length
      • If safe is false and the lengths of a and b are not equal
    • Check if the inputs are in single mode.

      This method checks if both a and b are single strings (not arrays), indicating that the metric is being run on a single pair of strings.

      Returns boolean

      • True if both inputs are single strings
    • Check if the metric is symmetrical.

      This method returns whether the metric is symmetric, meaning it produces the same result regardless of the order of inputs (e.g., Levenshtein distance).

      Returns boolean

      • True if the metric is symmetric
    • Pre-compute the metric for two strings. This method is called before the actual computation to handle trivial cases.

      Parameters

      • a: string

        First string

      • b: string

        Second string

      • m: number

        Length of the first string

      • n: number

        Length of the second string

      Returns MetricCompute<R> | undefined

      • Pre-computed result or undefined if not applicable
    • Run the metric computation based on the specified mode.

      Parameters

      • Optionalmode: MetricMode

        The mode to run the metric in (optional)

      • Optionalclear: boolean = true

        Whether to clear previous results before running

      Returns void

      • If an unsupported mode is specified
    • Run the metric computation based on the specified mode asynchronously.

      Parameters

      • Optionalmode: MetricMode

        The mode to run the metric in (optional)

      • Optionalclear: boolean = true

        Whether to clear previous results before running

      Returns Promise<void>

      • A promise that resolves when the metric computation is complete
      • If an unsupported mode is specified
    • Run the metric computation for batch inputs (arrays of strings).

      It iterates through each string in the first array and computes the metric against each string in the second array.

      Returns void

    • Run the metric computation for batch inputs (arrays of strings) asynchronously.

      Returns Promise<void>

    • Run the metric computation for pairwise inputs (A[i] vs B[i]).

      This method assumes that both a and b are arrays of equal length and computes the metric only for corresponding index pairs.

      Returns void

    • Run the metric computation for pairwise inputs (A[i] vs B[i]) asynchronously.

      Returns Promise<void>

    • Run the metric computation for single inputs (two strings). Applies preCompute for trivial cases before cache lookup and computation.

      If the profiler is active, it will measure time and memory usage.

      Parameters

      • i: number

        Pointer to the first string

      • j: number

        Pointer to the second string

      Returns MetricResultSingle<R>

      • The result of the metric computation
    • Run the metric computation for single inputs (two strings) asynchronously.

      Parameters

      • i: number

        Pointer to the first string

      • j: number

        Pointer to the second string

      Returns Promise<MetricResultSingle<R>>

      • Promise resolving the result of the metric computation
    • Determine which mode to run the metric in.

      This method checks the provided mode or defaults to the mode specified in options. If no mode is specified, it defaults to 'default'.

      Parameters

      • Optionalmode: MetricMode

        The mode to run the metric in (optional)

      Returns MetricMode

      • The determined mode
    • Clamps the similarity result between 0 and 1.

      Parameters

      • res: number

        The input similarity to clamp

      Returns number

      • The clamped similarity (0 to 1)
    • Static method to clear the cache of metric computations.

      Returns void

    • Swaps two strings and their lengths if the first is longer than the second.

      Parameters

      • a: string

        First string

      • b: string

        Second string

      • m: number

        Length of the first string

      • n: number

        Length of the second string

      Returns [string, string, number, number]

      • Swapped strings and lengths