#!/usr/bin/python #------------------------------------------------------------------------------ # # BiBTeX -> Pajek converter # # Vladimir Batagelj, April 2006 #------------------------------------------------------------------------------ from string import strip,split,replace,lower,index from sys import argv,exit def processRecord(bibRec,bibType): global net, numRec numRec += 1 net.write('%s : %s > %s\n' % (numRec,bibType,bibRec)) parts = bibRec.split('=') desKeys = ['head']; desVals = [] for i in range(len(parts)): part = strip(parts[i]); j = part.rfind(',') if j < 0: j = len(part)-1 desVal = strip(part[:j]); s = desVal[0] if (s == '"' or s == '{'): desVal = desVal[1:-1] desVals.append(desVal) desKeys.append(lower(strip(part[j+1:]))) desVals[0] = desVals[0].split('{')[1] for i in range(len(desVals)): net.write(' %s = %s\n' % (desKeys[i],desVals[i])) def run(workdir,input): global net, numRec pubs=['article','book','booklet','inbook','incollection', 'inproceedings','manual','mastersthesis','misc','phdthesis', 'proceedings','techreport','unpublished'] line=' ' try: # open input BiBTeX file bib = open(workdir+input,'r') except IOError , (errno, strerror): print "I/O error(%s): %s" % (errno, strerror) exit() print 'bibFile = ', workdir+input net = open(workdir+'parts.txt','w') print 'pajekFile = ', workdir+'parts.txt' numRec = 0; bibRec = ''; bibType = -1 numLine = 0; last = 0 while not last: line = bib.readline() if not line: line = '@'; last = 1 else: line = line.strip(); numLine += 1 if line != '': if line[0] == '@': # start of record if bibType >= 0: processRecord(bibRec,bibType) bibRec = '' head = line.split('{') head = replace(head[0],'@','') try: bibType = pubs.index(lower(head)) except ValueError: bibType = -1 print numLine, head if bibType >= 0: bibRec += line net.write('\n') net.write('*records = %s lines = %s\n' % (numRec,numLine)) bib.close; net.close # # run bib2pajek # if __name__ == '__main__': # run it from command line if len(sys.argv) == 3: run(argv[1]) else: print "Module Bib2Pajek" print "Two arguments (WorkDir, BiBTeXfile) required to run !" exit() print else: # it is imported print "Module Bib2Pajek imported." print "To run, type: bib2paj02.run('D:\\vlado\\BibTeX\\','lexicon.bib')" print "where 'lexicon.bib' is your input BiBTeX file" #- End -------------------------------------------------------------------------------