summaryrefslogtreecommitdiff
path: root/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala
diff options
context:
space:
mode:
Diffstat (limited to 'core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala')
-rwxr-xr-xcore/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala33
1 files changed, 33 insertions, 0 deletions
diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala
new file mode 100755
index 0000000..e4daa17
--- /dev/null
+++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala
@@ -0,0 +1,33 @@
+package com.rockymadden.stringmetric.phonetic
+
+import com.rockymadden.stringmetric.{StringFilter, StringMetric}
+import com.rockymadden.stringmetric.Alphabet.Alpha
+
+/** An implementation of the Soundex metric. */
+class SoundexMetric extends StringMetric[DummyImplicit, Boolean] { this: StringFilter =>
+ final override def compare(charArray1: Array[Char], charArray2: Array[Char])
+ (implicit di: DummyImplicit): Option[Boolean] = {
+
+ val fca1 = filter(charArray1)
+ lazy val fca2 = filter(charArray2)
+
+ if (fca1.length == 0 || !(Alpha isSuperset fca1.head) || fca2.length == 0 || !(Alpha isSuperset fca2.head)) None
+ else if (fca1.head.toLower != fca2.head.toLower) Some(false)
+ else SoundexAlgorithm.compute(fca1).filter(_.length > 0).flatMap(se1 =>
+ SoundexAlgorithm.compute(fca2).filter(_.length > 0).map(se1.sameElements(_))
+ )
+ }
+
+ final override def compare(string1: String, string2: String)(implicit di: DummyImplicit): Option[Boolean] =
+ compare(string1.toCharArray, string2.toCharArray)
+}
+
+object SoundexMetric {
+ private lazy val self = apply()
+
+ def apply(): SoundexMetric = new SoundexMetric with StringFilter
+
+ def compare(charArray1: Array[Char], charArray2: Array[Char]) = self.compare(charArray1, charArray2)
+
+ def compare(string1: String, string2: String) = self.compare(string1, string2)
+}