#!/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)) 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+'records.txt','w') print 'pajekFile = ', workdir+'records.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],argv[2]) 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: bib2paj01.run('D:\\vlado\\BibTeX\\','lexicon.bib')" print "where 'lexicon.bib' is your input BiBTeX file" #- End -------------------------------------------------------------------------------