diff options
author | Rocky Madden <git@rockymadden.com> | 2013-12-29 14:32:21 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-12-29 14:32:21 -0700 |
commit | 5fa5030a5858acee69525c0a84ca11b2b0499eed (patch) | |
tree | 2f00a301f75a4618aba67f0970c8dac8806e3c16 /core/source | |
parent | 0c0322939ded19cc8035110a21ed2c06711fc19f (diff) | |
download | stringmetric-5fa5030a5858acee69525c0a84ca11b2b0499eed.tar.gz stringmetric-5fa5030a5858acee69525c0a84ca11b2b0499eed.tar.bz2 stringmetric-5fa5030a5858acee69525c0a84ca11b2b0499eed.zip |
Added StringAlgorithmDecorator and StringMetricDecorator.
Diffstat (limited to 'core/source')
5 files changed, 105 insertions, 45 deletions
diff --git a/core/source/main/scala/com/rockymadden/stringmetric/Algorithm.scala b/core/source/main/scala/com/rockymadden/stringmetric/Algorithm.scala index 79821bd..dd810b8 100755 --- a/core/source/main/scala/com/rockymadden/stringmetric/Algorithm.scala +++ b/core/source/main/scala/com/rockymadden/stringmetric/Algorithm.scala @@ -1,6 +1,9 @@ package com.rockymadden.stringmetric object Algorithm { + import com.rockymadden.stringmetric.Transform.StringTransform + + trait Algorithm[A] { def compute(a: A): Option[A] } @@ -28,4 +31,16 @@ object Algorithm { def computeWithSoundex(a: Array[Char]) = Soundex.compute(a) } + + + final class StringAlgorithmDecorator(val sa: StringAlgorithm) { + val withTransform: (StringTransform => StringAlgorithm) = (st) => new StringAlgorithm { + private[this] val self: StringAlgorithm = sa + private[this] val transform: StringTransform = st + + override def compute(a: Array[Char]): Option[Array[Char]] = self.compute(transform(a)) + + override def compute(a: String): Option[String] = self.compute(transform(a.toCharArray)).map(_.mkString) + } + } } diff --git a/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala b/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala index ea70b9c..2a62456 100755 --- a/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala +++ b/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala @@ -1,6 +1,9 @@ package com.rockymadden.stringmetric object Metric { + import com.rockymadden.stringmetric.Transform.StringTransform + + trait Metric[A, B] { def compare(a: A, b: A): Option[B] } @@ -58,4 +61,17 @@ object Metric { WeightedLevenshtein(delete, insert, substitute).compare(a, b) } + + final class StringMetricDecorator[A](val sm: StringMetric[A]) { + val withTransform: (StringTransform => StringMetric[A]) = (st) => new StringMetric[A] { + private[this] val self: StringMetric[A] = sm + private[this] val transform: StringTransform = st + + override def compare(a: Array[Char], b: Array[Char]): Option[A] = + self.compare(transform(a), transform(b)) + + override def compare(a: String, b: String): Option[A] = + self.compare(transform(a.toCharArray), transform(b.toCharArray)) + } + } } diff --git a/core/source/main/scala/com/rockymadden/stringmetric/package.scala b/core/source/main/scala/com/rockymadden/stringmetric/package.scala index 5aefdff..07957c8 100755 --- a/core/source/main/scala/com/rockymadden/stringmetric/package.scala +++ b/core/source/main/scala/com/rockymadden/stringmetric/package.scala @@ -2,9 +2,16 @@ package com.rockymadden package object stringmetric { import scala.language.implicitConversions + import com.rockymadden.stringmetric.Algorithm._ + import com.rockymadden.stringmetric.Metric._ type CompareTuple[T] = (Array[T], Array[T]) type MatchTuple[T] = (Array[T], Array[T]) - implicit def stringToCharArray(s: String): Array[Char] = s.toCharArray + implicit def stringToCharArray(s: String): Array[Char] = + s.toCharArray + implicit def stringAlgorithmToDecoratedStringAlgorithm(sa: StringAlgorithm): StringAlgorithmDecorator = + new StringAlgorithmDecorator(sa) + implicit def stringMetricToDecoratedStringMetric[A](sa: StringMetric[A]): StringMetricDecorator[A] = + new StringMetricDecorator[A](sa) } diff --git a/core/source/test/scala/com/rockymadden/stringmetric/AlgorithmSpec.scala b/core/source/test/scala/com/rockymadden/stringmetric/AlgorithmSpec.scala index 700328d..4e0aef8 100644 --- a/core/source/test/scala/com/rockymadden/stringmetric/AlgorithmSpec.scala +++ b/core/source/test/scala/com/rockymadden/stringmetric/AlgorithmSpec.scala @@ -4,21 +4,31 @@ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) -final class AlgorithmSpec extends ScalaTest { "StringAlgorithm standalone object" should provide { +final class AlgorithmSpec extends ScalaTest { import com.rockymadden.stringmetric.phonetic._ import com.rockymadden.stringmetric.Algorithm._ + import com.rockymadden.stringmetric.Transform.StringTransform - "compute method and companion object pass through" in { - StringAlgorithm.computeWithMetaphone("testone").get should - equal (MetaphoneAlgorithm.compute("testone".toCharArray).get) - StringAlgorithm.computeWithNysiis("testone").get should - equal (NysiisAlgorithm.compute("testone".toCharArray).get) - StringAlgorithm.computeWithRefinedNysiis("testone").get should - equal (RefinedNysiisAlgorithm.compute("testone".toCharArray).get) - StringAlgorithm.computeWithRefinedSoundex("testone").get should - equal (RefinedSoundexAlgorithm.compute("testone".toCharArray).get) - StringAlgorithm.computeWithSoundex("testone").get should - equal (SoundexAlgorithm.compute("testone".toCharArray).get) + "StringAlgorithm" should provide { + "compute method and companion object pass through" in { + StringAlgorithm.computeWithMetaphone("testone").get should + equal (MetaphoneAlgorithm.compute("testone".toCharArray).get) + StringAlgorithm.computeWithNysiis("testone").get should + equal (NysiisAlgorithm.compute("testone".toCharArray).get) + StringAlgorithm.computeWithRefinedNysiis("testone").get should + equal (RefinedNysiisAlgorithm.compute("testone".toCharArray).get) + StringAlgorithm.computeWithRefinedSoundex("testone").get should + equal (RefinedSoundexAlgorithm.compute("testone".toCharArray).get) + StringAlgorithm.computeWithSoundex("testone").get should + equal (SoundexAlgorithm.compute("testone".toCharArray).get) + } } -}} + + "StringAlgorithmDecorator" should provide { + "withTransform()" in { + (MetaphoneAlgorithm withTransform StringTransform.filterAlpha).compute("abc123").get should + equal (MetaphoneAlgorithm.compute("abc").get) + } + } +} diff --git a/core/source/test/scala/com/rockymadden/stringmetric/MetricSpec.scala b/core/source/test/scala/com/rockymadden/stringmetric/MetricSpec.scala index 48994e9..4803869 100644 --- a/core/source/test/scala/com/rockymadden/stringmetric/MetricSpec.scala +++ b/core/source/test/scala/com/rockymadden/stringmetric/MetricSpec.scala @@ -4,39 +4,51 @@ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) -final class MetricSpec extends ScalaTest { "StringMetric standalone object" should provide { +final class MetricSpec extends ScalaTest { import com.rockymadden.stringmetric.phonetic._ import com.rockymadden.stringmetric.similarity._ import com.rockymadden.stringmetric.Metric._ + import com.rockymadden.stringmetric.Transform.StringTransform - "compare method and companion object pass through" in { - StringMetric.compareWithDiceSorensen(1)("testone", "testtwo").get should - equal (DiceSorensenMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithHamming("testone", "testtwo").get should - equal (HammingMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithJaccard(1)("testone", "testtwo").get should - equal (JaccardMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithJaro("testone", "testtwo").get should - equal (JaroMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithJaroWinkler("testone", "testtwo").get should - equal (JaroWinklerMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithLevenshtein("testone", "testtwo").get should - equal (LevenshteinMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithMetaphone("testone", "testtwo").get should - equal (MetaphoneMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithNGram(1)("testone", "testtwo").get should - equal (NGramMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithNysiis("testone", "testtwo").get should - equal (NysiisMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithOverlap(1)("testone", "testtwo").get should - equal (OverlapMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithRefinedNysiis("testone", "testtwo").get should - equal (RefinedNysiisMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithRefinedSoundex("testone", "testtwo").get should - equal (RefinedSoundexMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithSoundex("testone", "testtwo").get should - equal (SoundexMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) - StringMetric.compareWithWeightedLevenshtein(1, 2, 3)("testone", "testtwo").get should - equal (WeightedLevenshteinMetric(1, 2, 3).compare("testone".toCharArray, "testtwo".toCharArray).get) + "StringMetric standalone object" should provide { + "compare method and companion object pass through" in { + StringMetric.compareWithDiceSorensen(1)("testone", "testtwo").get should + equal (DiceSorensenMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithHamming("testone", "testtwo").get should + equal (HammingMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithJaccard(1)("testone", "testtwo").get should + equal (JaccardMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithJaro("testone", "testtwo").get should + equal (JaroMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithJaroWinkler("testone", "testtwo").get should + equal (JaroWinklerMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithLevenshtein("testone", "testtwo").get should + equal (LevenshteinMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithMetaphone("testone", "testtwo").get should + equal (MetaphoneMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithNGram(1)("testone", "testtwo").get should + equal (NGramMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithNysiis("testone", "testtwo").get should + equal (NysiisMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithOverlap(1)("testone", "testtwo").get should + equal (OverlapMetric(1).compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithRefinedNysiis("testone", "testtwo").get should + equal (RefinedNysiisMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithRefinedSoundex("testone", "testtwo").get should + equal (RefinedSoundexMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithSoundex("testone", "testtwo").get should + equal (SoundexMetric.compare("testone".toCharArray, "testtwo".toCharArray).get) + StringMetric.compareWithWeightedLevenshtein(1, 2, 3)("testone", "testtwo").get should + equal (WeightedLevenshteinMetric(1, 2, 3).compare("testone".toCharArray, "testtwo".toCharArray).get) + } } -}} + + "StringMetricDecorator" should provide { + "withTransform()" in { + (MetaphoneMetric withTransform StringTransform.filterAlpha).compare("abc123", "abc456").get should + equal (true) + (DiceSorensenMetric(1) withTransform StringTransform.filterAlpha).compare("abc123", "abc456").get should + equal (1.0) + } + } +} |