-
Notifications
You must be signed in to change notification settings - Fork 36
/
freebase.py
55 lines (46 loc) · 1.35 KB
/
freebase.py
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
"""
Copyright 2015, University of Freiburg.
Elmar Haussmann <[email protected]>
"""
FREEBASE_NS_PREFIX = "http://rdf.freebase.com/ns/"
FREEBASE_NS_PREFIX_BYTES = FREEBASE_NS_PREFIX.encode('utf-8')
FREEBASE_SPARQL_PREFIX = "fb"
FREEBASE_NAME_RELATION = "type.object.name"
FREEBASE_KEY_PREFIX = "http://rdf.freebase.com/key/"
def get_prefixed_qualified_mid(mid, prefix):
return "%s:%s" % (prefix, mid)
def get_qualified_mid(mid):
'''
Returns a fully qualified MID, with NS
prefix and brackets.
:param mid:
:return:
'''
return "<%s%s>" % (FREEBASE_NS_PREFIX, mid)
def get_mid_from_qualified_string(qualified_str):
'''
Returns a mid from a fully qualified string
:param qualified_str
:return:
'''
if qualified_str.startswith('<') and qualified_str.endswith('>'):
qualified_str = qualified_str[1:-1]
return remove_freebase_ns(qualified_str)
def remove_freebase_ns(mid):
'''
Returns a MID without freebase namespace
:param mid:
:return:
'''
if mid.startswith(FREEBASE_NS_PREFIX):
return mid[len(FREEBASE_NS_PREFIX):]
return mid
def remove_freebase_ns_bytes(mid):
'''
Returns a MID without freebase namespace
:param mid:
:return:
'''
if mid.startswith(FREEBASE_NS_PREFIX_BYTES):
return mid[len(FREEBASE_NS_PREFIX_BYTES):]
return mid