diff options
author | Rocky Madden <git@rockymadden.com> | 2013-02-01 19:35:22 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-02-01 19:35:22 -0700 |
commit | a759ce9693d87dbe54dc975fa2fb68de0252f21f (patch) | |
tree | 572005f1e222a9287a17807001d373cb50988113 /core | |
parent | 0651046caa4d9aa85161fc925935ed527b1dc8fd (diff) | |
download | stringmetric-a759ce9693d87dbe54dc975fa2fb68de0252f21f.tar.gz stringmetric-a759ce9693d87dbe54dc975fa2fb68de0252f21f.tar.bz2 stringmetric-a759ce9693d87dbe54dc975fa2fb68de0252f21f.zip |
Refactored Alphabet to be more flexible and usable.
Diffstat (limited to 'core')
12 files changed, 110 insertions, 82 deletions
diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala index 9aacdba..0d6726a 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala @@ -1,24 +1,36 @@ package com.rockymadden.stringmetric.phonetic -object Alphabet { - def is(char: Char): Boolean = (char >= 65 && char <= 90) || (char >= 97 && char <= 122) +import scala.language.implicitConversions - def is(charArray: Array[Char]): Boolean = charArray.length > 0 && !charArray.find(!is(_)).isDefined +private[phonetic] final class Alphabet private(private[this] val self: Array[Char]) { + def is(set: Set[Char]): Boolean = self.length > 0 && self.takeWhile(set.contains(_)).length == self.length - def is(string: String): Boolean = is(string.toCharArray) + def startsWith(set: Set[Char]): Boolean = self.length > 0 && set.contains(self.head) +} + +private[phonetic] object Alphabet { + final val LowercaseConsonant = Set('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x' ,'z') + final val UppercaseConsonant = Set('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X' ,'Z') + final val Consonant = LowercaseConsonant ++ UppercaseConsonant + final val LowercaseVowel = Set('a', 'e', 'i', 'o', 'u') + final val UppercaseVowel = Set('A', 'E', 'I', 'O', 'U') + final val Vowel = LowercaseVowel ++ UppercaseVowel + final val LowercaseY = Set('y') + final val UppercaseY = Set('Y') + final val Y = LowercaseY ++ UppercaseY + final val LowercaseAlpha = LowercaseConsonant ++ LowercaseVowel ++ LowercaseY + final val UppercaseAlpha = UppercaseConsonant ++ UppercaseVowel ++ UppercaseY + final val Alpha = LowercaseAlpha ++ UppercaseAlpha + + implicit def CharToAlphabet(char: Char): Alphabet = apply(char) + + implicit def CharArrayToAlphabet(charArray: Array[Char]): Alphabet = apply(charArray) - def isSometimesVowel(char: Char): Boolean = char == 'y' || char == 'Y' || isVowel(char) + implicit def CharToAlphabet(string: String): Alphabet = apply(string) - def isVowel(char: Char): Boolean = ( - char == 'a' || char == 'e' || char == 'i' || char == 'o' || char =='u' - || char == 'A' || char == 'E' || char == 'I' || char == 'O' || char =='U' - ) + def apply(char: Char): Alphabet = new Alphabet(Array(char)) - def startsWith(charArray: Array[Char]): Boolean = - if (charArray.length == 0) false - else is(charArray.head) + def apply(charArray: Array[Char]): Alphabet = new Alphabet(charArray) - def startsWith(string: String): Boolean = - if (string.length == 0) false - else is(string.charAt(0)) + def apply(string: String): Alphabet = new Alphabet(string.toCharArray) } diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala index d9eb167..a198890 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringAlgorithm, StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet._ import scala.annotation.{ switch, tailrec } /** An implementation of the Metaphone [[com.rockymadden.stringmetric.StringAlgorithm]]. */ @@ -10,7 +11,7 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val fca = stringFilter.filter(charArray) - if (fca.length == 0 || !Alphabet.is(fca.head)) None + if (fca.length == 0 || !(fca.head is Alpha)) None else { val th = deduplicate(transcodeHead(fca.map(_.toLower))) val t = transcode(Array.empty[Char], th.head, th.tail, Array.empty[Char]) @@ -66,7 +67,7 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm else if (r.length >= 1 && (r.head == 'i' || r.head == 'e' || r.head == 'y')) shift(2, o :+ 'j') else shift(1, o :+ 'k') case 'h' => - if ((l.length >= 1 && Alphabet.isVowel(l.last) && (r.length == 0 || !Alphabet.isVowel(r.head))) + if ((l.length >= 1 && (l.last is LowercaseVowel) && (r.length == 0 || !(r.head is LowercaseVowel))) || (l.length >= 2 && l.last == 'h' && (l(l.length - 2) == 'c' || l(l.length - 2) == 's' || l(l.length - 2) == 'p' || l(l.length - 2) == 't' || l(l.length - 2) == 'g'))) shift(1, o) @@ -84,7 +85,7 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm else if (r.length >= 2 && r.head == 'c' && r(1) == 'h') shift(1, o) else shift(1, o :+ 't') case 'v' => shift(1, o :+ 'f') - case 'w' | 'y' => if (r.length == 0 || !Alphabet.isVowel(r.head)) shift(1, o) else shift(1, o :+ c) + case 'w' | 'y' => if (r.length == 0 || !(r.head is LowercaseVowel)) shift(1, o) else shift(1, o :+ c) case 'x' => shift(1, (o :+ 'k') :+ 's') case 'z' => shift(1, o :+ 's') case _ => shift(1, o) @@ -112,4 +113,4 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm } } } -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala index 58ba16c..a8c0a77 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringMetric, StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet._ /** An implementation of the Metaphone [[com.rockymadden.stringmetric.StringMetric]]. */ object MetaphoneMetric extends StringMetric with FilterableStringMetric { @@ -12,7 +13,7 @@ object MetaphoneMetric extends StringMetric with FilterableStringMetric { val fca1 = stringFilter.filter(charArray1) lazy val fca2 = stringFilter.filter(charArray2) - if (fca1.length == 0 || !Alphabet.is(fca1.head) || fca2.length == 0 || !Alphabet.is(fca2.head)) None + if (fca1.length == 0 || !(fca1.head is Alpha) || fca2.length == 0 || !(fca2.head is Alpha)) None else MetaphoneAlgorithm.compute(fca1).filter(_.length > 0).flatMap(mp1 => MetaphoneAlgorithm.compute(fca2).filter(_.length > 0).map(mp1.sameElements(_)) ) @@ -25,4 +26,4 @@ object MetaphoneMetric extends StringMetric with FilterableStringMetric { stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) ) -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala index e375f28..9fd25ab 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringAlgorithm, StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet._ import scala.annotation.{ switch, tailrec } /** An implementation of the NYSIIS [[com.rockymadden.stringmetric.StringAlgorithm]]. */ @@ -10,7 +11,7 @@ object NysiisAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val fca = stringFilter.filter(charArray) - if (fca.length == 0 || !Alphabet.is(fca.head)) None + if (fca.length == 0 || !(fca.head is Alpha)) None else { val tr = transcodeRight(fca.map(_.toLower)) val tl = transcodeLeft(tr._1) @@ -63,7 +64,7 @@ object NysiisAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { if (r.length >= 1 && r.head == 'v') shift(2, o ++ Array('a', 'f')) else shift(1, o :+ 'a') case 'h' => - if (l.length >= 1 && (!Alphabet.isVowel(l.last) || (r.length >= 1 && !Alphabet.isVowel(r.head)))) shift(1, o) + if (l.length >= 1 && (!(l.last is LowercaseVowel) || (r.length >= 1 && !(r.head is LowercaseVowel)))) shift(1, o) else shift(1, o :+ c) case 'k' => if (r.length >= 1 && r.head == 'n') shift(2, o :+ 'n') else shift(1, o :+ 'c') case 'm' => shift(1, o :+ 'n') @@ -73,7 +74,7 @@ object NysiisAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { if (r.length >= 2 && r.head == 'c' && r(1) == 'h') shift(3, o :+ c) else shift(1, o :+ c) case 'w' => - if (l.length >= 1 && Alphabet.isVowel(l.last)) shift(1, o) + if (l.length >= 1 && (l.last is LowercaseVowel)) shift(1, o) else shift(1, o :+ c) case 'z' => shift(1, o :+ 's') case _ => shift(1, o) @@ -115,4 +116,4 @@ object NysiisAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { } } else (ca, Array.empty[Char]) } -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala index af523dd..9edaadd 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringMetric, StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet._ /** An implementation of the NYSIIS [[com.rockymadden.stringmetric.StringMetric]]. */ object NysiisMetric extends StringMetric with FilterableStringMetric { @@ -19,7 +20,7 @@ object NysiisMetric extends StringMetric with FilterableStringMetric { val fca1 = stringFilter.filter(charArray1) lazy val fca2 = stringFilter.filter(charArray2) - if (fca1.length == 0 || !Alphabet.is(fca1.head) || fca2.length == 0 || !Alphabet.is(fca2.head)) None + if (fca1.length == 0 || !(fca1.head is Alpha) || fca2.length == 0 || !(fca2.head is Alpha)) None else if (unequal(fca1.head, fca2.head)) Some(false) else NysiisAlgorithm.compute(fca1).filter(_.length > 0).flatMap(ny1 => NysiisAlgorithm.compute(fca2).filter(_.length > 0).map(ny1.sameElements(_)) @@ -33,4 +34,4 @@ object NysiisMetric extends StringMetric with FilterableStringMetric { stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) ) -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala index 6fca0eb..8f92768 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringAlgorithm, StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet._ import scala.annotation.{ switch, tailrec } /** An implementation of the refined NYSIIS [[com.rockymadden.stringmetric.StringAlgorithm]]. */ @@ -10,7 +11,7 @@ object RefinedNysiisAlgorithm extends StringAlgorithm with FilterableStringAlgor override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val fca = stringFilter.filter(charArray) - if (fca.length == 0 || !Alphabet.is(fca.head)) None + if (fca.length == 0 || !(fca.head is Alpha)) None else { val lfca = fca.map(_.toLower) val tlh = transcodeLast(transcodeHead(lfca.head +: cleanLast(lfca.tail, Set('s', 'z')))) @@ -69,7 +70,7 @@ object RefinedNysiisAlgorithm extends StringAlgorithm with FilterableStringAlgor else shift(1, o :+ c) case 'h' => if (l.length == 0) shift(1, o :+ c) - else if (!Alphabet.isVowel(l.last) || (r.length >= 1 && !Alphabet.isVowel(r.head))) shift(1, o) + else if (!(l.last is LowercaseVowel) || (r.length >= 1 && !(r.head is LowercaseVowel))) shift(1, o) else shift(1, o :+ c) case 'k' => if (r.length >= 1 && r.head == 'n') shift(2, o :+ 'n') else shift(1, o :+ 'c') case 'm' => if (l.length == 0) shift(1, o :+ c) else shift(1, o :+ 'n') @@ -80,7 +81,7 @@ object RefinedNysiisAlgorithm extends StringAlgorithm with FilterableStringAlgor else if (r.length >= 1 && r.head == 'h') shift(2, o :+ c) else shift(1, o :+ c) case 'w' => - if (l.length >= 1 && Alphabet.isVowel(l.last)) shift(1, o) + if (l.length >= 1 && (l.last is LowercaseVowel)) shift(1, o) else if (r.length >= 1 && r.head == 'r') shift(2, o :+ 'r') else shift(1, o :+ c) case 'y' => @@ -123,4 +124,4 @@ object RefinedNysiisAlgorithm extends StringAlgorithm with FilterableStringAlgor } } else ca } -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala index 3b318b2..8efa441 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringMetric, StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet._ /** An implementation of the refined NYSIIS [[com.rockymadden.stringmetric.StringMetric]]. */ object RefinedNysiisMetric extends StringMetric with FilterableStringMetric { @@ -19,7 +20,7 @@ object RefinedNysiisMetric extends StringMetric with FilterableStringMetric { val fca1 = stringFilter.filter(charArray1) lazy val fca2 = stringFilter.filter(charArray2) - if (fca1.length == 0 || !Alphabet.is(fca1.head) || fca2.length == 0 || !Alphabet.is(fca2.head)) None + if (fca1.length == 0 || !(fca1.head is Alpha) || fca2.length == 0 || !(fca2.head is Alpha)) None else if (unequal(fca1.head, fca2.head)) Some(false) else RefinedNysiisAlgorithm.compute(fca1).filter(_.length > 0).flatMap(rny1 => RefinedNysiisAlgorithm.compute(fca2).filter(_.length > 0).map(rny1.sameElements(_)) @@ -33,4 +34,4 @@ object RefinedNysiisMetric extends StringMetric with FilterableStringMetric { stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) ) -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala index f2c765b..ffb6fc6 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringAlgorithm, StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet._ import scala.annotation.{ switch, tailrec } /** An implementation of the refined Soundex [[com.rockymadden.stringmetric.StringAlgorithm]]. */ @@ -10,7 +11,7 @@ object RefinedSoundexAlgorithm extends StringAlgorithm with FilterableStringAlgo override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val fca = stringFilter.filter(charArray) - if (fca.length == 0 || !Alphabet.is(fca.head)) None + if (fca.length == 0 || !(fca.head is Alpha)) None else Some(transcode(fca, Array(fca.head.toLower))) } @@ -63,4 +64,4 @@ object RefinedSoundexAlgorithm extends StringAlgorithm with FilterableStringAlgo transcode(i.tail, if (a != '\0') o :+ a else o) } } -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala index 6208f9a..b576480 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringMetric, StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet._ /** An implementation of the refined Soundex [[com.rockymadden.stringmetric.StringMetric]]. */ object RefinedSoundexMetric extends StringMetric with FilterableStringMetric { @@ -12,7 +13,7 @@ object RefinedSoundexMetric extends StringMetric with FilterableStringMetric { val fca1 = stringFilter.filter(charArray1) lazy val fca2 = stringFilter.filter(charArray2) - if (fca1.length == 0 || !Alphabet.is(fca1.head) || fca2.length == 0 || !Alphabet.is(fca2.head)) None + 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 RefinedSoundexAlgorithm.compute(fca1).filter(_.length > 0).flatMap(rse1 => RefinedSoundexAlgorithm.compute(fca2).filter(_.length > 0).map(rse1.sameElements(_)) @@ -26,4 +27,4 @@ object RefinedSoundexMetric extends StringMetric with FilterableStringMetric { stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) ) -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala index cb8b246..10519f6 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ FilterableStringAlgorithm, StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet._ import scala.annotation.{ switch, tailrec } /** An implementation of the Soundex [[com.rockymadden.stringmetric.StringAlgorithm]]. */ @@ -10,7 +11,7 @@ object SoundexAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val fca = stringFilter.filter(charArray) - if (fca.length == 0 || !Alphabet.is(fca.head)) None + if (fca.length == 0 || !(fca.head is Alpha)) None else { val fc = fca.head.toLower @@ -61,4 +62,4 @@ object SoundexAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { else transcode(i.tail, c, if (a != '\0') o :+ a else o) } } -}
\ No newline at end of file +} diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala index 35eedd4..694762a 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala @@ -1,6 +1,7 @@ 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 { @@ -12,7 +13,7 @@ object SoundexMetric extends StringMetric with FilterableStringMetric { val fca1 = stringFilter.filter(charArray1) lazy val fca2 = stringFilter.filter(charArray2) - if (fca1.length == 0 || !Alphabet.is(fca1.head) || fca2.length == 0 || !Alphabet.is(fca2.head)) None + 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(_)) @@ -26,4 +27,4 @@ object SoundexMetric extends StringMetric with FilterableStringMetric { stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) ) -}
\ No newline at end of file +} diff --git a/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala b/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala index dc0e63b..5d41633 100755 --- a/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala +++ b/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.ScalaTest +import com.rockymadden.stringmetric.phonetic.Alphabet._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner @@ -10,116 +11,121 @@ final class AlphabetSpec extends ScalaTest { "an overloaded is method which accepts a Char" when passed { "non-alphabet argument" should returns { "false" in { - Alphabet.is('0') should be (false) + '0' is Alpha should be (false) } } "alphabet argument" should returns { "true" in { - Alphabet.is('a') should be (true) - Alphabet.is('A') should be (true) + 'a' is Alpha should be (true) + 'A' is Alpha should be (true) } } - } - "an overloaded is method which accepts an Array[Char]" when passed { - "empty argument" should returns { - "false" in { - Alphabet.is(Array.empty[Char]) should be (false) - } - } - "non-alphabet argument" should returns { + "non-vowel argument" should returns { "false" in { - Alphabet.is("hi!".toCharArray) should be (false) - Alphabet.is("helloworld!".toCharArray) should be (false) + 'y' is Vowel should be (false) } } - "alphabet argument" should returns { + "vowel argument" should returns { "true" in { - Alphabet.is("hi".toCharArray) should be (true) - Alphabet.is("helloworld".toCharArray) should be (true) - Alphabet.is("HI".toCharArray) should be (true) - Alphabet.is("HELLOWORLD".toCharArray) should be (true) + 'a' is Vowel should be (true) + 'A' is Vowel should be (true) } } } - "an overloaded is method which accepts a String" when passed { + "an overloaded is method which accepts an Array[Char]" when passed { "empty argument" should returns { "false" in { - Alphabet.is("") should be (false) + Array.empty[Char] is Alpha should be (false) } } "non-alphabet argument" should returns { "false" in { - Alphabet.is("helloworld!") should be (false) + "hi!".toCharArray is Alpha should be (false) + "helloworld!".toCharArray is Alpha should be (false) } } "alphabet argument" should returns { "true" in { - Alphabet.is("helloworld") should be (true) - Alphabet.is("HELLOWORLD") should be (true) + "hi".toCharArray is Alpha should be (true) + "helloworld".toCharArray is Alpha should be (true) + "HI".toCharArray is Alpha should be (true) + "HELLOWORLD".toCharArray is Alpha should be (true) } } - } - "isSometimesVowel method" when passed { "non-vowel argument" should returns { "false" in { - Alphabet.isSometimesVowel('z') should be (false) + "y".toCharArray is Vowel should be (false) } } "vowel argument" should returns { "true" in { - Alphabet.isSometimesVowel('a') should be (true) - Alphabet.isSometimesVowel('A') should be (true) - Alphabet.isSometimesVowel('y') should be (true) - Alphabet.isSometimesVowel('Y') should be (true) + "a".toCharArray is Vowel should be (true) + "A".toCharArray is Vowel should be (true) } } } - "isVowel method" when passed { + "an overloaded is method which accepts a String" when passed { + "empty argument" should returns { + "false" in { + "" is Alpha should be (false) + } + } + "non-alphabet argument" should returns { + "false" in { + "helloworld!" is Alpha should be (false) + } + } + "alphabet argument" should returns { + "true" in { + "helloworld" is Alpha should be (true) + "HELLOWORLD" is Alpha should be (true) + } + } "non-vowel argument" should returns { "false" in { - Alphabet.isVowel('y') should be (false) + "y" is Vowel should be (false) } } "vowel argument" should returns { "true" in { - Alphabet.isVowel('a') should be (true) - Alphabet.isVowel('A') should be (true) + "a" is Vowel should be (true) + "A" is Vowel should be (true) } } } "an overloaded startsWith method which accepts an Array[Char]" when passed { "empty argument" should returns { "false" in { - Alphabet.startsWith(Array.empty[Char]) should be (false) + Array.empty[Char] is Alpha should be (false) } } "non-alphabet argument" should returns { "false" in { - Alphabet.startsWith("1abc".toCharArray) should be (false) + "1abc".toCharArray is Alpha should be (false) } } "alphabet argument" should returns { "true" in { - Alphabet.startsWith("abc".toCharArray) should be (true) + "abc".toCharArray is Alpha should be (true) } } } "an overloaded startsWith method which accepts a String" when passed { "empty argument" should returns { "false" in { - Alphabet.startsWith("") should be (false) + "" is Alpha should be (false) } } "non-alphabet argument" should returns { "false" in { - Alphabet.startsWith("1abc") should be (false) + "1abc" is Alpha should be (false) } } "alphabet argument" should returns { "true" in { - Alphabet.startsWith("abc") should be (true) + "abc" is Alpha should be (true) } } } } -}
\ No newline at end of file +} |