Exemple #1
0
def wordify(concept):
    return concept[len('/c/en/'):].replace('_', ' ')

if __name__ == '__main__':
    args = docopt(__doc__)
    graph = AjguDB(args['--database'], logging=True)
    if args['conceptnet'] and args['load']:
        edges = chunks_to_rows(gzip_to_chunks(args['FILENAME']))
        for edge in edges:
            _, relation, start, end, _ = edge
            # index only cool concepts
            if COOL.search(start) and COOL.search(end):
                with graph.transaction():
                    # get or create start
                    start = Vertex(ref=start, concept=wordify(start))
                    new, start = graph.get_or_create(start)
                    if new:
                        graph.index(start, start['concept'])
                    # get or create end
                    end = Vertex(ref=end, concept=wordify(end))
                    new, end = graph.get_or_create(end)
                    if new:
                        graph.index(end, end['concept'])

                    edge = start.link(end, relation=relation)
                    graph.save(edge)

    if args['conceptnet'] and args['search']:
        query = args['QUERY']
        query = ' '.join(query)
        for uid, score in graph.like(query):