summaryrefslogtreecommitdiff
path: root/core/src/test/scala/com/rockymadden/stringmetric/similarity/NGramMetricSpec.scala
blob: 5e1abb8132c4509a0ce55bf7a88a09eb53b23834 (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
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 NGramMetricSpec extends ScalaTest { "NGramMetric" should provide {
	"compare method" when passed {
		"empty arguments" should returns {
			"None" in {
				NGramMetric(1).compare("", "").isDefined should be (false)
				NGramMetric(1).compare("abc", "").isDefined should be (false)
				NGramMetric(1).compare("", "xyz").isDefined should be (false)
			}
		}
		"equal arguments" should returns {
			"1" in {
				NGramMetric(1).compare("abc", "abc").get should be (1)
				NGramMetric(2).compare("abc", "abc").get should be (1)
				NGramMetric(3).compare("abc", "abc").get should be (1)
			}
		}
		"unequal arguments" should returns {
			"0" in {
				NGramMetric(1).compare("abc", "xyz").get should be (0)
				NGramMetric(2).compare("abc", "xyz").get should be (0)
				NGramMetric(3).compare("abc", "xyz").get should be (0)
			}
		}
		"invalid arguments" should returns {
			"None" in {
				NGramMetric(2).compare("n", "naght").isDefined should be (false)
				NGramMetric(2).compare("night", "n").isDefined should be (false)
				NGramMetric(3).compare("ni", "naght").isDefined should be (false)
				NGramMetric(3).compare("night", "na").isDefined should be (false)
			}
		}
		"valid arguments" should returns {
			"Double indicating distance" in {
				NGramMetric(1).compare("night", "nacht").get should be (0.6)
				NGramMetric(1).compare("night", "naght").get should be (0.8)
				NGramMetric(1).compare("context", "contact").get should be (0.7142857142857143)

				NGramMetric(2).compare("night", "nacht").get should be (0.25)
				NGramMetric(2).compare("night", "naght").get should be (0.5)
				NGramMetric(2).compare("context", "contact").get should be (0.5)
				NGramMetric(2).compare("contextcontext", "contact").get should be (0.23076923076923078)
				NGramMetric(2).compare("context", "contactcontact").get should be (0.23076923076923078)
				NGramMetric(2).compare("ht", "nacht").get should be (0.25)
				NGramMetric(2).compare("xp", "nacht").get should be (0)
				NGramMetric(2).compare("ht", "hththt").get should be (0.2)

				NGramMetric(3).compare("night", "nacht").get should be (0)
				NGramMetric(3).compare("night", "naght").get should be (0.3333333333333333)
				NGramMetric(3).compare("context", "contact").get should be (0.4)
			}
		}
	}
}}