def _read_mapping(self, filename): pairs = [] with open(filename) as f: for line in f.readlines(): acm_id, wiki_title = line.strip().split('\t') pairs.append((acm_id, to_category_uri(wiki_title))) return zip(*pairs)
def _compute_parents(self, node): """Returns and caches the parents (super-categories) for the given node. The parents are fetched from Wikipedia or from the local cache. Unlike self.get_parents, returns also the parents that are not in the 'subtree' of self._root. """ parents = self._parents.get(node) if not parents: # parents = self._wiki_graph.get_supercats(node) self._parents[node] = set( to_category_uri(parent) for parent in self._relations.parents(node))
def _compute_children(self, node): """Returns and caches the children (subcategories) for the given node. The children are fetched from Wikipedia or from the local cache. """ children = self._children.get(node) if children is None: # children = set(self._wiki_graph.get_subcats(node) or []) children = set( to_category_uri(child) for child in self._relations.children(node)) self._children[node] = children return children
def is_relevant(self, node): return to_category_uri(node) in self._relevant
def _compute_children(self, node): subcat_uris = self._wiki_graph.\ get_subcats(dbpedia.to_category_uri(node)) return sorted([dbpedia.to_title(uri) for uri in subcat_uris])
def is_parent(self, parent, child): return to_category_uri(child) in self.children(parent)
def _to_acm(self, node): return self._wiki2acm[to_category_uri(node)]
def contains(self, node): if is_acm_id(node): return node in self._acm2wiki else: return to_category_uri(node) in self._wiki2acm
if self.contains(child) ] else: return set() def is_parent(self, parent, child): return to_category_uri(child) in self.children(parent) def is_ancestor(self, parent, child): if self.is_parent(parent, child): return True return any([ self.is_ancestor(parents_child, child) for parents_child in self.children(parent) ]) acm = AcmWrapper() assert acm._acm2wiki['10002950'] == to_category_uri('Mathematics of computing') assert acm.is_parent('World Wide Web', 'Web applications') assert not acm.is_parent('Web applications', 'Social networks') assert not acm.is_ancestor('World Wide Web', 'Social networks') # import pprint # pprint.pprint( # [(to_title(acm._acm2wiki[parent]), to_title(acm._acm2wiki[child])) # for parent in acm._children.keys() # for child in acm._children[parent] # if parent in acm._acm2wiki and child in acm._acm2wiki])
def is_class(self, node): node = to_category_uri(node) if self._remember_gt and node in self._ground_truth: return self._ground_truth[node] else: return self._predict(to_features(self._features, [node]))[0]