def write_nested_lists(fl): # fl is the absolute *path* to the file which contains the ontology file try: n_lists = parse_ontologies.nested_lists(fl) except: fl = re.sub('updated', 'original', fl) print fl n_lists = parse_ontologies.nested_lists(fl) f = open(path_to_pa+'updated/nested_lists.py', 'w') f.write('nlists = '+str(n_lists)) f.close()
def write_nested_lists( fl ): # fl is the absolute *path* to the file which contains the ontology file try: n_lists = parse_ontologies.nested_lists(fl) except: fl = re.sub('updated', 'original', fl) print fl n_lists = parse_ontologies.nested_lists(fl) f = open(path_to_pa + 'updated/nested_lists.py', 'w') f.write('nlists = ' + str(n_lists)) f.close()
def extract_statements(fl, version=0): statements = {'instance': set([]), 'subclass': set([]), 'facts': set([]), 'agentNeeded': set([])} try: sys.path.append(path_to_pa+'updated') import nested_lists dictio = nested_lists.nlists[0] except: tup = parse_ontologies.nested_lists(fl, version) dictio = tup[0] f = open(path_to_pa+'updated/nested_lists.py', 'w') f.write('nlists = '+str(tup)) f.close() try: if not abb.has_key(fl): abbrev = abbr_n_vers[fl][0] # e.g. 'MD' for 'Media.kif' except: pass actions = [] for tup in dictio: formula = dictio[tup] # a 'tup' looks like this --> (1047647, 1047670): ['subclass', 'Pollen', 'Object'] # Also, we will notice that everything is a subclass of itself (Naive set theory) blacklisted = set(['documentation', '=>', 'domain', 'subAttribute', 'partition', 'range', 'relatedInternalConcept', 'disjointRelation', 'rangeSubclass', 'not', 'disjointDecomposition', 'synonymousExternalConcept', 'names', 'abbreviation', 'conventionalLongName', 'formerName', 'conventionalLongName', 'organizationalObjective', 'equal', 'subsumesContentClass', 'termFormat', 'externalImage', 'forall', 'longitude', 'format', 'exhaustiveDecomposition', 'dateEstablished', 'modalAttribute', 'lexicon', 'earthAltitude', 'latitude', 'located', 'depth', 'landAreaOnly', 'flowsInto', '<=>', 'disjoint', 'subrelation', 'domainSubclass', 'contraryAttribute']) if formula[0] == '=>' and formula[1][0] == 'and' and formula[2][0] == 'causesProposition' and formula[2][1][0] == 'and' and formula[2][2][0] == 'and': actions.append(formula) elif formula[0] == 'serviceProvider': form2 = ['agentNeeded', formula[2], formula[1]] statements['agentNeeded'].add(to_prolog(form2)) statements['facts'].add(to_prolog(formula)) elif not formula[0] in blacklisted: a = to_prolog(formula) if a: # i.e. if to_prolog has not returned None if a[:5] == 'class': statements['instance'].add(a) elif a[:8] == 'subclass': statements['subclass'].add(a) else: statements['facts'].add(a) statements['actions'] = find_actions(actions) return statements
def get_candidates(ontology): """ includes a recursive function which traverses the trees (nested structures) in the ontology to decide which terms ('concepts') are 'candidates' for semantic matching. For instance, the following are unlikely to change from one version to the other so, they are not 'candidates': -- built-in SUO-KIF operators such as 'and', 'not', 'or', '=>' and '<=>', as well as quantifiers such as 'exists' and 'forall' because they are part of the ontology language and not the ontology itself. Pease, A. (2009) "Standard Upper Ontology Knowledge Interchange Format", online. -- fundamental SUMO concepts which we can assume as known to ORS and very unlikely to change. Examples are 'subclass', 'instance', 'documentation', 'domain', 'attribute', 'disjoint' etc. """ try: sys.path.append(path_to_pa+'updated') import nested_lists tup = nested_lists.nlists except: tup = parse_ontologies.nested_lists(ontology) f = open(path_to_pa+'updated/nested_lists.py', 'w') f.write('nlists = '+str(tup)) f.close() """ The above tuple contains two dictionaries, one with nested lists and one with comments. See the nested_lists function in the data/parse_ontologies.py module. """ trees = tup[0] comments = tup[1] # will use it later excluded = set(['and', 'not', 'or', '=>', '<=>', 'forall', 'exists', 'subclass', 'instance', 'documentation', 'domain', 'attribute', 'disjoint', 'equal']) candidates = [] def recursive_backtracking(item): # item can be a list or a string if type(item) == str: # exclude uninstantiated variables and phrases if not item in excluded and not item[0] == '?' and not item[0] == '"' and not item in candidates and not is_number(item): candidates.append(item) else: for thing in item: recursive_backtracking(thing) for tu in trees: recursive_backtracking(trees[tu]) candidates.sort() # To print 'candidates' in a file, # comment out the following code: """ f = open(path_here+'candidates.txt', 'w') f.write(str(candidates)) f.close() """ # candidates is a list of concepts in the input ontology # which are 'candidates' for semantic matching # e.g. ['SmokingPipe', 'Smuggling', 'Snake', 'Sober'...etc] return candidates