All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.ormr.fuzzywuzzy.FuzzySearch.kt Maven / Gradle / Ivy

@file:JvmName("FuzzySearch")

package net.ormr.fuzzywuzzy

import net.ormr.fuzzywuzzy.algorithms.TokenSetAlgorithm
import net.ormr.fuzzywuzzy.algorithms.TokenSortAlgorithm
import net.ormr.fuzzywuzzy.algorithms.WeightedAlgorithm
import net.ormr.fuzzywuzzy.model.BoundExtractedResult
import net.ormr.fuzzywuzzy.model.ExtractedResult
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads

// collection
/**
 * Creates a **sorted** list of [ExtractedResult]  which contain the top @param limit most similar choices.
 */
@JvmOverloads
public fun Collection.extractTopMatches(
    query: String,
    limit: Int,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List = Extractor.of(cutoff).extractTop(query, this, scoreFunc, limit)

/**
 * Creates a **sorted** list of [ExtractedResult] which contain all the choices with their corresponding score where
 * higher is more similar.
 */
@JvmOverloads
public fun Collection.extractSortedMatches(
    query: String,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List = Extractor.of(cutoff).extractTop(query, this, scoreFunc)

/**
 * Creates a list of [ExtractedResult] which contain all the choices with their corresponding score where higher is
 * more similar.
 */
@JvmOverloads
public fun Collection.extractAllMatches(
    query: String,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List = Extractor.of(cutoff).extractWithoutOrder(query, this, scoreFunc)

/**
 * Find the single best match above a score in a list of choices.
 */
@JvmOverloads
public fun Collection.extractBestMatch(
    query: String,
    scoreFunc: Applicable = WeightedAlgorithm,
): ExtractedResult = Extractor.NO_CUTOFF.extractOne(query, this, scoreFunc)

/**
 * Creates a **sorted** list of [BoundExtractedResult]  which contain the
 * top @param limit most similar choices.
 */
@JvmOverloads
public fun  Collection.extractTopMatches(
    query: String,
    toString: ToStringFunction,
    limit: Int,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List> = Extractor.of(cutoff).extractTop(query, this, toString, scoreFunc, limit)

/**
 * Creates a **sorted** list of [BoundExtractedResult] which contain all the choices with their corresponding score
 * where higher is more similar.
 */
@JvmOverloads
public fun  Collection.extractSortedMatches(
    query: String,
    toString: ToStringFunction,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List> = Extractor.of(cutoff).extractTop(query, this, toString, scoreFunc)

/**
 * Creates a list of [BoundExtractedResult] which contain all the choices with their corresponding score where higher
 * is more similar.
 */
@JvmOverloads
public fun  Collection.extractAllMatches(
    query: String,
    toString: ToStringFunction,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List> = Extractor.of(cutoff).extractWithoutOrder(query, this, toString, scoreFunc)

/**
 * Find the single best match above a score in a list of choices.
 */
@JvmOverloads
public fun  Collection.extractBestMatch(
    query: String,
    toString: ToStringFunction,
    scoreFunc: Applicable = WeightedAlgorithm,
): BoundExtractedResult = Extractor.NO_CUTOFF.extractOne(query, this, toString, scoreFunc)

// sequence
/**
 * Creates a **sorted** list of [ExtractedResult]  which contain the top @param limit most similar choices.
 */
@JvmOverloads
public fun Sequence.extractTopMatches(
    query: String,
    limit: Int,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List = Extractor.of(cutoff).extractTop(query, this, scoreFunc, limit)

/**
 * Creates a **sorted** list of [ExtractedResult] which contain all the choices with their corresponding score where
 * higher is more similar.
 */
@JvmOverloads
public fun Sequence.extractSortedMatches(
    query: String,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): Sequence = Extractor.of(cutoff).extractTop(query, this, scoreFunc)

/**
 * Creates a sequence of [ExtractedResult] which contain all the choices with their corresponding score where higher is
 * more similar.
 */
@JvmOverloads
public fun Sequence.extractAllMatches(
    query: String,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): Sequence = Extractor.of(cutoff).extractWithoutOrder(query, this, scoreFunc)

/**
 * Find the single best match above a score in a list of choices.
 */
@JvmOverloads
public fun Sequence.extractBestMatch(
    query: String,
    scoreFunc: Applicable = WeightedAlgorithm,
): ExtractedResult = Extractor.NO_CUTOFF.extractOne(query, this, scoreFunc)

/**
 * Creates a **sorted** sequence of [BoundExtractedResult]  which contain the
 * top @param limit most similar choices.
 */
@JvmOverloads
public fun  Sequence.searchTop(
    query: String,
    toString: ToStringFunction,
    limit: Int,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): List> = Extractor.of(cutoff).extractTop(query, this, toString, scoreFunc, limit)

/**
 * Creates a **sorted** sequence of [BoundExtractedResult] which contain all the choices with their corresponding score
 * where higher is more similar.
 */
@JvmOverloads
public fun  Sequence.extractSortedMatches(
    query: String,
    toString: ToStringFunction,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): Sequence> = Extractor.of(cutoff).extractTop(query, this, toString, scoreFunc)

/**
 * Creates a sequence of [BoundExtractedResult] which contain all the choices with their corresponding score where higher
 * is more similar.
 */
@JvmOverloads
public fun  Sequence.extractAllMatches(
    query: String,
    toString: ToStringFunction,
    cutoff: Int = 0,
    scoreFunc: Applicable = WeightedAlgorithm,
): Sequence> = Extractor.of(cutoff).extractWithoutOrder(query, this, toString, scoreFunc)

/**
 * Find the single best match above a score in a list of choices.
 */
@JvmOverloads
public fun  Sequence.extractBestMatch(
    query: String,
    toString: ToStringFunction,
    scoreFunc: Applicable = WeightedAlgorithm,
): BoundExtractedResult = Extractor.NO_CUTOFF.extractOne(query, this, toString, scoreFunc)

// ratio

/**
 * Calculates a Levenshtein simple ratio between `this` and [text] and returns a measure of similarity.
 */
public fun String.ratio(text: String): Int = Ratio.Simple.apply(this, text)

/**
 * Calculates a Levenshtein simple ratio between `this` and [text] and returns a measure of similarity.
 */
public fun String.ratio(text: String, toString: ToStringFunction): Int =
    Ratio.Simple.apply(this, text, toString)

/**
 * Returns a partial ratio between `this` and [text].
 *
 * Inconsistent substrings lead to problems in matching. This ratio uses a heuristic called "best partial" for when two
 * strings are of noticeably different lengths.
 */
public fun String.partialRatio(text: String): Int = Ratio.Partial.apply(this, text)

/**
 * Returns a partial ratio between `this` and [text].
 *
 * Inconsistent substrings lead to problems in matching. This ratio uses a heuristic called "best partial" for when two
 * strings are of noticeably different lengths.
 */
public fun String.partialRatio(text: String, toString: ToStringFunction): Int =
    Ratio.Partial.apply(this, text, toString)

/**
 * Find all alphanumeric tokens in the string and sort those tokens and then take ratio of resulting joined strings.
 */
public fun String.tokenSortPartialRatio(text: String): Int = TokenSortAlgorithm.apply(this, text, Ratio.Partial)

/**
 * Find all alphanumeric tokens in the string and sort those tokens and then take ratio of resulting joined strings.
 */
public fun String.tokenSortPartialRatio(text: String, toString: ToStringFunction): Int =
    TokenSortAlgorithm.apply(this, text, Ratio.Partial, toString)

/**
 * Find all alphanumeric tokens in the string and sort those tokens and then take ratio of resulting joined strings.
 */
public fun String.tokenSortRatio(text: String): Int = TokenSortAlgorithm.apply(this, text, Ratio.Simple)

/**
 * Find all alphanumeric tokens in the string and sort those tokens and then take ratio of resulting joined strings.
 */
public fun String.tokenSortRatio(text: String, stringFunction: ToStringFunction): Int =
    TokenSortAlgorithm.apply(this, text, Ratio.Simple, stringFunction)

/**
 * Splits the strings into tokens and computes intersections and remainders between the tokens of the two strings.
 * A comparison string is then built up and is compared using the simple ratio algorithm.
 *
 * Useful for strings where words appear redundantly.
 */
public fun String.tokenSetRatio(text: String): Int = TokenSetAlgorithm.apply(this, text, Ratio.Simple)

/**
 * Splits the strings into tokens and computes intersections and remainders between the tokens of the two strings.
 * A comparison string is then built up and is compared using the simple ratio algorithm.
 *
 * Useful for strings where words appear redundantly.
 */
public fun String.tokenSetRatio(text: String, stringFunction: ToStringFunction): Int =
    TokenSetAlgorithm.apply(this, text, Ratio.Simple, stringFunction)

/**
 * Splits the strings into tokens and computes intersections and remainders between the tokens of the two strings.
 * A comparison string is then  built up and is compared using the simple ratio algorithm.
 *
 * Useful for strings where words appear redundantly.
 */
public fun String.tokenSetPartialRatio(text: String): Int = TokenSetAlgorithm.apply(this, text, Ratio.Partial)

/**
 * Splits the strings into tokens and computes intersections and remainders between the tokens of the two strings.
 * A comparison string is then  built up and is compared using the simple ratio algorithm.
 *
 * Useful for strings where words appear redundantly.
 */
public fun String.tokenSetPartialRatio(text: String, toString: ToStringFunction): Int =
    TokenSetAlgorithm.apply(this, text, Ratio.Partial, toString)

/**
 * Calculates a weighted ratio between the different algorithms for best results
 */
public fun String.weightedRatio(text: String): Int = WeightedAlgorithm.apply(this, text)

/**
 * Calculates a weighted ratio between the different algorithms for best results
 */
public fun String.weightedRatio(text: String, toString: ToStringFunction): Int =
    WeightedAlgorithm.apply(this, text, toString)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy