summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorRocky Madden <git@rockymadden.com>2012-11-09 14:26:36 -0700
committerRocky Madden <git@rockymadden.com>2012-11-09 14:26:36 -0700
commitc9b9f06a7e1cdb9d661f210565fd91117fbcbf21 (patch)
tree7ce596e051c3db3d1677749d8a1bfe3e6025ada2 /cli
parentebfef117bdf0ff2b7888e9f6b45957d31403f27a (diff)
downloadstringmetric-c9b9f06a7e1cdb9d661f210565fd91117fbcbf21.tar.gz
stringmetric-c9b9f06a7e1cdb9d661f210565fd91117fbcbf21.tar.bz2
stringmetric-c9b9f06a7e1cdb9d661f210565fd91117fbcbf21.zip
Created N-Gram metric and algorithm commands and spec.
Diffstat (limited to 'cli')
-rwxr-xr-xcli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala59
-rwxr-xr-xcli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala61
-rwxr-xr-xcli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithmSpec.scala66
-rwxr-xr-xcli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramMetricSpec.scala68
4 files changed, 254 insertions, 0 deletions
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala
new file mode 100755
index 0000000..7bdd9aa
--- /dev/null
+++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala
@@ -0,0 +1,59 @@
+package org.hashtree.stringmetric.cli.similarity
+
+import org.hashtree.stringmetric.StringFilterDelegate
+import org.hashtree.stringmetric.cli._
+import org.hashtree.stringmetric.filter.AsciiLetterCaseStringFilter
+import org.hashtree.stringmetric.similarity.NGramAlgorithm
+
+/**
+ * The nGramAlgorithm [[org.hashtree.stringmetric.cli.Command]]. Returns the N-Gram representation of the passed string.
+ */
+object nGramAlgorithm extends Command {
+ override def main(args: Array[String]): Unit = {
+ val options = OptionMapUtility.toOptionMap(args)
+
+ try {
+ // Help.
+ if (options.contains('h) || options.contains('help)) {
+ help()
+ exit(options)
+ // Execute.
+ } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 0 &&
+ options.contains('n) && ParseUtility.parseInt(options('n)).isDefined
+ ) {
+ execute(options)
+ exit(options)
+ // Invalid syntax.
+ } else throw new IllegalArgumentException("Expected valid syntax. See --help.")
+ } catch {
+ case e => error(e, options)
+ }
+ }
+
+ override def help(): Unit = {
+ val ls = sys.props("line.separator")
+ val tab = " "
+
+ println(
+ "Returns the N-Gram representation of the passed string." + ls + ls +
+ "Syntax:" + ls +
+ tab + "nGramAlgorithm [Options] string..." + ls + ls +
+ "Options:" + ls +
+ tab + "-h, --help" + ls +
+ tab + tab + "Outputs description, syntax, and options." +
+ tab + "--n" + ls +
+ tab + tab + "The n."
+ )
+ }
+
+ override def execute(options: OptionMap): Unit = {
+ val n = ParseUtility.parseInt(options('n)).get
+ val ngram = NGramAlgorithm.compute(options('dashless))(n)
+ (new StringFilterDelegate with AsciiLetterCaseStringFilter)
+
+ ngram match {
+ case Some(a) => println(a.mkString("|"))
+ case None => println("not computable")
+ }
+ }
+} \ No newline at end of file
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala
new file mode 100755
index 0000000..39ca1b6
--- /dev/null
+++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala
@@ -0,0 +1,61 @@
+package org.hashtree.stringmetric.cli.similarity
+
+import org.hashtree.stringmetric.StringFilterDelegate
+import org.hashtree.stringmetric.cli._
+import org.hashtree.stringmetric.filter.AsciiLetterCaseStringFilter
+import org.hashtree.stringmetric.similarity.NGramMetric
+
+/**
+ * The nGramMetric [[org.hashtree.stringmetric.cli.Command]]. Compares the similarity of two strings using an N-Gram
+ * similarity index.
+ */
+object nGramMetric extends Command {
+ override def main(args: Array[String]): Unit = {
+ val options = OptionMapUtility.toOptionMap(args)
+
+ try {
+ // Help.
+ if (options.contains('h) || options.contains('help)) {
+ help()
+ exit(options)
+ // Execute.
+ } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 &&
+ options.contains('n) && ParseUtility.parseInt(options('n)).isDefined
+ ) {
+ execute(options)
+ exit(options)
+ // Invalid syntax.
+ } else throw new IllegalArgumentException("Expected valid syntax. See --help.")
+ } catch {
+ case e => error(e, options)
+ }
+ }
+
+ override def help(): Unit = {
+ val ls = sys.props("line.separator")
+ val tab = " "
+
+ println(
+ "Compares the similarity of two strings using an N-Gram similarity index." + ls + ls +
+ "Syntax:" + ls +
+ tab + "nGramMetric [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."
+ )
+ }
+
+ override def execute(options: OptionMap): Unit = {
+ val strings = options('dashless).split(" ")
+ val n = ParseUtility.parseInt(options('n)).get
+
+ println(
+ NGramMetric.compare(
+ strings(0),
+ strings(1)
+ )(n)(new StringFilterDelegate with AsciiLetterCaseStringFilter).getOrElse("not comparable").toString
+ )
+ }
+} \ No newline at end of file
diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithmSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithmSpec.scala
new file mode 100755
index 0000000..5997a9c
--- /dev/null
+++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithmSpec.scala
@@ -0,0 +1,66 @@
+package org.hashtree.stringmetric.cli.similarity
+
+import org.hashtree.stringmetric.ScalaTest
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+final class nGramAlgorithmSpec extends ScalaTest {
+ "nGramAlgorithm" should provide {
+ "main method" when passed {
+ "valid dashless argument and valid n argument" should executes {
+ "print N-Gram representation" in {
+ val out = new java.io.ByteArrayOutputStream()
+
+ Console.withOut(out)(
+ nGramAlgorithm.main(
+ Array(
+ "--unitTest",
+ "--debug",
+ "--n=1",
+ "aBc"
+ )
+ )
+ )
+
+ out.toString should equal ("a|B|c\n")
+ out.reset()
+
+ Console.withOut(out)(
+ nGramAlgorithm.main(
+ Array(
+ "--unitTest",
+ "--debug",
+ "--n=2",
+ "aBc"
+ )
+ )
+ )
+
+ out.toString should equal ("aB|Bc\n")
+ out.reset()
+ }
+ }
+ "valid dashless argument and invalid n argument" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ nGramAlgorithm.main(
+ Array(
+ "--unitTest",
+ "aBc",
+ "abc"
+ )
+ )
+ } should produce [IllegalArgumentException]
+ }
+ }
+ "no dashless argument" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ nGramAlgorithm.main(Array("--unitTest", "--debug"))
+ } should produce [IllegalArgumentException]
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramMetricSpec.scala
new file mode 100755
index 0000000..18e8a50
--- /dev/null
+++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/similarity/nGramMetricSpec.scala
@@ -0,0 +1,68 @@
+package org.hashtree.stringmetric.cli.similarity
+
+import org.hashtree.stringmetric.ScalaTest
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+final class nGramMetricSpec extends ScalaTest {
+ "nGramMetric" should provide {
+ "main method" when passed {
+ "valid dashless arguments and valid n argument" should executes {
+ "print if they are a match" in {
+ val out = new java.io.ByteArrayOutputStream()
+
+ Console.withOut(out)(
+ nGramMetric.main(
+ Array(
+ "--unitTest",
+ "--debug",
+ "--n=1",
+ "aBc",
+ "abc"
+ )
+ )
+ )
+
+ out.toString should equal ("1.0\n")
+ out.reset()
+
+ Console.withOut(out)(
+ nGramMetric.main(
+ Array(
+ "--unitTest",
+ "--debug",
+ "--n=1",
+ "aBc",
+ "xyz"
+ )
+ )
+ )
+
+ out.toString should equal ("0.0\n")
+ out.reset()
+ }
+ }
+ "valid dashless arguments and invalid n argument" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ nGramMetric.main(
+ Array(
+ "--unitTest",
+ "aBc",
+ "abc"
+ )
+ )
+ } should produce [IllegalArgumentException]
+ }
+ }
+ "no dashless arguments" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ nGramMetric.main(Array("--unitTest", "--debug"))
+ } should produce [IllegalArgumentException]
+ }
+ }
+ }
+ }
+} \ No newline at end of file