-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
66 lines (58 loc) · 1.99 KB
/
Copy pathstring.go
File metadata and controls
66 lines (58 loc) · 1.99 KB
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
package main
import (
"strings"
"github.com/hbollon/go-edlib"
)
const PERFECT_FACTOR = 0.85
func WhitespaceLength(line []rune) int {
i := 0
for i < len(line) {
charAt := line[i]
if charAt == ' ' || charAt == '\t' || charAt == '\n' || charAt == '\r' {
i++
continue
}
break
}
return i
}
func ContentSimilarity(targetLine string, candidateLine string) float32 {
targetRunes := []rune(targetLine)
candidateRunes := []rune(candidateLine)
targetWhitespace := WhitespaceLength(targetRunes)
candidateWhitespace := WhitespaceLength(candidateRunes)
similarity, err := edlib.StringsSimilarity(targetLine[targetWhitespace:], candidateLine[candidateWhitespace:], edlib.Levenshtein)
if err != nil {
return 0.0
} else {
return similarity
}
}
func LineContentSimilarity(targetLine string, candidateLine string) float32 {
overallContentSimilarity := ContentSimilarity(targetLine, candidateLine)
if overallContentSimilarity >= MINIMUM_RAW_SIMILARITY {
return overallContentSimilarity
}
// Even if we didn't score high on raw similarity, maybe we can break apart the thing into chunks and compare those?
splitTargetLine := strings.Split(targetLine, "=")
splitCandidateLine := strings.Split(candidateLine, "=")
if len(splitTargetLine) <= 1 || len(splitCandidateLine) <= 1 {
return 0.0
}
firstTermContentSimilarity := ContentSimilarity(splitTargetLine[0], splitCandidateLine[0])
if firstTermContentSimilarity >= MINIMUM_SIMILARITY_FIRST_TERM_ONLY {
return (1.0*PERFECT_FACTOR + firstTermContentSimilarity) / (PERFECT_FACTOR + 1)
}
var bestSimilarityFound float32 = 0.0
for i := 1; i < minint(len(splitCandidateLine), len(splitTargetLine)); i++ {
termSimilarity := ContentSimilarity(splitTargetLine[i], splitCandidateLine[i])
if termSimilarity > bestSimilarityFound {
bestSimilarityFound = termSimilarity
}
}
if bestSimilarityFound >= MINIMUM_SIMILARITY_SUBSEQUENT_TERMS {
result := (1.0*PERFECT_FACTOR + overallContentSimilarity) / (PERFECT_FACTOR + 1)
return result
}
return 0.0
}