diff options
author | Rocky Madden <git@rockymadden.com> | 2013-03-12 17:53:59 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-03-12 17:53:59 -0600 |
commit | b848cf2945b0be8d9fad0e81d98deda78e7443dd (patch) | |
tree | be4b6e8592c5166f4e9e162241b1e112f626e39d /cli/source | |
parent | cd4919a5093b916e5f568734f8ec2e799aa1037a (diff) | |
download | stringmetric-b848cf2945b0be8d9fad0e81d98deda78e7443dd.tar.gz stringmetric-b848cf2945b0be8d9fad0e81d98deda78e7443dd.tar.bz2 stringmetric-b848cf2945b0be8d9fad0e81d98deda78e7443dd.zip |
Created overlap metric, spec, benchmark, and CLI.
Diffstat (limited to 'cli/source')
4 files changed, 92 insertions, 2 deletions
diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala index fc69dc7..e0d3f80 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala @@ -38,7 +38,7 @@ object diceSorensenMetric extends Command { tab + "-h, --help" + ls + tab + tab + "Outputs description, syntax, and options." + tab + "--n" + ls + - tab + tab + "The n, traditionally 2." + tab + tab + "The n-gram size, traditionally 2." ) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala index d87c84b..b7aa9c7 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala @@ -38,7 +38,7 @@ object jaccardMetric extends Command { tab + "-h, --help" + ls + tab + tab + "Outputs description, syntax, and options." + tab + "--n" + ls + - tab + tab + "The n, traditionally 2." + tab + tab + "The n-gram size." ) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetric.scala new file mode 100755 index 0000000..ebf0aca --- /dev/null +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetric.scala @@ -0,0 +1,51 @@ +package com.rockymadden.stringmetric.cli.similarity + +import com.rockymadden.stringmetric.cli._ +import com.rockymadden.stringmetric.similarity.OverlapMetric + +/** + * The overlapMetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the + * overlap coefficient. + */ +object overlapMetric extends Command { + override def main(args: Array[String]): Unit = { + val options = OptionMap(args) + + try + if (options.contains('h) || options.contains('help)) { + help() + exit(options) + } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2 + && options.contains('n) && (options('n): OptionMapInt) >= 1) { + + execute(options) + exit(options) + } else throw new IllegalArgumentException("Expected valid syntax. See --help.") + catch { + case e: Throwable => error(e, options) + } + } + + override def help(): Unit = { + val ls = sys.props("line.separator") + val tab = " " + + println( + "Compares the similarity of two strings using the overlap coefficient." + ls + ls + + "Syntax:" + ls + + tab + "overlapMetric [Options] string1 string2..." + ls + ls + + "Options:" + ls + + tab + "-h, --help" + ls + + tab + tab + "Outputs description, syntax, and options." + + tab + "--n" + ls + + tab + tab + "The n-gram size." + ) + } + + override def execute(options: OptionMap): Unit = { + val strings: OptionMapArray = options('dashless) + val n: OptionMapInt = options('n) + + println(OverlapMetric.compare(strings(0), strings(1))(n).getOrElse("not comparable")) + } +} diff --git a/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetricSpec.scala b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetricSpec.scala new file mode 100755 index 0000000..c1bb6b6 --- /dev/null +++ b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/overlapMetricSpec.scala @@ -0,0 +1,39 @@ +package com.rockymadden.stringmetric.cli.similarity + +import com.rockymadden.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class overlapMetricSpec extends ScalaTest { + "overlapMetric" should provide { + "main method" when passed { + "valid dashless arguments" should executes { + "print if they are a match" in { + val out = new java.io.ByteArrayOutputStream() + + Console.withOut(out)( + overlapMetric.main(Array("--unitTest", "--debug", "--n=2", "abc", "abc")) + ) + + out.toString should equal ("1.0\n") + out.reset() + + Console.withOut(out)( + overlapMetric.main(Array("--unitTest", "--debug", "--n=2", "abc", "xyz")) + ) + + out.toString should equal ("0.0\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + overlapMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} |