예제 #1
0
    def insertRecord(
        self, record
    ):  # recieves a line of the text file and is used to build the graph
        tokens = record.split(',')  # splits the line via the split method
        if len(tokens) != 6:  # check regarding whether the line has 6 commas
            print('Incorrect Song Record')
        song = tokens[1]  # assings the song
        artist = tokens[2]  # assings the artist
        neighbors = tokens[5][:len(tokens[5]) - 1].split(
            ';')  # via the split method again, it creates a
        #  list of coArists AKA neighbors

        for i in range(
                0, len(neighbors)
        ):  # This for loop checks if an artist is listed in the list neighbors
            # this prevents repeated edges when creating the graph
            if artist in neighbors:
                neighbors.remove(artist)

        currentVert = None  # inializion of the current vertice
        if artist in self.vertList:  # checks if artist is in the the graph
            currentVert = self.vertList[
                artist]  # the vertice is set to the artist at that postion
        else:
            currentVert = Vertex(artist)  # creation of vertex
            self.vertList[
                artist] = currentVert  # Vertex is conneccted to the node
            self.numVertices += 1  # number of vertices is incremented
        ## insert info  for this artist

        arr = []
        currentVert.addsong(
            song)  # the song is added to the array of songs in the Graph file
        for nb in neighbors:  # iterates through the array neigbors and connects the neighbors together bidirectionally
            nbVert = None
            if nb in self.vertList:
                nbVert = self.vertList[nb]
            else:
                nbVert = Vertex(nb)
                self.vertList[nb] = nbVert
                self.numVertices += 1

            currentVert.addNeighbor(nbVert)
            nbVert.addNeighbor(currentVert)