From f044c242ef8afb79e9e7bcc8263d2c02cb02f73d Mon Sep 17 00:00:00 2001 From: pictuga Date: Thu, 25 Oct 2018 00:59:39 +0200 Subject: [PATCH] readabilite: simplify scoring loop For perfomance --- morss/readabilite.py | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/morss/readabilite.py b/morss/readabilite.py index f2cadf4..cd2c766 100644 --- a/morss/readabilite.py +++ b/morss/readabilite.py @@ -93,33 +93,19 @@ def score_node(node): return score -def score_all(root): - grades = {} +def score_all(node, grades=None): + " Fairly dumb loop to score all worthwhile nodes. Tries to be fast " - for node in list(root.iter()): - score = score_node(node) + if grades is None: + grades = {} - parent = node.getparent() - clean_node(node) + for child in node: + score = score_node(child) + child.attrib['seen'] = 'yes, ' + str(int(score)) - if parent is not None and node.getparent() is None: - # if the node got deleted/dropped (else, nothing to do) - # maybe now the parent only contains 1 item and needs to be flattened? - - gdparent = parent.getparent() - clean_node(parent) - - if gdparent is not None and parent.getparent() is None: - # if the parent got deleted/dropped - spread_score(gdparent, score + grades[parent], grades) - - else: - # if the parent was kept - spread_score(parent, score, grades) - - else: - # if the node was kept - spread_score(node, score, grades) + if score > 0: + spread_score(child, score, grades) + score_all(child, grades) return grades