-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringExtractors.go
115 lines (92 loc) · 2.62 KB
/
stringExtractors.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"regexp"
"strings"
)
func getMarking(text string) string {
marking := text[strings.Index(text, ":"): len(text)]
return marking
}
func getNote(text string) string {
note := text[strings.Index(text, ":") + 2:]
return note
}
func getSiteInformation(text string) string {
siteInformation := text[0:strings.Index(text, ":")]
return siteInformation
}
func getTitleAndAuthorOrMarking(text string) (string, string) {
isAddingInformation := strings.Contains(text, "Hinzugefügt am")
isQuote := strings.Contains(text, `"`) || strings.Contains(text,"»") || strings.Contains(text, "«")
title := ""
author := ""
if !isAddingInformation && !isQuote && checkAuthorString(text) {
re := regexp.MustCompile(`(?m)\((.*?)\)`)
str := text
foundStrings := re.FindAllString(str, -1)
author = foundStrings[len(foundStrings) - 1]
author = author[1 : len(author) - 1]
authorSplitted := strings.Split(author, ", ")
author = authorSplitted[1] + " " + authorSplitted[0]
firstParentheses := strings.Index(text, "(")
title = text[0:firstParentheses]
} else {
return text, "marking"
}
return title, author
}
func checkAuthorString(text string) bool {
re := regexp.MustCompile(`(?m)\((.*?)\)`)
str := text
foundStrings := re.FindAllString(str, -1)
isAuthor := len(foundStrings) > 0
if isAuthor {
author := foundStrings[len(foundStrings) - 1]
author = author[1 : len(author) - 1]
authorSplitted := strings.Split(author, ", ")
// When there are not two strings it's probably not a name
if len(authorSplitted) < 2 {
return false
}
}
return isAuthor
}
func checkNoteType(text string) string {
if strings.Contains(text, NOTE) {
return "note"
} else if strings.Contains(text, MARKING) {
return "marking"
} else if strings.Contains(text, BOOKMARK) {
return "bookmark"
} else if strings.Contains(text, DELIMETER) {
return "delimeter"
} else if strings.Contains(text, ADDED) || strings.Contains(text, CHANGED) {
return "added"
} else if len(text) == 0 {
return "empty"
}
return "other"
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func getNotesSortedByBooks(books []string, notes Notes) []Notes {
sortedNotes := make([]Notes, 0)
for _ = range books {
emptyNote := make([]Note, 0)
sortedNotes = append(sortedNotes, emptyNote)
}
for _, note := range notes {
bookIndex := IndexOf(books, note.book)
sortedNotes[bookIndex] = append(sortedNotes[bookIndex], note)
}
return sortedNotes
}
func getStringSeperated(text string, seperator string) string {
return text[strings.Index(text, seperator) + 1: len(text)]
}