forked from TurkuNLP/list-of-publications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonal_lop.py
119 lines (86 loc) · 4.36 KB
/
personal_lop.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
import six
assert six.PY3, "Run me with python3"
import sys
import bibtexparser
import argparse
import collections
PType=collections.namedtuple("Ptype","label,bibtypes,section")
nocites="journal bookc conf phd coed techrep".split()
pub_types="article incollection inproceedings phdthesis proceedings techreport".split() #can also be incollection,inbook
ptypes=[PType("journal",["article"],"Co-authored journal articles (OKM class A1)"),
PType("bookc",["incollection"],"Co-authored peer-reviewed book chapters (OKM class A3)"),
PType("conf",["inproceedings"],"Co-authored articles in peer-reviewed conference proceedings (OKM class A4)"),
PType("techrep",["techreport"],"Co-authored technical reports (OKM class D4)"),
PType("coed",["proceedings"],"Co-edited proceedings (OKM class C2)"),
PType("phd",["phdthesis"],"PhD dissertation (OKM class G5)")]
preamble=r"""
\documentclass[a4paper,11pt]{article}
\usepackage{natbib}
\usepackage{multibib}
\usepackage{a4wide}
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
%%%NEWCITESDEFS%%%
\title{List of publications}
\author{%%%LATEXAUTHOR%%%}
\begin{document}
\maketitle
%%%CITES%%%
%%%BIBSTYLES%%%
\end{document}
"""
parser = argparse.ArgumentParser(description='Generate a .tex which compiles into a personal publication list.')
parser.add_argument('--latexauthor', dest='latexauthor', nargs=1,help='The author field of the latex document, your name.',required=True)
parser.add_argument('-a','--author', dest='author', nargs="+",help='Space-separated strings to look for in the author E.g. --author Kanerva Nyblom',required=True)
parser.add_argument('-e','--editor', dest='editor', action='store_true',help='Also look in the editor field.')
parser.add_argument('-r','--relevant', dest='relevant', default="", nargs=1, help='Comma-separated (no space) list of bibids to produce the related citations part, if any')
parser.add_argument('-y','--year-since', dest='year', default=1800, nargs=1, type=int, help='Only publications whose year is greater or equal to this')
args = parser.parse_args()
with open("turkunlp.bib") as f:
db=bibtexparser.load(f)
matching={} #type -> [pubs]
for x in db.entries:
#whih year?
y=x["year"].split()[0] #sometimes these go like "2018 (to appear)"
y=int(y)
if y<args.year:
continue
for n in args.author:
if n in x.get("author","") or (args.editor and n in x.get("editor","")):
matching.setdefault(x["ENTRYTYPE"],[]).append(x)
break
print("Found {} matching entries\n".format(sum(len(lst) for lst in matching.values())),file=sys.stderr)
for etype,lst in sorted(matching.items(),key=lambda etype_lst: len(etype_lst[1]), reverse=True):
print("{:20} : {}".format(etype,len(matching[etype])),file=sys.stderr)
nocites="journal bookc conf phd coed techrep".split()
pub_types="article incollection inproceedings phdthesis proceedings techreport".split() #can also be incollection,inbook
#\bibliographystylejournal{unsrtnat-nourl}
#\bibliographyjournal{turkunlp}
newcitedefs=[]
cites=[]
bibstyles=[]
for ptype in ptypes:
records=[]
for bib_type in ptype.bibtypes:
records.extend(matching.get(bib_type,[]))
for r in records:
if "year" not in r:
print("WARNING: record {} had no year".format(r["ID"]),file=sys.stderr)
print(r,file=sys.stderr)
records.sort(key=lambda rec:rec["year"],reverse=True)
if records:
#Found something
newcitedefs.append(r"\newcites{{{}}}{{{}}}".format(ptype.label,ptype.section))
bibstyles.append(r"\bibliographystyle{}{{unsrtnat-nourl}}\bibliography{}{{turkunlp}}".format(ptype.label,ptype.label))
for r in records:
cites.append(r"\nocite{}{{{}}} %%%{}".format(ptype.label,r["ID"],r["title"]))
if args.relevant:
bibstyles.insert(0,r"\bibliographystyle{}{{unsrtnat-nourl}}\bibliography{}{{turkunlp}}".format("rel","rel"))
newcitedefs.insert(0,r"\newcites{rel}{Co-authored publications most relevant to the proposal (also repeated in their relevant category listing)}")
for x in args.relevant[0].split(",")[::-1]:
cites.insert(0,r"\nociterel{{{}}}".format(x))
p=preamble.replace("%%%NEWCITESDEFS%%%","\n".join(newcitedefs))
p=p.replace("%%%LATEXAUTHOR%%%",args.latexauthor[0])
p=p.replace("%%%CITES%%%","\n".join(cites))
p=p.replace("%%%BIBSTYLES%%%","\n".join(bibstyles))
print(p)