diff options
author | Rocky Madden <git@rockymadden.com> | 2012-11-20 11:21:01 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-11-20 11:21:01 -0700 |
commit | c20f08049db4ef5f5fc8d3ab04c42fa8ab5375be (patch) | |
tree | c8cb6a0d5946a21ec4487f1a9dc46a680ad6ae79 | |
parent | bf518921004cc4c0ee23fd2bcdbaeb59286e1570 (diff) | |
download | stringmetric-c20f08049db4ef5f5fc8d3ab04c42fa8ab5375be.tar.gz stringmetric-c20f08049db4ef5f5fc8d3ab04c42fa8ab5375be.tar.bz2 stringmetric-c20f08049db4ef5f5fc8d3ab04c42fa8ab5375be.zip |
Removed nested if statement.
5 files changed, 27 insertions, 43 deletions
diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneAlgorithm.scala index 5f67d8e..a4fc811 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneAlgorithm.scala @@ -11,15 +11,12 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val ca = stringFilter.filter(charArray) - if (ca.length == 0) None + if (ca.length == 0 || !Alphabet.is(ca.head)) None else { - if (!Alphabet.is(ca.head)) None - else { - val th = deduplicate(transcodeHead(ca.map(_.toLower))) - val t = transcode(Array.empty[Char], th.head, th.tail, Array.empty[Char]) + val th = deduplicate(transcodeHead(ca.map(_.toLower))) + val t = transcode(Array.empty[Char], th.head, th.tail, Array.empty[Char]) - if (t.length == 0) None else Some(t) // Single Y or W would have 0 length. - } + if (t.length == 0) None else Some(t) // Single Y or W would have 0 length. } } @@ -28,8 +25,7 @@ object MetaphoneAlgorithm extends StringAlgorithm with FilterableStringAlgorithm private[this] def deduplicate(ca: Array[Char]) = if (ca.length <= 1) ca - else - ca.sliding(2).withFilter(a => a(0) == 'c' || a(0) != a(1)).map(a => a(0)).toArray[Char] :+ ca.last + else ca.sliding(2).withFilter(a => a(0) == 'c' || a(0) != a(1)).map(a => a(0)).toArray[Char] :+ ca.last @tailrec private[this] def transcode(l: Array[Char], c: Char, r: Array[Char], o: Array[Char]): Array[Char] = { diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisAlgorithm.scala index 13adc1c..33abe1d 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisAlgorithm.scala @@ -11,20 +11,17 @@ object NysiisAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val ca = stringFilter.filter(charArray) - if (ca.length == 0) None + if (ca.length == 0 || !Alphabet.is(ca.head)) None else { - if (!Alphabet.is(ca.head)) None - else { - val tr = transcodeRight(ca.map(_.toLower)) - val tl = transcodeLeft(tr._1) - val t = - if (tl._2.length == 0) tl._1 ++ tr._2 - else - tl._1 ++ transcodeCenter(Array.empty[Char], tl._2.head, if (tl._2.length > 1) tl._2.tail else Array.empty[Char], Array.empty[Char]) ++ tr._2 - - if (t.length == 1) Some(t) - else Some(t.head +: deduplicate(cleanTerminal(cleanLast(t.tail)))) - } + val tr = transcodeRight(ca.map(_.toLower)) + val tl = transcodeLeft(tr._1) + val t = + if (tl._2.length == 0) tl._1 ++ tr._2 + else + tl._1 ++ transcodeCenter(Array.empty[Char], tl._2.head, if (tl._2.length > 1) tl._2.tail else Array.empty[Char], Array.empty[Char]) ++ tr._2 + + if (t.length == 1) Some(t) + else Some(t.head +: deduplicate(cleanTerminal(cleanLast(t.tail)))) } } diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedNysiisAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedNysiisAlgorithm.scala index 3139583..d3870b2 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedNysiisAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedNysiisAlgorithm.scala @@ -11,17 +11,14 @@ object RefinedNysiisAlgorithm extends StringAlgorithm with FilterableStringAlgor override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val ca = stringFilter.filter(charArray) - if (ca.length == 0) None + if (ca.length == 0 || !Alphabet.is(ca.head)) None else { - if (!Alphabet.is(ca.head)) None - else { - val cal = ca.map(_.toLower) - val thl = transcodeLast(transcodeHead(cal.head +: cleanLast(cal.tail, Set('s', 'z')))) - val t = transcode(Array.empty[Char], thl.head, thl.tail, Array.empty[Char]) + val cal = ca.map(_.toLower) + val thl = transcodeLast(transcodeHead(cal.head +: cleanLast(cal.tail, Set('s', 'z')))) + val t = transcode(Array.empty[Char], thl.head, thl.tail, Array.empty[Char]) - if (t.length == 1) Some(t) - else Some(deduplicate(t.head +: cleanTerminal(cleanLast(t.tail, Set('a'))))) - } + if (t.length == 1) Some(t) + else Some(deduplicate(t.head +: cleanTerminal(cleanLast(t.tail, Set('a'))))) } } diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedSoundexAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedSoundexAlgorithm.scala index 693941c..878cf5c 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedSoundexAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/RefinedSoundexAlgorithm.scala @@ -11,14 +11,11 @@ object RefinedSoundexAlgorithm extends StringAlgorithm with FilterableStringAlgo override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val ca = stringFilter.filter(charArray) - if (ca.length == 0) None + if (ca.length == 0 || !Alphabet.is(ca.head)) None else { - if (!Alphabet.is(ca.head)) None - else { - val fc = ca.head.toLower + val fc = ca.head.toLower - Some(transcode(ca, fc, Array(fc))) - } + Some(transcode(ca, fc, Array(fc))) } } diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexAlgorithm.scala index bd96c52..0b955bb 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexAlgorithm.scala @@ -11,14 +11,11 @@ object SoundexAlgorithm extends StringAlgorithm with FilterableStringAlgorithm { override def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] = { val ca = stringFilter.filter(charArray) - if (ca.length == 0) None + if (ca.length == 0 || !Alphabet.is(ca.head)) None else { - if (!Alphabet.is(ca.head)) None - else { - val fc = ca.head.toLower + val fc = ca.head.toLower - Some(transcode(ca.tail, fc, Array(fc)).padTo(4, '0')) - } + Some(transcode(ca.tail, fc, Array(fc)).padTo(4, '0')) } } |