forked from rjruigrok/HackingScience
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildtex.py
executable file
·137 lines (115 loc) · 4.76 KB
/
buildtex.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import tempfile
import shutil
from pymarkov import markov
from random import randrange
import time
def pdflatex(tempdir, current, number,title,abstract,content):
pdflatexbin = "/usr/texbin/"
# Set up temporary working directory for latex
parsefile(tempdir + "/template.tex",title,abstract,content)
os.chdir(tempdir)
# Build pdf file
os.system(pdflatexbin + "pdflatex template.tex && " + pdflatexbin + "bibtex template && " + pdflatexbin + "pdflatex template.tex && " + pdflatexbin + "pdflatex template.tex && " + pdflatexbin + "pdflatex template.tex")
# Copy result and cleanup temp
if os.path.isfile(tempdir + "/template.pdf"):
shutil.copyfile(tempdir + "/template.pdf", "%s/generated/result_%03d.pdf" % (current, number))
shutil.rmtree(tempdir)
os.chdir(current)
def appendbib(bibfile, bibtex):
fw = open(bibfile, 'a')
fw.write(bibtex + "\n\n\n")
fw.close()
# fr.close()
# os.remove(texfile)
# fw = open(texfile, 'w')
# fw.write(lines)
# fw.close()
def markovtitle(text):
markov_dict = markov.train([text], 2)
return markov.generate(markov_dict, randrange(4,10), 2)
def getnextabstract(papers,tempdir):
# select random paper (which is not already chosen)
randomindex = randrange(0,len(papers))
if randomindex in usedpapers:
return getnextabstract(papers,tempdir)
usedpapers.append(randomindex)
print "abstract %d selected" % randomindex
fn = papers[randomindex]
# retrieve abstract
abstractfile = "papers/" + fn.replace(".pdf",".abstract.txt")
if os.path.isfile(abstractfile):
fr = open(abstractfile, 'r')
abstract = escapelatex(fr.read())
if len(abstract) == 0:
return getnextabstract(papers,tempdir)
fr.close()
else:
return getnextabstract(papers,tempdir)
# retrieve bibtex
fr = open("papers/" + fn.replace(".pdf",".bib"), 'r')
bibtex = fr.read()
fr.close()
if (len(bibtex) > 0):
#bibtex = bibtex.replace("_","\_")
appendbib(tempdir + "/biblio.bib",bibtex)
abstract += "\cite{%s}" % fn
return abstract
#return getnextabstract(papers,tempdir)
def allpapertitles():
titles = ""
for fn in os.listdir("papers"):
if (fn.endswith(".title.txt")):
fr = open("papers/" + fn, 'r')
title = fr.read()
if not "No title found" in title and not "Could not convert PDF" in title:
titles += " " + title
fr.close()
#print title
return titles
# Put papers in a list
#papers = []
#for fn in os.listdir("papers"):
# if fn.endswith(".pdf"):
# papers.append(fn)
#papertitles = allpapertitles()
#exit()
# Generate 100 random papers
for x in range(0, 0):
usedpapers = []
current = os.getcwd()
tempdir = tempfile.mkdtemp()
shutil.copyfile(current + "/latextemplate/biblio.bib", tempdir + "/biblio.bib")
shutil.copyfile(current + "/latextemplate/template.tex", tempdir + "/template.tex")
# generate random sections...
abstrandom = getnextabstract(papers,tempdir)
papertitle = markovtitle(papertitles).capitalize()
intro = getnextabstract(papers,tempdir)
bgwork = getnextabstract(papers,tempdir)
problemdesc = getnextabstract(papers,tempdir)
propsol1 = getnextabstract(papers,tempdir)
propsol2 = getnextabstract(papers,tempdir)
random7 = getnextabstract(papers,tempdir)
randomsect1 = getnextabstract(papers,tempdir)
sectiontitle1 = markovtitle(randomsect1).capitalize()
randomsect2 = getnextabstract(papers,tempdir)
randomsect3 = getnextabstract(papers,tempdir)
sectiontitle2 = markovtitle(randomsect3).capitalize()
randomsect4 = getnextabstract(papers,tempdir)
randomsect5 = getnextabstract(papers,tempdir)
randomsect6 = getnextabstract(papers,tempdir)
randomsect7 = getnextabstract(papers,tempdir)
subsectiontitle1 = markovtitle(random7).capitalize()
random8 = getnextabstract(papers,tempdir)
subsectiontitle2 = markovtitle(random8).capitalize()
concl = getnextabstract(papers,tempdir)
future = getnextabstract(papers,tempdir)
# Build PDF from tex
pdflatex(tempdir, current, x,papertitle,
abstrandom,
"\section{Introduction} %s \section{Background work} %s \section{Problem description} %s \section{Proposed solution} %s %s \section{%s} %s %s %s \section{%s} %s %s %s %s \subsection{%s} %s \subsection{%s} %s \section{Conclusion} %s \section{Future work} %s " %
(intro, bgwork, problemdesc, propsol1, propsol2, sectiontitle1, randomsect1, randomsect2, randomsect3, sectiontitle2, randomsect4, randomsect5, randomsect6, randomsect7, subsectiontitle1, random7, subsectiontitle2, random8, concl, future))
#print tempdir
#exit()