blob: 4ead1f5fb885a49313da44cba0d0891923d9a558 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.rockymadden.stringmetric.phonetic
import com.rockymadden.stringmetric.{ FilterableStringMetric, StringFilter, StringMetric }
import com.rockymadden.stringmetric.phonetic.Alphabet._
/** An implementation of the Soundex [[com.rockymadden.stringmetric.StringMetric]]. */
object SoundexMetric extends StringMetric with FilterableStringMetric {
type CompareReturn = Boolean
override def compare(charArray1: Array[Char], charArray2: Array[Char])
(implicit stringFilter: StringFilter): Option[CompareReturn] = {
val fca1 = stringFilter.filter(charArray1)
lazy val fca2 = stringFilter.filter(charArray2)
if (fca1.length == 0 || !(fca1.head is Alpha) || fca2.length == 0 || !(fca2.head is Alpha)) 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(_)))
}
override def compare(string1: String, string2: String)
(implicit stringFilter: StringFilter): Option[CompareReturn] =
compare(stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray))
}
|