-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.py
More file actions
145 lines (95 loc) · 4.62 KB
/
entity.py
File metadata and controls
145 lines (95 loc) · 4.62 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
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
'''
Created on 20 Mar 2019
@author: ejimenez-ruiz
'''
from enum import Enum
class KG(Enum):
DBpedia = 0
Wikidata = 1
Google = 2
All = 3
class URI_KG(object):
dbpedia_uri_resource = 'http://dbpedia.org/resource/'
dbpedia_uri_property = 'http://dbpedia.org/property/'
dbpedia_uri = 'http://dbpedia.org/ontology/'
wikidata_uri ='http://www.wikidata.org/entity/'
schema_uri = 'http://schema.org/'
uris = list()
uris.append(dbpedia_uri)
uris.append(wikidata_uri)
uris.append(schema_uri)
uris_resource = list()
uris_resource.append(dbpedia_uri_resource)
uris_resource.append(wikidata_uri)
wikimedia_disambiguation_concept=wikidata_uri+'Q4167410'
avoid_predicates=set()
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageDisambiguates")
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageRedirects")
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageWikiLink")
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageID")
#Large amount of text
avoid_predicates.add("http://dbpedia.org/ontology/abstract")
avoid_predicates.add("http://www.w3.org/2000/01/rdf-schema#comment")
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageRevisionID")
avoid_predicates.add("http://dbpedia.org/ontology/wikiPageExternalLink")
avoid_predicates.add("http://purl.org/dc/terms/subject") #Link to categories
avoid_predicates.add("http://www.w3.org/2000/01/rdf-schema#seeAlso")
avoid_predicates.add("http://purl.org/linguistics/gold/hypernym")
avoid_predicates.add("http://xmlns.com/foaf/0.1/primaryTopic")
#avoid_predicates.add("http://www.w3.org/2002/07/owl#differentFrom")
#avoid_predicates.add("http://www.w3.org/2002/07/owl#sameAs")
avoid_predicates.add("http://dbpedia.org/property/related")
avoid_top_concepts=set()
avoid_top_concepts.add("http://www.w3.org/2002/07/owl#Thing")
avoid_top_concepts.add("http://www.wikidata.org/entity/Q35120") #something
avoid_top_concepts.add("http://www.wikidata.org/entity/Q830077") #subject
avoid_top_concepts.add("http://www.wikidata.org/entity/Q18336849") #item with given name property
avoid_top_concepts.add("http://www.wikidata.org/entity/Q23958946") #individual/instance
avoid_top_concepts.add("http://www.wikidata.org/entity/Q26720107") # subject of a right
avoid_top_concepts.add("http://www.wikidata.org/entity/Q488383") # object
avoid_top_concepts.add("http://www.wikidata.org/entity/Q4406616") #concrete object
avoid_top_concepts.add("http://www.wikidata.org/entity/Q29651224") # natural object
avoid_top_concepts.add("http://www.wikidata.org/entity/Q223557") #physical object
avoid_top_concepts.add("http://www.wikidata.org/entity/Q16686022") # natural physical object
def __init__(self):
''''
'''
class KGEntity(object):
def __init__(self, enity_id, label, description, types, source):
self.ident = enity_id
self.label = label
self.desc = description #sometimes provides a very concrete type or additional semantics
self.types = types #set of semantic types
self.source = source #KG of origin: dbpedia, wikidata or google kg
def __repr__(self):
return "<id: %s, label: %s, description: %s, types: %s, source: %s>" % (self.ident, self.label, self.desc, self.types, self.source)
def __str__(self):
return "<id: %s, label: %s, description: %s, types: %s, source: %s>" % (self.ident, self.label, self.desc, self.types, self.source)
def getId(self):
return self.ident
'''
One can retrieve all types or filter by KG: DBpedia, Wikidata and Google (Schema.org)
'''
def getTypes(self, kgfilter=KG.All):
if kgfilter==KG.All:
return self.types
else:
kg_uri = URI_KG.uris[kgfilter.value]
filtered_types = set()
for t in self.types:
if t.startswith(kg_uri):
filtered_types.add(t)
return filtered_types
def getLabel(self):
return self.label
def getDescription(self):
return self.desc
def getSource(self):
return self.sourcec
def addType(self, cls):
self.types.add(cls)
def addTypes(self, types):
self.types.update(types)
if __name__ == '__main__':
print(URI_KG.uris[KG.DBpedia.value])
print(KG.DBpedia.value)