#!/usr/bin/python import sys import string import re bibData = string.split(sys.stdin.read(),'\n') def FixAuthors(authList): result = [] authRE = re.compile('([a-zA-Z0-9]+), *([a-zA-Z0-9\.]+)') for a in authList: m = authRE.search(a) if (m): result.append(m.group(2) + ' ' + m.group(1)) else: result.append(a) return string.join(result,' and ') def FixPub(pubString): pubRE = re.compile('([a-zA-Z0-9 ]+) *,') m = pubRE.search(pubString) transRE = re.compile('IEEE *[Tt]ransactions *[Oo]n') mm = transRE.search(pubString) if (mm): return string.join(['IEEE Transactions on',m.group(1)]) else: return m.group(1) def GetYear(pubString): yearRE = re.compile('.+, *([0-9]+)') m = yearRE.search(pubString) if (m): return m.group(1) yearRE = re.compile('([0-9]+) *$') m = yearRE.search(pubString) if (m): return m.group(1) else: return '' def FixPages(pagesString): pageRE = re.compile('[^:]+: *([- 0-9]+)') m = pageRE.search(pagesString) return string.replace(m.group(1),' ','') def GetVolume(pubString): volRE = re.compile('.+ Volume: *([0-9]+)') m = volRE.search(pubString) if (m): return m.group(1) else: return '' def GetIssue(pubString): issRE = re.compile('.+ Issue: *([0-9]+)') m = issRE.search(pubString) if (m): return m.group(1) else: return '' def GetRefName(authorString,year): refRE = re.compile('([A-Z][a-z]+) *and') m = refRE.search(authorString) if (m == None): refRE = re.compile('([A-Z][a-z]+) *$') m = refRE.search(authorString) return m.group(1) + '_' + year def GetMonth(pubString): monthRE = re.compile('(January)|(Jan)|(February)|(Feb)|(March)|(Mar)|(April)|(Apr)|(May)|(June)|(Jun)|(July)|(Jul)|(August)|(Aug)|(September)|(Sep)|(October)|(Oct)|(November)|(Nov)|(December)|(Dec)') m = monthRE.search(pubString) if (m): rr = filter(None,m.groups()) return rr[0] else: return '' paperTitle = bibData[0] authors = FixAuthors(string.split(bibData[1],';')) pubTitle = FixPub(bibData[2]) year = GetYear(bibData[2]) pages = FixPages(bibData[3]) refName = GetRefName(authors,year) volume = GetVolume(bibData[2]) issue = GetIssue(bibData[2]) month = GetMonth(bibData[2]) def ConferenceEntry(refName,authors,paperTitle,pubTitle,pages,year): print '@InProceedings{' + refName + ',' print ' author = \"' + authors + '\",' print ' title = \"' + paperTitle + '\",' print ' booktitle = \"' + pubTitle + '\",' if (volume): print ' volume = \"' + volume + '\",' print ' pages = \"' + pages + '\",' print ' year = \"' + year + '\",' print ' note = \"\"' print '}' def JournalEntry(refName,authors,paperTitle,pubTitle,pages,year,volume,issue,month): print '@Article{' + refName + ',' print ' author = \"' + authors + '\",' print ' title = \"' + paperTitle + '\",' print ' journal = \"' + pubTitle + '\",' print ' year = \"' + year + '\",' print ' volume = \"' + volume + '\",' print ' number = \"' + issue + '\",' print ' pages = \"' + pages + '\",' print ' month = \"' + month + '\",' print ' note = \"\"' print '}' if (sys.argv[1] == 'conference'): ConferenceEntry(refName,authors,paperTitle,pubTitle,pages,year) else: JournalEntry(refName,authors,paperTitle,pubTitle,pages,year,volume,issue,month)