#!/usr/bin/python #------------------------------------------------------------------------------ # # BiBTeX -> Pajek converter # # Vladimir Batagelj, April 2006 #------------------------------------------------------------------------------ from string import strip,split,replace,lower,index from sys import argv,exit def run(workdir,input): global net, numRec line=' ' try: # open input DBLP file bib = open(workdir+input,'r') except IOError , (errno, strerror): print "I/O error(%s): %s" % (errno, strerror) exit() print 'dblpFile = ', workdir+input name = input[:input.rfind('.')] net = open(workdir+name+'.net','w') print 'pajekFile = ', workdir+name+'.net' nam = open(workdir+name+'.nam','w') print 'namesFile = ', workdir+name+'.nam' nam.write('*vertices\n') numRec = 0; numVer = 0; bibType = -1 numLine = 0; last = 0 while not last: line = bib.readline() if not line: line = '@'; last = 1 else: line = line.strip(); numLine += 1 ch = line[0] if ch == '[': numRec += 1 elif ch == 'K': key = line[2:] elif ch == 'Y': year = line[2:] elif ch == 'T': title = line[2:] elif ch == 'W': wtype = int(line[2:]) elif ch == 'A': author = line[2:] ok = key != '' elif ch == ']' and ok: numVer += 1 nam.write('%s \"%s\"\n' % (numVer,title)) print numVer, numRec, wtype, key, author elif ch == 'E': last = 1 if ch == ']' or ch == 'I' or ch == 'S': ok = 0; key = ''; year = '0'; title = ''; wtype = 0 net.write('\n') print ('# of vertices = %s, records = %s, lines = %s\n' % (numVer,numRec,numLine)) bib.close; net.close; nam.close # # run dblp2pajek # if __name__ == '__main__': # run it from command line if len(sys.argv) == 3: run(argv[1],argv[2]) else: print "Module Dblp2Pajek" print "Two arguments (WorkDir, BiBTeXfile) required to run !" exit() print else: # it is imported print "Module Bib2Pajek imported." print "To run, type: dblp2paj01.run('D:\\vlado\\BibTeX\\','lexicon.bib')" print "where 'lexicon.bib' is your input BiBTeX file" #- End -------------------------------------------------------------------------------