summaryrefslogtreecommitdiff
path: root/core/source/test/scala/com/rockymadden/stringmetric/similarity/DiceSorensenMetricSpec.scala
blob: 5ddfc06238856bc421a08825a764bbc17b4fe23c (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.rockymadden.stringmetric.similarity

import com.rockymadden.stringmetric.ScalaTest
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
final class DiceSorensenMetricSpec extends ScalaTest {
	import DiceSorensenMetricSpec.Metric

	"DiceSorensenMetric" should provide {
		"compare method" when passed {
			"empty arguments" should returns {
				"None" in {
					Metric.compare("", "")(1).isDefined should be (false)
					Metric.compare("abc", "")(1).isDefined should be (false)
					Metric.compare("", "xyz")(1).isDefined should be (false)
				}
			}
			"equal arguments" should returns {
				"1" in {
					Metric.compare("abc", "abc")(1).get should be (1)
					Metric.compare("abc", "abc")(2).get should be (1)
					Metric.compare("abc", "abc")(3).get should be (1)
				}
			}
			"unequal arguments" should returns {
				"0" in {
					Metric.compare("abc", "xyz")(1).get should be (0)
					Metric.compare("abc", "xyz")(2).get should be (0)
					Metric.compare("abc", "xyz")(3).get should be (0)
				}
			}
			"invalid arguments" should returns {
				"None" in {
					Metric.compare("n", "naght")(2).isDefined should be (false)
					Metric.compare("night", "n")(2).isDefined should be (false)
					Metric.compare("ni", "naght")(3).isDefined should be (false)
					Metric.compare("night", "na")(3).isDefined should be (false)
				}
			}
			"valid arguments" should returns {
				"Double indicating distance" in {
					Metric.compare("night", "nacht")(1).get should be (0.6)
					Metric.compare("night", "naght")(1).get should be (0.8)
					Metric.compare("context", "contact")(1).get should be (0.7142857142857143)

					Metric.compare("night", "nacht")(2).get should be (0.25)
					Metric.compare("night", "naght")(2).get should be (0.5)
					Metric.compare("context", "contact")(2).get should be (0.5)
					Metric.compare("contextcontext", "contact")(2).get should be (0.3157894736842105)
					Metric.compare("context", "contactcontact")(2).get should be (0.3157894736842105)
					Metric.compare("ht", "nacht")(2).get should be (0.4)
					Metric.compare("xp", "nacht")(2).get should be (0)
					Metric.compare("ht", "hththt")(2).get should be (0.3333333333333333)

					Metric.compare("night", "nacht")(3).get should be (0)
					Metric.compare("night", "naght")(3).get should be (0.3333333333333333)
					Metric.compare("context", "contact")(3).get should be (0.4)
				}
			}
		}
	}
	"DiceSorensenMetric companion object" should provide {
		"pass-through compare method" should returns {
			"same value as class" in {
				DiceSorensenMetric.compare("context", "contact")(3).get should be (0.4)
			}
		}
	}
}

object DiceSorensenMetricSpec {
	private final val Metric = DiceSorensenMetric()
}