Example #1
0
def parse_to_network(input_stream) -> nx.DiGraph:
    """Parseja i retorna el graph."""
    lexer = EnquestesLexer(input_stream)
    token_stream = CommonTokenStream(lexer)
    parser = EnquestesParser(token_stream)
    tree = parser.root()

    visitor = EnquestesVisitor()
    return visitor.visit(tree)
Example #2
0
    def visit(self):
        visitor = EnquestesVisitor()
        visitor.visitRoot(self.tree)  # save questions and things

        # add base nodes
        for i, question in zip(visitor.question_ids, visitor.questions):
            self.generator.add_node_with_content(i, str(question), "question")

        for i, answer in zip(visitor.answer_ids, visitor.answers):
            self.generator.add_node_with_content(i, answer, "answer")

        # add the end node
        self.generator.add_node("END", "end")

        # add poll relationships
        for poll_id, item_list in zip(visitor.poll_ids, visitor.poll_item_list):
            self.generator.add_node(poll_id, "poll")
            items_ids = str(item_list).split()
            items = []
            for i_id in items_ids:
                items.append(visitor.item_pairs[i_id])

            self.generator.add_poll_edge(str(poll_id), items[0][0])
            for i in range(1, len(items)):
                self.generator.add_poll_edge(items[i - 1][0], items[i][0])
            self.generator.add_poll_edge(items[-1][0], 'END')

        # add item edges
        for pair in visitor.item_pairs.items():
            self.generator.add_item_edge(pair[1][0], pair[1][1], pair[0])

        # add alternative edges
        for alt in visitor.alternative_pairs.items():
            questionId = visitor.item_pairs[alt[0]][0]
            for a in alt[1]:
                self.generator.add_alt_edge(questionId, visitor.item_pairs[a[1]][0], a[0])
Example #3
0
import sys
from antlr4 import *
from EnquestesLexer import EnquestesLexer
from EnquestesParser import EnquestesParser
from antlr4.InputStream import InputStream
from EnquestesVisitor import EnquestesVisitor

if len(sys.argv) > 1:
    input_stream = FileStream(sys.argv[1], encoding='utf-8')
else:
    input_stream = InputStream(input('? '))

lexer = EnquestesLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = EnquestesParser(token_stream)
tree = parser.root()

visitor = EnquestesVisitor()
visitor.visit(tree)
Example #4
0
# a un fitxer extern
def crearEDRespostes(graph, dirname):
    dict = {}
    roots = [n for n, d in graph.in_degree() if d == 0]
    for r in roots:
        dict[r] = {}
        nodes = nx.get_node_attributes(graph, 'enquesta')
        nodesEnquesta = [k for k in nodes.keys() if nodes[k] == r]
        for p in nodesEnquesta:
            dict[r][p] = {}
    botDir = dirname[:len(dirname) - 2] + 'bot/'
    pickle.dump(dict, open(botDir + 'data.p', 'wb'))


input_stream = FileStream(sys.argv[1], encoding='utf-8')

lexer = EnquestesLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = EnquestesParser(token_stream)
ast = parser.root()

# path a test.graph.py
dirname, filename = os.path.split(os.path.abspath(__file__))

visitor = EnquestesVisitor()
# visitor que retorna un graf creat a partir de l'AST
graph = visitor.visit(ast)

draw_graph_and_pickle(graph, dirname)
crearEDRespostes(graph, dirname)
Example #5
0
from EnquestesLexer import EnquestesLexer
from EnquestesParser import EnquestesParser
from antlr4.InputStream import InputStream
from EnquestesVisitor import EnquestesVisitor
from testDrawGraph import drawGraph
from os import path
if len(sys.argv) > 1:
    input_stream = FileStream(sys.argv[1])
else:
    input_stream = InputStream(input('? '))

lexer = EnquestesLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = EnquestesParser(token_stream)
tree = parser.botGraph()
visitor = EnquestesVisitor()
G = visitor.visit(tree)
idEnquesta = visitor.getStartNode()
nx.write_gpickle(
    G, "../GeneratedData/GeneratedEnquestes/" + idEnquesta + ".pickle")
pathQuizzesIDs = "../GeneratedData/0QuizzesIDs.pickle"
if not path.exists(pathQuizzesIDs):
    pickleOut = open(pathQuizzesIDs, "wb")
    quizzesIDs = {idEnquesta}
    pk.dump(quizzesIDs, pickleOut)
    pickleOut.close()
else:
    pickleQuizzesIDs = open(pathQuizzesIDs, "rb")
    quizzesIDs = pk.load(pickleQuizzesIDs)
    pickleQuizzesIDs.close()
    pickleOut = open(pathQuizzesIDs, "wb")
Example #6
0
from EnquestesParser import EnquestesParser
from antlr4.InputStream import InputStream
from EnquestesVisitor import EnquestesVisitor

if len(sys.argv) > 1:
    input_stream = FileStream(sys.argv[1], encoding='utf-8')
else:
    input_stream = InputStream(input('? '))

lexer = EnquestesLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = EnquestesParser(token_stream)

tree = parser.root()

visitor = EnquestesVisitor()
graph = visitor.visit(tree)

pos = nx.circular_layout(graph)
nx.draw_networkx_nodes(graph,
                       pos,
                       cmap=plt.get_cmap('jet'),
                       node_color='lightblue',
                       node_size=500)

nx.draw_networkx_labels(graph, pos)

edge_labels = nx.get_edge_attributes(graph, 'tag')

nx.draw_networkx_edge_labels(graph,
                             pos,
Example #7
0
import sys
import networkx as nx
import matplotlib.pyplot as plt
from antlr4 import *
from EnquestesLexer import EnquestesLexer
from EnquestesParser import EnquestesParser
from antlr4.InputStream import InputStream
from EnquestesVisitor import EnquestesVisitor

if len(sys.argv) > 1:
    input_stream = FileStream(sys.argv[1], encoding='utf-8')
else:
    input_stream = InputStream(input('? '))

lexer = EnquestesLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = EnquestesParser(token_stream)
tree = parser.chatboot()
print(tree.toStringTree(recog=parser))

visitor = EnquestesVisitor()
arbol = visitor.visit(tree)

nx.write_gpickle(arbol, "arbol.pickle")
pos = nx.circular_layout(arbol)
colors = [arbol[u][v]['color'] for u, v in arbol.edges()]
nx.draw(arbol, pos, with_labels=True, edge_color=colors)
plt.show()