summaryrefslogtreecommitdiff
path: root/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/diceSorensenMetric.scala
blob: 5d6590c88ecf0017bc1d37877cf427fac6d7d4e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.hashtree.stringmetric.cli.similarity

import org.hashtree.stringmetric.cli._
import org.hashtree.stringmetric.similarity.DiceSorensenMetric

/**
 * The diceSorensenMetric [[org.hashtree.stringmetric.cli.Command]]. Compares the similarity of two strings using the
 * Dice coefficient / Sorensen similarity index.
 */
object diceSorensenMetric 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 the Dice coefficient / Sorensen similarity index." + ls + ls +
			"Syntax:" + ls +
			tab + "diceSorensenMetric [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 = options('dashless).split(" ")
		val n = ParseUtility.parseInt(options('n)).get

		println(
			DiceSorensenMetric.compare(
				strings(0),
				strings(1)
			)(n).getOrElse("not comparable").toString
		)
	}
}