def graph_from_conceptdb(output='conceptdb.graph'): import conceptdb from conceptdb.justify import Reason conceptdb.connect('conceptdb') outfile = open(output, 'w') counts = defaultdict(int) for reason in Reason.objects: reason_name = '/c/%s' % reason.id if reason.target == '/sentence/None': continue for factor in reason.factors: counts[factor] += 1 counts[reason.target] += 1 print 'counted' for reason in Reason.objects: reason_name = '/c/%s' % reason.id if reason.target == '/sentence/None': continue polar_weight = 1.0 if reason.polarity == False: polar_weight = -0.5 factor_counts = [counts[factor] for factor in reason.factors] if min(factor_counts) <= 3: continue if counts[reason.target] <= 3: continue for factor in reason.factors: output_edge(outfile, factor, reason_name, weight=reason.weight, dependencies=reason.factors) output_edge(outfile, reason_name, reason.target, weight=polar_weight) outfile.close() print "Done building the file." return graph_from_file(output)
def graph_from_conceptnet(output='conceptnet'): import conceptdb from conceptdb.justify import Reason conceptdb.connect('conceptdb') bn = BeliefNetwork(output=output) for reason in Reason.objects: reason_name = '/c/%s' % reason.id if reason.target == '/sentence/None': continue print len(bn.nodes), reason_name bn.add_conjunction(reason.factors, reason_name, reason.weight) bn.add_edge(reason_name, reason.target, 1.0) return bn