def find_similar(tree): c = db.conn.cursor() goal = 5 results = [] shel_stack = tree.sub_leaves() while shel_stack and len(results) < goal: #print("shel_stack size") #print(len(shel_stack)) item = shel_stack.pop() idx = 1 froms = ["FROM nodes n1 "] wheres= [" WHERE n1.is_input=1 AND n1.type=? AND n1.word=?"] paramss = [[ item.label(),item[0] ]] ancestry = [item] combinesql = lambda: (' '.join(froms))+(' '.join(wheres)) while True: debug_ex(c,"SELECT COUNT(*) "+combinesql(),flatten(paramss)) count = c.fetchone()[0] if (not ancestry[-1].parent()) or (not count): if not ancestry[-1].parent(): #froms.pop() wheres.pop() paramss.pop() ancestry.pop() debug_ex(c,("SELECT n%d.rowid " % idx)+combinesql(), flatten(paramss)) for row in c.fetchall(): nodeid = get_top_node(row[0]) results.append(nodeid) break else: ancestry.append(ancestry[-1].parent()) idx += 1 froms.append(" JOIN nodes n%d " "ON n%d.parent_id=n%d.rowid" % (idx,idx-1,idx)) wheres.append(" AND n%d.type=?" % idx) paramss.append([ancestry[-1].label()]) results.reverse() return ordered_uniquify(results)
import sys from get_top_node import get_top_node if len(sys.argv) > 1: arg = sys.argv[1] else: arg = 3459 print(get_top_node(arg))