-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.py
146 lines (130 loc) · 3.92 KB
/
validate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
:author: Maximilian Golla
:contact: [email protected]
:version: 0.0.2, 2021-02-07
:description: Checks bibfile for common errors
'''
import sys, operator
def readfile(filename):
result = []
with open(filename, 'r') as inputfile:
for line in inputfile:
line = line.rstrip('\r\n')
result.append(line)
return result
def check_title(bib):
for line in bib:
if 'title' in line:
if '{{' not in line:
if 'booktitle' not in line:
print(line)
def check_pages(bib):
for line in bib:
if 'pages' in line:
if '--' not in line:
if 'pages = {},' not in line:
print(line)
def check_equal_signs(bib):
for line in bib:
if '=' in line:
if ' = ' not in line:
print(line)
def check_double_space(bib):
for line in bib:
if ' ' in line:
if not line.startswith(' '):
print(line)
def check_author_names(bib):
for line in bib:
if 'author' in line:
if ', ' not in line:
print(line)
def get_entries(kind, bib):
result = {}
for line in bib:
if kind in line:
if line in result:
result[line] += 1
else:
result[line] = 1
result = sorted(result.items(), key=operator.itemgetter(1), reverse=True)
once = []
for entry in result:
if entry[1] == 1:
once.append(entry[0])
else:
print("{} -- {}".format(entry[1], entry[0]))
once.sort()
for entry in once:
print("{} -- {}".format(1, entry))
def get_bibitem(bib, item_type):
results = []
item = ''
for line in bib:
item += line + '\n'
if line.startswith(item_type):
item = line + '\n'
if line.startswith('}'):
results.append([item])
item = ''
return results
def get_allitems(bib):
no_of_entries = 0
allitems = []
allitems += get_bibitem(bib, '@')
for bibitem in allitems:
no_of_entries += 1
print("Found {} entries in the provided bib file.".format(no_of_entries))
return allitems
def find_sorting_error(bibitems):
before = []
entries = 0
for item in bibitems:
for line in item:
firstline = line.split('\n')[0]
break
currentline = firstline.replace('@inproceedings{', '').replace('@article{', '').replace('@misc{', '').replace('@book{', '').replace('@techreport{', '').replace('@phdthesis{', '').replace('@incollection{', '').replace('@inbook{', '')
entries += 1
before.append(currentline)
#print("{}\t{}".format(entries, currentline))
print("###########\n\n\n")
after = sorted(before)
for k, v in enumerate(after):
if after[k].split('-')[0] != before[k].split('-')[0]:
print("Check", k, after[k], before[k])
def get_spacing(bib):
entries = 0
counter = 0
for line in bib:
counter += 1
if ' ' not in line:
if '%' not in line:
if '@' not in line:
if '}' not in line:
entries += 1
print(entries, counter, line)
def check_brokes_series(bib):
allitems = []
allitems += get_bibitem(bib, '@inproceedings')
for item in allitems:
if '@inproceedings' in str(item):
if 'series' not in str(item):
print(item)
print()
def main():
bib = readfile('max.bib')
#check_pages(bib)
#check_title(bib)
#check_equal_signs(bib)
#check_double_space(bib)
#check_author_names(bib)
#get_entries('publisher', bib)
#get_entries('series', bib)
#get_spacing(bib)
#allitems = get_allitems(bib)
#find_sorting_error(allitems)
check_brokes_series(bib)
if __name__ == '__main__':
main()