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

commonMain.com.dshatz.fuzzykat.diffutils.algorithms.TokenSet.kt Maven / Gradle / Ivy

The newest version!
package com.dshatz.fuzzykat.diffutils.algorithms

import com.dshatz.fuzzykat.Ratio
import com.dshatz.fuzzykat.ToStringFunction

class TokenSet : RatioAlgorithm() {

    override fun apply(s1: String, s2: String, ratio: Ratio, stringFunction: ToStringFunction): Int {
        var s1Copy = s1
        var s2Copy = s2

        s1Copy = stringFunction.apply(s1Copy)
        s2Copy = stringFunction.apply(s2Copy)

        val tokens1 = Utils.tokenizeSet(s1Copy)
        val tokens2 = Utils.tokenizeSet(s2Copy)

        val intersection = SetUtils.intersection(tokens1, tokens2)
        val diff1to2 = SetUtils.difference(tokens1, tokens2)
        val diff2to1 = SetUtils.difference(tokens2, tokens1)

        val sortedInter = Utils.sortAndJoin(intersection, " ").trim()
        val sorted1to2 = (sortedInter + " " + Utils.sortAndJoin(diff1to2, " ")).trim { it <= ' ' }
        val sorted2to1 = (sortedInter + " " + Utils.sortAndJoin(diff2to1, " ")).trim { it <= ' ' }

        val results = ArrayList()

        results.add(ratio.apply(sortedInter, sorted1to2))
        results.add(ratio.apply(sortedInter, sorted2to1))
        results.add(ratio.apply(sorted1to2, sorted2to1))

        return results.max()

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy