#!/usr/bin/python ########################################################################### # # Preprint - Preprint transformation of citation network # # Preprint.run(wdir,project,citefile,strongfile) # # import sys; wdir = r'D:\2007\work\WoS'; sys.path.append(wdir) # # # import Preprint; Preprint.run(wdir,'SN3','SN3cite.net','SN3strong.net') # reload(Preprint); Preprint.run(wdir,'SN3','SN3cite.net','SN3strong.net') # # Vladimir Batagelj, 29. December 2007 # ########################################################################### import string, os, shutil, sys, array, datetime def tranClosure(r,N): c = r for k in range(N): for u in range(N): for v in range(N): if c[u*N+k] and c[k*N+v]: c[u*N+v] = 1 return c def run(wdir,project,citefile,strongfile): global comlin t1 = datetime.datetime.now() if not comlin: print "\n*** Preprint - 0.1" print "by V. Batagelj, December 29, 2007 / December 29, 2007\n" print "started: "+t1.ctime()+"\n" workdir = wdir+'\\'+project+'\\' try: cit = open(workdir+citefile, 'r') except: print 'bad citation file:', workdir+citefile exit() try: stc = open(workdir+strongfile, 'r') except: print 'bad strong components file:', workdir+strongfile exit() acy = open(workdir+'acyclic.net', 'w') line = stc.readline() stcNver = eval(line[line.rfind(' '):]) while True: line = cit.readline() if line[0] == '*': break acy.write(line) citNver = eval(line[line.rfind(' '):]) acy.write('% Tranformed into Acyclic network by Preprint ' + datetime.datetime.now().ctime()+"\n") acy.write('*vertices '+str(citNver+stcNver)+'\n') preInd = [ 0 for i in range(stcNver+1) ] vtxInd = [ 0 for i in range(stcNver+1) ] vtxName = [ '' for i in range(stcNver+1) ] stVert = {}; nstc = 0; vtxStr = {} while True: line = stc.readline() if line[0] == '*': break # print line[:-1] nstc = nstc+1 name = line[line.find('"')+1:line.rfind('"')] stVert[name] = nstc preInd[nstc] = citNver + nstc vtxInd[nstc] = 0 vtxName[nstc] = name # print nstc, preInd[nstc], name r = array.array('i',[ 0 for i in range(stcNver*stcNver) ]) while True: line = stc.readline() if not line: break print line[:-1] row = filter(lambda s: s not in [''], line.split(' ')) u = eval(row[0]); v = eval(row[1]) r[(u-1)*stcNver+v-1] = 1 print "\nRelation" print range(stcNver+1)[1:] for i in range(stcNver): print i+1,list(r[i*stcNver:(i+1)*stcNver]) c = tranClosure(r,stcNver) print "\nTransitive closure" print range(stcNver+1)[1:] for i in range(stcNver): print i+1,list(c[i*stcNver:(i+1)*stcNver]) acyNver = 0 while True: line = cit.readline() if line[0] == '*': break acy.write(line) acyNver = acyNver + 1 name = line[line.find('"')+1:line.rfind('"')] if stVert.has_key(name): indx = stVert[name] vtxInd[indx] = acyNver vtxStr[acyNver] = indx print "\nStrong components vertices" for i in range(stcNver): ip = i+1 acy.write(str(citNver+ip)+' "='+vtxName[ip]+'"\n') print "%5d %6d %6d %s" % (ip,preInd[ip],vtxInd[ip],vtxName[ip]) acy.write(line) while True: line = cit.readline() if not line: break row = filter(lambda s: s not in [''], line.split(' ')) if len(row) > 1: u = eval(row[0]); v = eval(row[1]) uc = False; vc = False if vtxStr.has_key(u): indx = vtxStr[u] u = preInd[indx] uc = True if vtxStr.has_key(v): # indx = vtxStr[v] # v = preInd[indx] vc = True if not(uc and vc): acy.write(str(u)+' '+str(v)+'\n') for u in range(stcNver): for v in range(stcNver): if c[u*stcNver+v] > 0: acy.write(str(vtxInd[u+1])+' '+str(preInd[v+1])+'\n') cit.close(); stc.close(); acy.close() t2 = datetime.datetime.now() print "finished: "+t2.ctime() print "time used: ", t2-t1 print "***" # # Run Preprint # global comlin if __name__ == '__main__': comlin = True print "\n*** Preprint - 0.1" print "by V. Batagelj, December 29, 2007 / December 29, 2007\n" if len(sys.argv) == 5: for x in sys.argv[1:]: print x print "------------------------" wdir = sys.argv[1] project = sys.argv[2] citefile = sys.argv[3] strongfile = sys.argv[4] else: print "4 arguments required to run !" wdir = input("WoS directory = ") project = input("project subdirectory = ") citefile = input("citation file = ") strongfile = input("strong components file = ") run(wdir,project,citefile,strongfile) print a = input("Close console?") else: comlin = False print "Module Preprint imported.\n" print "To run, type:" print " Preprint.run(wdir,project,citefile,strongfile)" print "for example:" print " Preprint.run(r'D:\Vlado\work\Python\WoS','SN','SNcite.net','strong.net')\n" #- End -------------------------------------------------------------------------------