summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorRocky Madden <git@rockymadden.com>2013-03-09 15:01:48 -0700
committerRocky Madden <git@rockymadden.com>2013-03-09 15:01:48 -0700
commit532568e38856f277eb947c46239ed39b98216947 (patch)
treedbed1f847de666babb0a40f94b7c00ccab5d93b2 /cli
parent2a775ee2a8279b458b84ef35c458c58b19aa98c9 (diff)
downloadstringmetric-532568e38856f277eb947c46239ed39b98216947.tar.gz
stringmetric-532568e38856f277eb947c46239ed39b98216947.tar.bz2
stringmetric-532568e38856f277eb947c46239ed39b98216947.zip
Created Jaccard metric, spec, benchmark, and CLI.
Diffstat (limited to 'cli')
-rwxr-xr-xcli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala4
-rwxr-xr-xcli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala51
-rwxr-xr-xcli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetricSpec.scala39
3 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 02b4b09..fc69dc7 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
@@ -5,7 +5,7 @@ import com.rockymadden.stringmetric.similarity.DiceSorensenMetric
/**
* The diceSorensenMetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the
- * Dice coefficient / Sorensen similarity index.
+ * Dice / Sorensen coefficient.
*/
object diceSorensenMetric extends Command {
override def main(args: Array[String]): Unit = {
@@ -31,7 +31,7 @@ object diceSorensenMetric extends Command {
val tab = " "
println(
- "Compares the similarity of two strings using the Dice coefficient / Sorensen similarity index." + ls + ls +
+ "Compares the similarity of two strings using the Dice / Sorensen coefficient." + ls + ls +
"Syntax:" + ls +
tab + "diceSorensenMetric [Options] string1 string2..." + ls + ls +
"Options:" + ls +
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
new file mode 100755
index 0000000..d87c84b
--- /dev/null
+++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetric.scala
@@ -0,0 +1,51 @@
+package com.rockymadden.stringmetric.cli.similarity
+
+import com.rockymadden.stringmetric.cli._
+import com.rockymadden.stringmetric.similarity.JaccardMetric
+
+/**
+ * The jaccardMetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the
+ * Jaccard coefficient.
+ */
+object jaccardMetric 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 Jaccard coefficient." + ls + ls +
+ "Syntax:" + ls +
+ tab + "jaccardMetric [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, traditionally 2."
+ )
+ }
+
+ override def execute(options: OptionMap): Unit = {
+ val strings: OptionMapArray = options('dashless)
+ val n: OptionMapInt = options('n)
+
+ println(JaccardMetric.compare(strings(0), strings(1))(n).getOrElse("not comparable"))
+ }
+}
diff --git a/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetricSpec.scala b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetricSpec.scala
new file mode 100755
index 0000000..2ade182
--- /dev/null
+++ b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/jaccardMetricSpec.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 jaccardMetricSpec extends ScalaTest {
+ "jaccardMetric" 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)(
+ jaccardMetric.main(Array("--unitTest", "--debug", "--n=2", "abc", "abc"))
+ )
+
+ out.toString should equal ("1.0\n")
+ out.reset()
+
+ Console.withOut(out)(
+ jaccardMetric.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 {
+ jaccardMetric.main(Array("--unitTest", "--debug"))
+ } should produce [IllegalArgumentException]
+ }
+ }
+ }
+ }
+}