-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
155 lines (141 loc) · 5.98 KB
/
parser.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/python
import sys, os
import re
import urllib.request
class key_values:
def __init__(self,citekey,values):
self.citekey = citekey
self.values = values
def get_data(datastring):
attributevalue = []
start = 0
stop = datastring.find("@")
if stop > 0:
datastring=datastring[start:stop]
else:
datastring = datastring[start:]
stop = datastring.rfind("}")
datastring=datastring[start:stop] #key,att1={txt1},att2={txt2}.... last , can be present or not
start = datastring.find("{")
stop = datastring.find(",")
citekey = datastring[start+1:stop]
#from now starts attribute extrations
cont = True #we suppose to have almost one attribute
while cont:
datastring = datastring[stop+1:]
start = 0
stop = datastring.find("}")
analyzedstring = datastring[start:stop]
analyzedstring = analyzedstring.strip();
stop = analyzedstring.find("=")
key = analyzedstring[start:stop]
key = key.strip()
start = analyzedstring.find("{")
value = analyzedstring[start+1:]
value = value.strip()
attributevalue.append(key)
attributevalue.append(value)
stop = datastring.find(",")
if stop == -1:
cont = False
else:
stop = datastring.find("}")
datastring = datastring[stop + 1:]
stop = datastring.find(",")
#print(len(attributevalue))
returnvalue = key_values(citekey, attributevalue)
return returnvalue
bibfile = sys.argv[1]
f = open(bibfile, "r")
recordstring = f.read()
classes = list(dict.fromkeys(re.findall('@(.*){', recordstring)))
recordstring=recordstring.replace('\n',' ')
#clean from {\
recordstringcopy=recordstring
recordstring=""
idx = 0
graphdepth = 0
while idx <= len(recordstringcopy)-2:
simplycopy = True
if (recordstringcopy[idx] == "{"):
graphdepth = graphdepth+1
if (recordstringcopy[idx] == "}"):
graphdepth = graphdepth-1
skipcopy = False
if ( ( (recordstringcopy[idx] == "{") & (graphdepth >= 3) ) or
( (recordstringcopy[idx] == "}") & (graphdepth >= 2) ) ):
skipcopy = True
if recordstringcopy[idx] == "{":
recordstring = recordstring + "__open_graph__"
if recordstringcopy[idx] == "}":
recordstring=recordstring+"__close_graph__"
if ((recordstringcopy[idx] == "@") & (graphdepth >= 1)):
skipcopy = True
recordstring = recordstring + "__at__"
if skipcopy == False:
recordstring=recordstring+recordstringcopy[idx]
idx = idx+1
# recordarray=recordstring.split"
for classelementname in classes:
fileToWrite = classelementname+".tex"
stringToWrite = ""
# To generate, if present
# Title
# Author(s)
# per Journal: journal + publisher
# DOI
# abstract
# citeref
attributestoprint = ["title", "author", "doi", "abstract"]
if classelementname == "article":
attributestoprint = ["title", "author", "journal", "publisher", "doi", "abstract"]
elementnametosearch = "@" + classelementname + "{"
classelementstarts = [i for i in range(len(recordstring)) if recordstring.startswith(elementnametosearch, i)]
for classelementstart in classelementstarts:
#print(classelementname +" "+str(classelementstart))
substringstartswithelement = recordstring[classelementstart+1:]
stoptoken=substringstartswithelement.find("@")
if stoptoken > 1:
substringstartswithelement = substringstartswithelement[:stoptoken]
keyvalue=get_data(substringstartswithelement)
#print("key "+keyvalue.key)
for idxatt in attributestoprint:
for idx in range(int(len(keyvalue.values)/2)):
#print(idx)
if (keyvalue.values[2*idx] == idxatt):
# reinsert graph
txt = keyvalue.values[2 * idx + 1]
txt = txt.replace("__open_graph__", "{")
txt = txt.replace("__close_graph__", "}")
txt = txt.replace("__at__", "@")
txt = txt.replace("_", "\\_")
txt = txt.replace("&", "\\&")
attributetitletoprint = idxatt.capitalize()
if attributetitletoprint == "Doi":
attributetitletoprint = "DOI"
if attributetitletoprint == "Author":
if txt.find("and")>1:
attributetitletoprint = "Authors"
if txt.find(",")>1:
attributetitletoprint = "Authors"
stringprint = False
if idxatt == "title":
stringprint = True
httppage = "https://openportal.isti.cnr.it/results?qv=\"" + txt + "\""
stringToWrite = stringToWrite+"\\textbf{"+attributetitletoprint+"}: \href{" + \
httppage + "}{" + txt + "}\\\\ \n"
#print(httppage)
#contents = urllib.request.urlopen("\""+httppage+"\"").read()
#contents = urllib.request.urlopen("https://openportal.isti.cnr.it/results?qv=Venturino").read()
if idxatt == "doi":
stringprint = True
stringToWrite = stringToWrite+"\\textbf{"+attributetitletoprint+"}: \doix{"+ txt +"}\\\\ \n"
if idxatt == "abstract":
stringprint = True
stringToWrite = stringToWrite+"\\textbf{"+attributetitletoprint+"}: \\textit{"+ txt +"}\\\\ \n"
if stringprint == False:
stringToWrite = stringToWrite + "\\textbf{" + attributetitletoprint + "}: " + txt + "\\\\ \n"
#add cite key
stringToWrite = stringToWrite + "\cite{"+keyvalue.citekey+"}\\\\ \\vspace{5mm} \\\\ \n"
with open(fileToWrite, 'w') as f:
f.write(stringToWrite)