-
Notifications
You must be signed in to change notification settings - Fork 0
/
trbib2bib.py
102 lines (87 loc) · 2.74 KB
/
trbib2bib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# bib-napper tool, written by jason mars
import os
import sys
import re
#from urllib import FancyURLopener
# os.system("cat *.html >> ids.clump;");
final = []
def bibdecoder(filepath, conference):
title = ""
authors = ""
journal = ""
date = ""
pages = ""
keyflag = 0
count = 0
key = ""
keys = {""}
try:
fd = open(filepath, 'r')
filecontents_source = fd.readlines()
fd.close()
except:
print('Could not open file:' + filepath)
do_print = 0
for j in filecontents_source:
if len(j) < 2:
continue
if j[1] == "T":
title = "title = {" + j[2:-1].lstrip() + "}"
if j[1] == "J":
journal = "booktitle = {" + j[2:-1].lstrip() + "}"
if j[1] == "A":
if keyflag == 0:
authors = "author = {" + j[2:-1].lstrip()
list = j.split()
list.reverse()
key = key + list[0]
keyflag = 1
else:
authors = authors + " and " + j[2:-1].lstrip()
# Termination character.
if j[1] == "R":
do_print = 1
if j[1] == "P":
pages = "pages = {" + j[3:-1] + "}"
if j[1] == "D":
date = "year = {" + j[3:-1] + "}"
key = key + "-" + conference + "-" + j[2:-1].strip()
key = re.sub('[^A-Za-z0-9\-]+', '', key)
alpha = 97
# eliminate duplicate keys. This will keep adding characters until a unique key is found.
# Hopefully we won't have more than 26 papers from the same first author at the same conference.
# Otherwise we will start adding non-alpha suffixes. Who knows what's going to happen then.
duplicatekey = key
while (duplicatekey in keys):
duplicatekey = key + "-" + chr(alpha)
alpha = alpha+1
key = duplicatekey
keys.add(key)
if do_print:
sys.stdout.write("\n\n@inproceedings{")
sys.stdout.write(key.lower()+",\n")
sys.stdout.write(title+",\n")
sys.stdout.write(authors+"},\n")
sys.stdout.write(journal+",\n")
sys.stdout.write(date + ",\n")
sys.stdout.write(pages + "\n}")
do_print = 0
key = ""
keyflag = 0
title = ""
authors = ""
journal = ""
date = ""
count = count+1
print(count)
def main():
filepath = sys.argv[1]
if sys.argv[1:]:
filepath = sys.argv[1]
conference = sys.argv[2]
bibdecoder(filepath, conference)
else:
print("No input file")
sys.exit()
if __name__ == "__main__":
main()