-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
202 lines (171 loc) · 5.51 KB
/
utils.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/iand/gordf"
)
var prefixToNs = map[string]string{
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"cs": "http://purl.org/vocab/changeset/schema#",
"bf": "http://schemas.talis.com/2006/bigfoot/configuration#",
"frm": "http://schemas.talis.com/2006/frame/schema#",
"dc": "http://purl.org/dc/elements/1.1/",
"dct": "http://purl.org/dc/terms/",
"dctype": "http://purl.org/dc/dcmitype/",
"foaf": "http://xmlns.com/foaf/0.1/",
"bio": "http://purl.org/vocab/bio/0.1/",
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
"rel": "http://purl.org/vocab/relationship/",
"rss": "http://purl.org/rss/1.0/",
"wn": "http://xmlns.com/wordnet/1.6/",
"air": "http://www.daml.org/2001/10/html/airport-ont#",
"contact": "http://www.w3.org/2000/10/swap/pim/contact#",
"ical": "http://www.w3.org/2002/12/cal/ical#",
"icaltzd": "http://www.w3.org/2002/12/cal/icaltzd#",
"frbr": "http://purl.org/vocab/frbr/core#",
"ad": "http://schemas.talis.com/2005/address/schema#",
"lib": "http://schemas.talis.com/2005/library/schema#",
"dir": "http://schemas.talis.com/2005/dir/schema#",
"user": "http://schemas.talis.com/2005/user/schema#",
"sv": "http://schemas.talis.com/2005/service/schema#",
"mo": "http://purl.org/ontology/mo/",
"status": "http://www.w3.org/2003/06/sw-vocab-status/ns#",
"label": "http://purl.org/net/vocab/2004/03/label#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"bibo": "http://purl.org/ontology/bibo/",
"ov": "http://open.vocab.org/terms/",
"void": "http://rdfs.org/ns/void#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"dbp": "http://dbpedia.org/resource/",
"dbpo": "http://dbpedia.org/ontology/",
"wiki": "http://en.wikipedia.org/wiki/",
"gn": "http://www.geonames.org/ontology#",
"cyc": "http://sw.opencyc.org/2009/04/07/concept/en/",
"s": "http://schema.org/",
"gr": "http://purl.org/goodrelations/v1#",
}
var nsToPrefix = map[string]string{}
func init() {
for prefix, ns := range prefixToNs {
nsToPrefix[ns] = prefix
}
}
func ucfirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[n:]
}
func lcfirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
}
func period(s string) string {
if s == "" {
return ""
}
r, _ := utf8.DecodeLastRuneInString(s)
if r != '.' {
return s + "."
}
return s
}
var rdfNumRegexp = regexp.MustCompile(`^http://www.w3.org/1999/02/22-rdf-syntax-ns#_([0-9]+)$`) // '~^[a-zA-Z][a-zA-Z0-9\-]+$~'
func rdfListItem(t rdf.Term) (int, bool) {
if !rdf.IsIRI(t) {
return 0, false
}
matches := rdfNumRegexp.FindStringSubmatch(t.Value)
if len(matches) == 2 {
if i, err := strconv.Atoi(matches[1]); err == nil {
return i, true
}
}
return 0, false
}
var prefixRegexp = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-]+$`) // '~^[a-zA-Z][a-zA-Z0-9\-]+$~'
func getPrefix(ns string) string {
if prefix, exists := nsToPrefix[ns]; exists {
return prefix
}
nsRaw := ns
if strings.HasSuffix(nsRaw, "#") {
nsRaw = nsRaw[:len(nsRaw)-1]
}
parts := strings.Split(nsRaw, "/")
for i := len(parts) - 1; i >= 0; i-- {
if len(parts[i]) > 3 && parts[i] != "schema" && parts[i] != "ontology" && parts[i] != "vocab" && parts[i] != "terms" && parts[i] != "ns" && parts[i] != "core" && prefixRegexp.MatchString(parts[i]) {
if _, exists := prefixToNs[parts[i]]; !exists {
prefix := strings.ToLower(parts[i])
prefixToNs[prefix] = ns
return prefix
}
}
}
i := 0
prefix := fmt.Sprintf("msg%d", i)
_, exists := prefixToNs[prefix]
for exists {
i++
prefix = fmt.Sprintf("msg%d", i)
_, exists = prefixToNs[prefix]
}
prefixToNs[prefix] = ns
return prefix
}
func qnameToIRI(qname string) rdf.Term {
if c := strings.IndexRune(qname, ':'); c != -1 {
prefix := qname[:c]
if ns, exists := prefixToNs[prefix]; exists {
return rdf.IRI(ns + qname[c+1:])
}
}
return rdf.Term{} // TODO: is there a better return value?
}
var iriRegexp = regexp.MustCompile(`^(?i)(.*[/#])([a-z0-9-_:]+)$`) // '~^(.*[\/\#])([a-z0-9\-\_\:]+)$~i'
func iriToQname(iri rdf.Term) (string, error) {
if !rdf.IsIRI(iri) {
return "", fmt.Errorf("cannot create QNames for non IRI terms")
}
ns, local := splitIRI(iri)
if local != "" {
prefix := getPrefix(ns)
return prefix + ":" + local, nil
}
return "", fmt.Errorf("cannot create QName")
}
func splitIRI(iri rdf.Term) (string, string) {
matches := iriRegexp.FindStringSubmatch(iri.Value)
if len(matches) > 2 {
return matches[1], matches[2]
}
return iri.Value, ""
}
// termID returns a string for use in id attributes HTML documents, may return an empty string
func termID(t rdf.Term) string {
if rdf.IsIRI(t) {
_, local := splitIRI(t)
return local
}
return ""
}
type AlphaContexts []*Context
func (a AlphaContexts) Len() int { return len(a) }
func (a AlphaContexts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AlphaContexts) Less(i, j int) bool { return a[i].Label(false, true) < a[j].Label(false, true) }
type orderedProperties []rdf.Term
func (o orderedProperties) Len() int { return len(o) }
func (o orderedProperties) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o orderedProperties) Less(i, j int) bool {
return PreferredPropertyOrder[o[i]] > PreferredPropertyOrder[o[j]]
}