/
autoindex.py
38 lines (30 loc) · 1.1 KB
/
autoindex.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
#!/usr/bin/python3
import glob
import re
lgs=open("locallanguages.txt").read().split('\n')
terms=open("localsubjectterms.txt").read().split('\n')[::-1]#reverse to avoid double indexing
print("found %i language names for autoindexing" % len(lgs))
print("found %i subject terms for autoindexing" % len(terms))
files = glob.glob('chapters/*tex')
SUBJECTP = re.compile
for f in files:
print("indexing %s" % f)
c = open(f).read()
for lg in lgs:
lg = lg.strip()
shortlg = lg.replace(' ','').replace('-','').replace('"','').replace('=','')
if lg == '':
continue
c = re.sub('(?<!ili{)%s(?![\w}])'%lg, '\ili%s'%shortlg, c)
for term in terms:
term = term.strip()
if term == '':
continue
c = re.sub('(?<!isi{)%s(?![-_\w}])'%term, '\isi{%s}'%term, c)
nlg = len(re.findall('\\ili',c))
nt = len(re.findall('\\isi{',c))
outfile = open(f.replace('chapters','indexed'), 'w')
outfile.write(c)
outfile.close()
print(" %s now contains %i indexed languages and %i indexed subject terms"%(f.split('/')[-1],nlg,nt))
print("indexed files are in the folder 'indexed'")