-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualise.py
More file actions
executable file
·68 lines (62 loc) · 2.75 KB
/
visualise.py
File metadata and controls
executable file
·68 lines (62 loc) · 2.75 KB
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
# A python script to visualise the author relationships in a bibtex database
# Very basic bibtex parsing, but no dependencies (besides using graphvis for graphing the dot file)
# Author: Glen Robertson
# Date: 4 Oct 2011
BIBTEX_FILENAME = "Database.bib"
DOT_FILENAME = "graph.dot"
import re
bibtexFile = open(BIBTEX_FILENAME, 'r')
contents = bibtexFile.read()
bibtexFile.close()
edges = []
# Remove special character notation like in: "Andrea Fre{\ss}mann and Stefanie Br{\"u}ninghaus"
contents = re.sub(r"(\{\\.)(.)(\})", r"\2", contents)
# Split out each bibtex entry (at each @ symbol, will cause problems if @ in an entry)
# Probably would be better to match brackets but that still would fail for quoted brackets.
entries = re.findall("@\w*\{(\w*),([^@]*)", contents)
for entry in entries:
try:
# entry[0] is the bibtexkey
# entry[1] is the content for that entry
# Find authors in the form: author = {Andrea Fresmann and Stefanie Bruninghaus}
authors = re.findall("author = \{([^\}]+)\}", entry[1])
# Should return a list with one element always, so [0]
if len(authors) > 0:
authors = authors[0]
else:
# Try using editor as author
authors = re.findall("editor = \{([^\}]+)\}", entry[1])
if len(authors) > 0:
authors = authors[0]
else:
raise Exception("Cannot find author or editor in bibtex file")
# Split out individual authors to a list
# Convert all whitespace to a single space
authors = re.sub("\s+", " ", authors)
authors = re.split(" and ", authors)
except:
# Debugging
print("Failed on " + entry[0])
print(" Entry[1] was: " + entry[1])
continue
# Convert from "Andrea Fresmann" to "Fresmann, A.". Avoid single-word authors.
for i in range(len(authors)):
if " " in authors[i] and not "," in authors[i]:
pos = authors[i].rfind(" ")
authors[i] = authors[i][pos+1:] + ", " + authors[i][:pos]
# Get the first letter of the firstname
pos = authors[i].find(",")
authors[i] = authors[i][:pos+3] + "."
# Record edges
for i in range(len(authors)):
for j in range(i+1, len(authors)):
edges.append((authors[i], authors[j]))
# Write out the dot file for graphvis
dotFile = open(DOT_FILENAME, 'w')
dotFile.write("/* Warning: This file is generated by 'visualise.py' and will be overwritten! */\r\n")
dotFile.write("/* %d entries found in database */\r\n" % len(entries))
dotFile.write("graph G {\r\n")
for edge in edges:
dotFile.write('"{0}" -- "{1}";\r\n'.format(edge[0], edge[1]))
dotFile.write("}\r\n")
dotFile.close()