Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_onto_for_testing(TEST_ONLINE=False):
"Wrapper for util script used in viz main methods"
if TEST_ONLINE:
from ontospy import Ontospy
g = Ontospy("http://cohere.open.ac.uk/ontology/cohere.owl#")
else:
from ontospy.core.manager import get_random_ontology
uri, g = get_random_ontology(50)
return g
def read_emotions_dict(depth = 'inf', path="../NIL_ontoEmotion/emotions_v11.owl"):
"""Reads the ontology and returns an empty dict containing word describing emotions at the specified depth of the tree
Args:
depth: depth of the tree in the ontology (default: {'inf'})
path: path of the ontology (default: {"../NIL_ontoEmotion/emotions_v11.owl"})
Returns:
dict: the keys of this dict are the names of emotions
"""
model = ontospy.Ontospy("../NIL_ontoEmotion/emotions_v11.owl")
emotions_dict = {}
emotions_parents = {}
print 'Reading NIL_ontoEmotion...'
if depth=='inf':
for emotion_class in model.classes:
emotions_dict[emotion_class.bestLabel().lower()] = []
elif depth==1:
for emotion_class in model.getClass('http://nil.fdi.ucm.es/projects/emotions/onto/emotions_v11.owl#Emotion').children():
emotions_dict[emotion_class.bestLabel().lower()] = []
for emotion_class_2 in emotion_class.children():
emotions_parents[emotion_class_2.bestLabel().lower()] = emotion_class.bestLabel().lower()
for emotion_class_3 in emotion_class_2.children():
emotions_parents[emotion_class_3.bestLabel().lower()] = emotion_class.bestLabel().lower()
:return:
:rtype: OWLParser
"""
session = requests.Session()
if url.startswith('file://'):
session.mount('file://', FileAdapter())
res = session.get(url)
try:
owl = OWLParser(content=res.content)
return owl
except:
g = nx.DiGraph(IRI=url)
o = ontospy.Ontospy(url)
for cls in o.classes:
g.add_node(cls.locale, type='Class')
for parent in cls.parents():
g.add_edge(cls.locale, parent.locale, type='SubClassOf')
for instance in cls.instances():
_, frag = urldefrag(instance)
g.add_edge(frag, cls.locale, type='ClassAssertion')
return g
def generate_class_tree_file(ontology_name = 'dbpedia_2016-10', prune=True):
print('loading ontology: ', ontology_name)
ont = ontospy.Ontospy('{0}.nt'.format(ontology_name))
all_classes = set()
print('building class relationships')
relationships = {}
for cl in ont.classes:
relationships[to_class_name(cl)] = {
'parents': [to_class_name(p) for p in cl.parents()],
'children': [to_class_name(c) for c in cl.children()],
}
parents = set([to_class_name(p) for p in cl.parents()])
children = set([to_class_name(c) for c in cl.children()])
all_classes = all_classes.union(parents, children, set([to_class_name(cl)]))
if prune:
relationships = {name: rels for (name, rels) in relationships.items() if has_relations(rels)}
def generate_class_tree_file(ontology_path = 'ontologies/dbpedia_2016-10.nt', prune=False):
print('loading ontology: ', ontology_path)
ont = ontospy.Ontospy(ontology_path)
all_classes = set()
print('building class relationships')
relationships = {}
for cl in ont.classes:
relationships[to_class_name(cl)] = {
'parents': [to_class_name(p) for p in cl.parents()],
'children': [to_class_name(c) for c in cl.children()],
}
parents = {to_class_name(p) for p in cl.parents()}
children = {to_class_name(c) for c in cl.children()}
all_classes = all_classes.union(parents, children, set([to_class_name(cl)]))
print('pre prune relationships length:', len(relationships.keys()))
def get_onto_for_testing(TEST_ONLINE=False):
"Wrapper for util script used in viz main methods"
if TEST_ONLINE:
from ontospy import Ontospy
g = Ontospy("http://cohere.open.ac.uk/ontology/cohere.owl#")
else:
from ontospy.core.manager import get_random_ontology
uri, g = get_random_ontology(50)
return g