diff options
author | Rocky Madden <git@rockymadden.com> | 2012-10-28 18:07:15 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-10-28 18:07:15 -0600 |
commit | 3a01694c107081fab350ad837e57177e1580a955 (patch) | |
tree | 2387b906c2df4f17000bcc589b3c379923c5e74e /core | |
parent | 434df013728807dad4967391bec128f78ad25194 (diff) | |
download | stringmetric-3a01694c107081fab350ad837e57177e1580a955.tar.gz stringmetric-3a01694c107081fab350ad837e57177e1580a955.tar.bz2 stringmetric-3a01694c107081fab350ad837e57177e1580a955.zip |
Code formatting tweaks.
Diffstat (limited to 'core')
13 files changed, 31 insertions, 63 deletions
diff --git a/core/source/core/scala/org/hashtree/stringmetric/StringAlgorithm.scala b/core/source/core/scala/org/hashtree/stringmetric/StringAlgorithm.scala index e85f83e..c6bb499 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/StringAlgorithm.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/StringAlgorithm.scala @@ -2,5 +2,5 @@ package org.hashtree.stringmetric /** Marks those which leverage traits of a string based [[org.hashtree.stringmetric.Algorithm]]. */ trait StringAlgorithm extends Algorithm[String, StringFilter] { - def compute(ca: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] + def compute(charArray: Array[Char])(implicit stringFilter: StringFilter): Option[Array[Char]] }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/StringMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/StringMetric.scala index 57add60..5d4d68b 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/StringMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/StringMetric.scala @@ -2,5 +2,5 @@ package org.hashtree.stringmetric /** Marks those which leverage traits of a string based [[org.hashtree.stringmetric.Metric]]. */ trait StringMetric extends Metric[String, StringFilter] { - def compare(ca1: Array[Char], ca2: Array[Char])(implicit stringFilter: StringFilter): Option[AnyVal] + def compare(charArray1: Array[Char], charArray2: Array[Char])(implicit stringFilter: StringFilter): Option[AnyVal] }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Metaphone.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Metaphone.scala index 5560158..557829f 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Metaphone.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Metaphone.scala @@ -21,18 +21,16 @@ object Metaphone extends StringAlgorithm { } } - override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = { + override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = compute(stringFilter.filter(string.toCharArray))(new StringFilterDelegate) match { case Some(mp) => Some(mp.mkString) case None => None } - } - private[this] def deduplicate(ca: Array[Char]) = { + private[this] def deduplicate(ca: Array[Char]) = if (ca.length <= 1) ca else ca.sliding(2).filter(a => a(0) == 'c' || a(0) != a(1)).map(a => a(0)).toArray[Char] :+ ca.last - } private[this] def isVowel(c: Char) = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') @@ -86,8 +84,7 @@ object Metaphone extends StringAlgorithm { shift(1, o :+ 't') } case 'g' => { - if ( - (r.length > 1 && r.head == 'h') || + if ((r.length > 1 && r.head == 'h') || (r.length == 1 && r.head == 'n') || (r.length == 3 && r.head == 'n' && r(1) == 'e' && r(2) == 'd') ) @@ -98,8 +95,7 @@ object Metaphone extends StringAlgorithm { shift(1, o :+ 'k') } case 'h' => { - if ( - (l.length >= 1 && isVowel(l.last) && (r.length == 0 || !isVowel(r.head))) || + if ((l.length >= 1 && isVowel(l.last) && (r.length == 0 || !isVowel(r.head))) || (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' @@ -146,8 +142,7 @@ object Metaphone extends StringAlgorithm { private[this] def transcodeHead(ca: Array[Char]) = { val h = ca.take(2).padTo(2, '\0') - if ( - (h.head == 'a' && h.last == 'e') || + if ((h.head == 'a' && h.last == 'e') || (h.last == 'n' && (h.head == 'g' || h.head == 'k' || h.head == 'p')) || (h.head == 'w' && h.last == 'r') ) @@ -156,7 +151,6 @@ object Metaphone extends StringAlgorithm { 'w' +: ca.drop(2) else if (h.head == 'x') 's' +: ca.tail - else - ca + else ca } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneMetric.scala index 895c1a5..73f48af 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/MetaphoneMetric.scala @@ -13,17 +13,14 @@ object MetaphoneMetric extends StringMetric { val mp1 = Metaphone.compute(ca1) val mp2 = Metaphone.compute(ca2) - if (!mp1.isDefined || !mp2.isDefined || (mp1.get.length == 0 && mp2.get.length == 0)) - None - else - Some(mp1.get.sameElements(mp2.get)) + if (!mp1.isDefined || !mp2.isDefined || (mp1.get.length == 0 && mp2.get.length == 0)) None + else Some(mp1.get.sameElements(mp2.get)) } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Nysiis.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Nysiis.scala index 90267ee..6718fb2 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Nysiis.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Nysiis.scala @@ -16,7 +16,6 @@ object Nysiis extends StringAlgorithm { else { if (thl.length == 1) Some(thl) else { - val ts = thl.splitAt(1) val t = transcode(ts._1, ts._2.head, ts._2.tail, ts._1) @@ -26,18 +25,16 @@ object Nysiis extends StringAlgorithm { } } - override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = { + override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = compute(stringFilter.filter(string.toCharArray))(new StringFilterDelegate) match { case Some(se) => Some(se.mkString) case None => None } - } - private[this] def deduplicate(ca: Array[Char]) = { + private[this] def deduplicate(ca: Array[Char]) = if (ca.length <= 1) ca else ca.sliding(2).filter(a => a(0) != a(1)).map(a => a(0)).toArray[Char] :+ ca.last - } private[this] def isVowel(c: Char) = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') @@ -97,14 +94,12 @@ object Nysiis extends StringAlgorithm { } } - private[this] def transcodeClean(ca: Array[Char]) = { + private[this] def transcodeClean(ca: Array[Char]) = if (ca.length >= 1 && (ca.last == 'a' || ca.last == 's')) ca.reverse.dropWhile(c => c == 'a' || c == 's').reverse else if (ca.length >= 2 && ca.last == 'y' && ca(ca.length - 2) == 'a') ca.dropRight(2) :+ 'y' - else - ca - } + else ca private[this] def transcodeHead(ca: Array[Char]) = { val h = ca.take(3).padTo(3, '\0') @@ -119,21 +114,18 @@ object Nysiis extends StringAlgorithm { Array('n', 'n') ++ ca.takeRight(ca.length - 2) else if (h.head == 'k') Array('c') ++ ca.takeRight(ca.length - 1) - else - ca + else ca } private[this] def transcodeLast(ca: Array[Char]) = { val h = ca.take(2).padTo(2, '\0') - if ( - (h.last == 't' && (h.head == 'd' || h.head == 'r' || h.head == 'n')) || + if ((h.last == 't' && (h.head == 'd' || h.head == 'r' || h.head == 'n')) || (h.last == 'd' && (h.head == 'r' || h.head == 'n')) ) Array('d') ++ ca.takeRight(ca.length - 2) else if (h.last == 'e' && (h.head == 'i' || h.head == 'e')) Array('y') ++ ca.takeRight(ca.length - 2) - else - ca + else ca } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisMetric.scala index 0112b06..6d4cec1 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/NysiisMetric.scala @@ -13,17 +13,14 @@ object NysiisMetric extends StringMetric { val ny1 = Nysiis.compute(ca1) val ny2 = Nysiis.compute(ca2) - if (!ny1.isDefined || !ny2.isDefined || (ny1.get.length == 0 && ny2.get.length == 0)) - None - else - Some(ny1.get.sameElements(ny2.get)) + if (!ny1.isDefined || !ny2.isDefined || (ny1.get.length == 0 && ny2.get.length == 0)) None + else Some(ny1.get.sameElements(ny2.get)) } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Soundex.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Soundex.scala index 33b285c..8d1a783 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Soundex.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Soundex.scala @@ -25,12 +25,11 @@ object Soundex extends StringAlgorithm { } } - override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = { + override def compute(string: String)(implicit stringFilter: StringFilter): Option[String] = compute(stringFilter.filter(string.toCharArray))(new StringFilterDelegate) match { case Some(se) => Some(se.mkString) case None => None } - } @tailrec private[this] def transcode(i: Array[Char], p: Char, o: Array[Char]): Array[Char] = { diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexMetric.scala index cca2f4e..439cf14 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/SoundexMetric.scala @@ -13,18 +13,14 @@ object SoundexMetric extends StringMetric { val se1 = Soundex.compute(ca1) val se2 = Soundex.compute(ca2) - if (!se1.isDefined || !se2.isDefined || (se1.get.length == 0 && se2.get.length == 0)) - None - else - Some(se1.get.sameElements(se2.get)) + if (!se1.isDefined || !se2.isDefined || (se1.get.length == 0 && se2.get.length == 0)) None + else Some(se1.get.sameElements(se2.get)) } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = { - // Unable to perform simple equality check, due to situations where no letters are passed. + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Boolean] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/similarity/DiceSorensenMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/similarity/DiceSorensenMetric.scala index d5e7eac..b800fb7 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/similarity/DiceSorensenMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/similarity/DiceSorensenMetric.scala @@ -19,12 +19,11 @@ object DiceSorensenMetric extends StringMetric { } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } private[this] def bigrams(ct: CompareTuple[Char]): MatchTuple[String] = { @tailrec @@ -37,7 +36,5 @@ object DiceSorensenMetric extends StringMetric { (set(ct._1, Array.empty[String]), set(ct._2, Array.empty[String])) } - private[this] def scoreMatches(mt: MatchTuple[String]) = { - mt._1.intersect(mt._2).length - } + private[this] def scoreMatches(mt: MatchTuple[String]) = mt._1.intersect(mt._2).length }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/similarity/HammingMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/similarity/HammingMetric.scala index 2dc1fbc..9e4951f 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/similarity/HammingMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/similarity/HammingMetric.scala @@ -13,12 +13,11 @@ object HammingMetric extends StringMetric { else Some(hamming(ca1, ca2)) } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Int] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Int] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } private[this] def hamming(ct: CompareTuple[Char]) = { require(ct._1.length == ct._2.length) diff --git a/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroMetric.scala index 2ad137f..d5895a6 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroMetric.scala @@ -26,12 +26,11 @@ object JaroMetric extends StringMetric { } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } private[this] def `match`(ct: CompareTuple[Char]): MatchTuple[Char] = { val window = math.abs((math.max(ct._1.length, ct._2.length) / 2d).floor.toInt - 1) diff --git a/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroWinklerMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroWinklerMetric.scala index a9f007e..53d1c40 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroWinklerMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/similarity/JaroWinklerMetric.scala @@ -24,10 +24,9 @@ object JaroWinklerMetric extends StringMetric { } } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Double] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } }
\ No newline at end of file diff --git a/core/source/core/scala/org/hashtree/stringmetric/similarity/LevenshteinMetric.scala b/core/source/core/scala/org/hashtree/stringmetric/similarity/LevenshteinMetric.scala index 4e58f02..7ac3974 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/similarity/LevenshteinMetric.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/similarity/LevenshteinMetric.scala @@ -14,12 +14,11 @@ object LevenshteinMetric extends StringMetric { else Some(levenshtein(ca1, ca2)) } - override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Int] = { + override def compare(string1: String, string2: String)(implicit stringFilter: StringFilter): Option[Int] = compare( stringFilter.filter(string1.toCharArray), stringFilter.filter(string2.toCharArray) )(new StringFilterDelegate) - } private[this] def levenshtein(ct: CompareTuple[Char]) = { val m = Array.fill[Int](ct._1.length + 1, ct._2.length + 1)(-1) |