def evaluate(self, term_list_1, term_list_2): """Performs a set of evaluations (in no particular order) and returns their results as members of a ResultSet""" results=ResultSet() for each_evaluator in self: logging.log(ULTRADEBUG, "Applying %s as part of an EvaluationGroup", each_evaluator.__class__.__name__) results.add(each_evaluator.evaluate(term_list_1, term_list_2)) return results
def create_graph(self, list_of_lines): # Make sure that the measurements only reflect one run """Sets up the creation of a graph, ensuring that the metrics from the creation reflect only the latest run.""" self._measurements = ResultSet() self._line_set_id = list_of_lines.set_id self._preprocess_article(list_of_lines.lines) graph = self._create_graph(list_of_lines.lines) graph = self._post_process_graph(graph) self._compute_measurements(list_of_lines.lines) return graph
def __init__(self, type_of_graph_to_build=Graph, node_weight_threshold=0.0, link_weight_threshold=0.0, tf_idf_provider=None, add_orphan_nodes=True): """Accepts a type of Graph to build (so we can use Graph subclasses without creating a separate hierarchy of GraphBuilder subclasses)""" self._type_of_graph_to_build = type_of_graph_to_build self._node_weight_threshold = node_weight_threshold self._link_weight_threshold = link_weight_threshold self._tf_idf = tf_idf_provider self._tf_idf_scores = None self._line_set_id = None self._node_cache = set([]) self._add_orphan_nodes = add_orphan_nodes self._measurements = ResultSet()
def compute_measures(self): """Computes graph metrics for the current object.""" self._consolidate_if_necessary() logging.log(ULTRADEBUG, "Computing graph metrics for %r", self) graph_measures = ResultSet() graph_measures.add(GraphNumberLinks(len(self._relationships))) unique_nodes = set() for a_relation in self._relationships: unique_nodes.add(a_relation.node1) unique_nodes.add(a_relation.node2) graph_measures.add(GraphNumberNodes(len(unique_nodes))) graph_measures.add( GraphAverageNodeWeight( reduce(operator.add, [x.weight for x in unique_nodes]) / float(len(unique_nodes)))) graph_measures.add( GraphAverageLinkWeight( reduce(operator.add, [x.weight for x in self._relationships]) / float(len(self._relationships)))) graph_measures.add( GraphLinkDegree( float(len(self._relationships)) / float(len(unique_nodes)))) logging.log(ULTRADEBUG, "Starting computation of the distance matrix.") distmat = DistanceMatrix(self.as_mapped_link_matrix()) logging.log(ULTRADEBUG, "Distance matrix obtained. Computing stats.") rocs = [ distmat.relative_out_centrality(x) for x in xrange(len(distmat)) ] rics = [ distmat.relative_in_centrality(x) for x in xrange(len(distmat)) ] avrocs = reduce(operator.add, rocs) / float(len(distmat)) avrics = reduce(operator.add, rics) / float(len(distmat)) graph_measures.add(GraphRelativeOutCentrality(avrocs)) graph_measures.add(GraphRelativeInCentrality(avrics)) graph_measures.add(GraphStratum(distmat.stratum())) graph_measures.add(GraphCompactness(distmat.compactness())) logging.log(ULTRADEBUG, "Finished computing graph metrics.") return graph_measures