Exemple #1
0
class Graph:

    def __init__(self, graphfile="graph.txt"):
        self.nodelist = []
        self.namelookup = dict()
        self.idlookup = dict()
        self.adjmatrix = None
        self.load_graph(graphfile)
        self.print_stats()



    def load_graph(self, filename):
        with open(filename, 'r') as graphfile: 

            if graphfile == None:
                sys.exit("Graph File Non-Existent, ensure file exists.")

            nodecount = 0


            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        if matchobj.group('nodeone') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodeone')
                            self.idlookup[matchobj.group('nodeone')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodeone')])
                            nodecount += 1

                        if matchobj.group('nodetwo') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodetwo')
                            self.idlookup[matchobj.group('nodetwo')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodetwo')])
                            nodecount += 1


            self.adjmatrix = AdjMatrix(nodecount)

            graphfile.seek(0)
            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        self.adjmatrix.set_adjvalue(self.idlookup[matchobj.group('nodeone')], self.idlookup[matchobj.group('nodetwo')], float(matchobj.group('adjval')))



    def print_stats(self):
        print ""
        print self.nodelist
        print self.namelookup
        print self.idlookup
        self.adjmatrix.print_adjmatrix
        print ""
Exemple #2
0
class TSPGraph:

    def __init__(self):
        self.nodelist = []
        self.idlookup = []
        self.adjmatrix = None
        self.read_graph()
        self.generate_adjmatrix()



    def read_graph(self):
        for line in sys.stdin:
            # May restore index variable. This area kind of hacky.
            nodeid, xcoord, ycoord = list(map(float, line.split()))
            self.nodelist.append([len(self.nodelist), float(xcoord), float(ycoord)])
            self.idlookup.append(int(nodeid))



    def generate_adjmatrix(self):
        self.adjmatrix = AdjMatrix(len(self.nodelist))

        # Try to fix the redundant assignments later.
        for n in self.nodelist:
            for m in self.nodelist[n[0]:len(self.nodelist)]:
                if m == n:
                    self.adjmatrix.set_adjvalue(n[0], m[0], 0)
                else:
                    deltax = n[1] - m[1]
                    deltay = n[2] - m[2]
                    distance = math.sqrt(math.pow(deltax, 2) + math.pow(deltay, 2))
                    self.adjmatrix.set_adjvalue(n[0], m[0], distance)
Exemple #3
0
def fromListToAdjMatrix(_list: List):
    adjMatrix = AdjMatrix(_list.tops)
    for i in range(0, _list.tops):
        for j in range(0, _list.tops):
            if _list.matrix[i][j] != 0:
                adjMatrix.matrix[i][_list.matrix[i][j] - 1] = 1
            #else:
            #adjMatrix.matrix[i][_list.matrix[i][j]-1] = 0
    return adjMatrix
Exemple #4
0
    def generate_adjmatrix(self):
        self.adjmatrix = AdjMatrix(len(self.nodelist))

        # Try to fix the redundant assignments later.
        for n in self.nodelist:
            for m in self.nodelist[n[0]:len(self.nodelist)]:
                if m == n:
                    self.adjmatrix.set_adjvalue(n[0], m[0], 0)
                else:
                    deltax = n[1] - m[1]
                    deltay = n[2] - m[2]
                    distance = math.sqrt(math.pow(deltax, 2) + math.pow(deltay, 2))
                    self.adjmatrix.set_adjvalue(n[0], m[0], distance)
Exemple #5
0
    def load_graph(self, filename):
        with open(filename, 'r') as graphfile: 

            if graphfile == None:
                sys.exit("Graph File Non-Existent, ensure file exists.")

            nodecount = 0


            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        if matchobj.group('nodeone') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodeone')
                            self.idlookup[matchobj.group('nodeone')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodeone')])
                            nodecount += 1

                        if matchobj.group('nodetwo') not in self.idlookup:
                            self.namelookup[nodecount] = matchobj.group('nodetwo')
                            self.idlookup[matchobj.group('nodetwo')] = nodecount
                            self.nodelist.append([nodecount, matchobj.group('nodetwo')])
                            nodecount += 1


            self.adjmatrix = AdjMatrix(nodecount)

            graphfile.seek(0)
            for line in graphfile:
                if line.strip()[0] == '#':
                    continue
                else:
                    matchobj = re.match(r"(?P<nodeone>\w+) (?P<nodetwo>\w+) (?P<adjval>\d+)", line)
                    if matchobj != None:
                        self.adjmatrix.set_adjvalue(self.idlookup[matchobj.group('nodeone')], self.idlookup[matchobj.group('nodetwo')], float(matchobj.group('adjval')))
def create_graph(text, filename):
    f = open(filename)
    basetext = f.read().lower()
    from textmodel import Text
    btext = Text(basetext)
    #btokens = nltk.word_tokenize(basetext)
    btokens = nltk.regexp_tokenize(basetext, "[\w']+")
    btext.tokens = btokens
    if text is None:
        print("Grrrrrrrrrr")

    #text.tokens = text1
    #print ("..........",len(text.tokens))
    txt = list(text.tokens)
    poslist = list()
    #print ("..............", len(txt))
    #print text
    temp = {}

    t = []
    #creating poslist for the terms
    for x in txt:
        #tmptokens = nltk.word_tokenize(txt[x])
        tmptokens = nltk.regexp_tokenize(x, "[\w']+")
        #print(x,tmptokens)
        #print(tmptokens)
        #tmptokens = nltk.regexp_tokenize(text[x],"[\w']+")
        if len(tmptokens) == 1:
            pos = list(get_positions(btext.tokens, tmptokens[0], txt))
        elif len(tmptokens) > 1:
            pos = list(get_phrasepos(btext.tokens, x, txt))
        else:
            pos = []
            t.append(x)
        if len(pos) > 0:
            if pos[0][0] in temp.keys():
                for i in range(len(pos)):
                    if pos[i] not in temp[pos[0][0]]:
                        temp[pos[0][0]].append(pos[i])
            else:
                temp[pos[0][0]] = pos
            if pos[0][0] != x:
                #print ("removed",x," | ",pos[0][0])
                t.append(x)
    #removing unneccessary sub-lists ie "linked" from "linked list"
    k = []
    for x in temp.keys():
        for y in temp.keys():
            if x != y:
                if x in y:
                    tmpx = nltk.word_tokenize(x)
                    tmpy = nltk.word_tokenize(y)
                    fx = 0
                    fy = 0
                    for i in range(len(tmpy)):
                        if tmpy[fy] == tmpx[fx]:
                            fy += 1
                            fx += 1
                        else:
                            fy += 1
                        if fx == len(tmpx):
                            if len(temp[x]) == len(temp[y]):
                                #print("delete: ",x," | ",y)
                                if x not in k:
                                    #print(x)
                                    k.append(x)
                            break
    #print("k : ",k)
    for i in k:
        del temp[i]
        txt.remove(i)
    si = {}
    for i in temp.keys():
        si[i] = len(temp[i])
    r = []
    for i in temp.keys():
        tmpx = nltk.word_tokenize(i)
        if len(tmpx) > 1:
            for j in temp:
                if i != j:
                    if i in j:
                        si[i] -= si[j]
                        r.append(j)
##    for i in j:
##        si[i]=0

    for i in temp.keys():
        tmpx = nltk.word_tokenize(i)
        if len(tmpx) == 1:
            for j in temp:
                if i != j:
                    if i in j:
                        si[i] -= si[j]
    r = []
    for i in temp.keys():
        if si[i] == 0:
            #print ("to be removed: ",i)
            r.append(i)
    for i in r:
        txt.remove(i)
        del temp[i]
    for i in t:
        txt.remove(i)
    for i in temp.keys():
        poslist.append(temp[i])
##    #removing unneccessary sub-lists ie "linked" from "linked list"
##    d=[]
##    for x in range(len(txt)):
##        tmptokens = nltk.word_tokenize(txt[x])
##        if len(tmptokens)>1:
##            for y in range(len(tmptokens)):
##                if tmptokens[y] not in stop:
##                    if tmptokens[y] in txt:
##                        u=0
##                        flag1=0
##                        flag2=0
##                        flag=0
##                        w=0
##                        z=0
##                        for u in range(len(poslist)):
##                            if len(poslist[u])>1:
##                                if poslist[u][0][0]==tmptokens[y] and flag1==0:
##                                    flag1=1
##                                    flag+=1
##                                    w=u
##                                if poslist[u][0][0]==txt[x] and flag2==0:
##                                    flag2=1
##                                    flag+=1
##                                    z=u
##                            if flag==2:
##                                break
##                        if flag1==1 and flag2==1:
##                            #print(poslist[u][0][0]," : ",len(poslist[u]),poslist[v][0][0]," : ",len(poslist[v]))
##                            if len(poslist[w])==len(poslist[z]):
##                                print("del: ",poslist[w][0][0],len(poslist[w])," ",txt[x],len(poslist[z]))
##                                d.append(poslist[w][0][0])
##                                del poslist[w]
##    for i in d:
##        txt.remove(i)

#mlist is a merged list for merging the created positions in sorted order
    mlist = list()
    if len(poslist) > 1:
        mlist = merge(poslist[0], poslist[1])
    x = 2
    while x < len(poslist):
        if len(poslist[x]) != 0:
            #print len(poslist[x]), poslist[x]
            mlist = merge(mlist, poslist[x])
        x += 1

    t = []
    for z in txt:
        count = 0
        for i in range(len(mlist)):
            if z == mlist[i][0]:
                count += 1


##        if count==1:
##            print ("1: ",z)
        if count == 0:
            #print ("0: ",z)
            t.append(z)
    for i in t:
        txt.remove(i)

    # Now create the adjacency matrix for the graph
    adjmatrix = list()
    txt.sort()
    text.tokens = txt
    #print ("txt::",txt)
    from adjmatrix import AdjMatrix
    amatrix = AdjMatrix()
    amatrix.createMatrix(txt, mlist, False)
    #text.append(filename)
    #print text
    #amatrix.addFilename(filename,text)
    #print(txt)
    return amatrix