summaryrefslogtreecommitdiff
path: root/cli/source
diff options
context:
space:
mode:
authorRocky Madden <git@rockymadden.com>2012-10-29 13:31:47 -0600
committerRocky Madden <git@rockymadden.com>2012-10-29 13:31:47 -0600
commit400973bb472918b6046cd7fc1ef65f040225d1b3 (patch)
treed02ca9b6eb539fd5475b5c82c3e6030b10fac0f6 /cli/source
parent26d85c948eb5359e62f9ad54469a3730fb0499f5 (diff)
downloadstringmetric-400973bb472918b6046cd7fc1ef65f040225d1b3.tar.gz
stringmetric-400973bb472918b6046cd7fc1ef65f040225d1b3.tar.bz2
stringmetric-400973bb472918b6046cd7fc1ef65f040225d1b3.zip
Created refined soundex algorithm, metric, command, and specs.
Diffstat (limited to 'cli/source')
-rwxr-xr-xcli/source/core/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetric.scala56
-rwxr-xr-xcli/source/test/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetricSpec.scala46
2 files changed, 102 insertions, 0 deletions
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetric.scala
new file mode 100755
index 0000000..d05e602
--- /dev/null
+++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetric.scala
@@ -0,0 +1,56 @@
+package org.hashtree.stringmetric.cli.command
+
+import org.hashtree.stringmetric.StringFilterDelegate
+import org.hashtree.stringmetric.cli._
+import org.hashtree.stringmetric.cli.command._
+import org.hashtree.stringmetric.phonetic.RefinedSoundexMetric
+
+/**
+ * The refinedSoundexMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to determine if they are
+ * phonetically similarly, per the refined Soundex algorithm.
+ */
+object refinedSoundexMetric 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) {
+ 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 two strings to determine if they are phonetically similarly, per the refined Soundex algorithm." + ls + ls +
+ "Syntax:" + ls +
+ tab + "refinedSoundexMetric [Options] string1 string2..." + ls + ls +
+ "Options:" + ls +
+ tab + "-h, --help" + ls +
+ tab + tab + "Outputs description, syntax, and options."
+ )
+ }
+
+ override def execute(options: OptionMap): Unit = {
+ val strings = options('dashless).split(" ")
+
+ println(
+ RefinedSoundexMetric.compare(
+ strings(0),
+ strings(1)
+ )(new StringFilterDelegate).getOrElse("not comparable").toString
+ )
+ }
+} \ No newline at end of file
diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetricSpec.scala
new file mode 100755
index 0000000..480473a
--- /dev/null
+++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/refinedSoundexMetricSpec.scala
@@ -0,0 +1,46 @@
+package org.hashtree.stringmetric.cli.command
+
+import org.hashtree.stringmetric.ScalaTest
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+
+@RunWith(classOf[JUnitRunner])
+final class refinedSoundexMetricSpec extends ScalaTest {
+ "refinedSoundexMetric" 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)(
+ refinedSoundexMetric.main(Array("--unitTest", "--debug", "aBc", "abc"))
+ )
+
+ out.toString should equal ("true\n")
+ out.reset()
+
+ Console.withOut(out)(
+ refinedSoundexMetric.main(Array("--unitTest", "--debug", "aBc", "xyz"))
+ )
+
+ out.toString should equal ("false\n")
+ out.reset()
+
+ Console.withOut(out)(
+ refinedSoundexMetric.main(Array("--unitTest", "--debug", "1", "1"))
+ )
+
+ out.toString should equal ("not comparable\n")
+ out.reset()
+ }
+ }
+ "no dashless arguments" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ refinedSoundexMetric.main(Array("--unitTest", "--debug"))
+ } should produce [IllegalArgumentException]
+ }
+ }
+ }
+ }
+} \ No newline at end of file