summaryrefslogtreecommitdiff
path: root/parse.py
blob: e92743f87cd86a8772d6f9774dede6c3635d1dac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from globals import *
from Node import Node

def findNode(node, listOfNodes):
    for i in range(len(listOfNodes)):
        if node.text == listOfNodes[i].text:
            return i
    return len(listOfNodes)

# Each file is in the form
# <source> <destination> <weight>
def parse_input(filename):
    nodes = []
    links = []

    lines = open(filename, "r").readlines()

    for line in lines:
        # Parse each line and add to links array
        if (not line == "\n"): # Line isn't empty
            line_split = line.split(" ")
            currSrc = Node(text=line_split[0])
            currDest = Node(text=line_split[1])

            currSrc_index, currDest_index = (findNode(currSrc, nodes), findNode(currDest, nodes))
            appendSrc, appendDest = (False, False)
            if (currSrc_index == len(nodes)):
                appendSrc = True
            if (currDest_index == len(nodes)):
                appendDest = True
            if (appendSrc):
                nodes.append(currSrc)
                currSrc_index = len(nodes)-1
            if (appendDest):
                nodes.append(currDest)
                currDest_index = len(nodes)-1

            if (len(line_split) > 2):
                connection_description = "".join([str(x).strip() + " " for x in line_split[3::]])
            else:
                connection_description = ""
            # Append the connection to links
            links.append((nodes[currSrc_index], nodes[currDest_index], float(line_split[2]), connection_description))

    return [nodes, links]