forked from pgaskin/BookBrowser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
matching.go
70 lines (51 loc) · 1.74 KB
/
matching.go
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
package main
import (
"regexp"
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var leadingNumbers = regexp.MustCompile("^ *[0-9]+")
var betweenParentheses = regexp.MustCompile(`\(.*\)`)
var betweenBockHooks = regexp.MustCompile(`\[.*\]`)
var leadingZeroes = regexp.MustCompile(`^ *(0)([0-9]+) `)
var alphaNumeric = regexp.MustCompile(`[^a-z0-9]+`)
//var year = regexp.MustCompile(`(19[0-9]{2})|(20[0-9]{2})`)
func HashBook(author, title string) string {
author = strings.ToLower(author)
author = strings.Replace(author, "-", " ", -1)
title = strings.ToLower(title)
authorParts := strings.Split(author, " ")
lastName := authorParts[len(authorParts)-1]
//remove author from title
title = strings.Replace(title, author, "", -1)
title = strings.Replace(title, lastName, "", -1)
//remove leading numbers
title = leadingNumbers.ReplaceAllString(title, "")
//concatenate to half further actions
title = lastName + " " + title
title = removeAccents(title)
//make sure no whitespace is on either end
title = strings.TrimSpace(title)
//remove everything between parenthesis
title = betweenParentheses.ReplaceAllString(title, " ")
//remove everything between blockhooks
title = betweenBockHooks.ReplaceAllString(title, " ")
//remove ': a novel'
title = strings.Replace(title, ": a novel", " ", -1)
//remove leading zeroes from numbers
title = leadingZeroes.ReplaceAllString(title, " $2 ")
//remove all non [a-z0-9]
title = alphaNumeric.ReplaceAllString(title, "")
return title
}
func removeAccents(in string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
s, _, err := transform.String(t, in)
if err != nil {
return in
}
return s
}