-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_concepts.py
executable file
·268 lines (196 loc) · 8.38 KB
/
index_concepts.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
#
# index_concepts.py
# José Devezas <[email protected]>
# 2020-06-12
import argparse
import csv
import glob
import operator
import os
import re
import sys
import tempfile
from collections import defaultdict
from itertools import chain
from shutil import copy2, copytree
import ahocorasick
# *****************************************************************************
# * Constants (you can change these)
# *****************************************************************************
# Number of distance lines (resets per chapter)
DISTANCE = 200
# Stopwords based on a cutoff at frequency 100 for the top concepts
THESIS_STOPWORDS = set([
'entity',
'information',
'document',
'model',
'hypergraph-of-entity',
'node',
'entity-oriented search',
'hypergraph',
'ranking',
'term'
])
# *****************************************************************************
# * Functions
# *****************************************************************************
def check_begin(line, count_env, allowed_env):
m = re.match(r'.*\\begin{(.*?)}.*', line)
if m and m.group(1) not in allowed_env:
count_env[m.group(1)] += 1
def check_end(line, count_env, allowed_env):
m = re.match(r'.*\\end{(.*?)}.*', line)
if m and m.group(1) not in allowed_env:
count_env[m.group(1)] -= 1
def is_inside_env(count_env):
for _, v in count_env.items():
if v > 0:
return True
return False
def is_inside_command(line, start_index, end_index, open_char='{', close_char='}'):
before_match = line[:start_index]
after_match = line[end_index+1:]
before_count = before_match.count(open_char) - before_match.count(close_char)
after_count = after_match.count(open_char) - after_match.count(close_char)
return before_count > 0 and after_count < 0
def is_part_of_command(line, end_index):
rev_line = ''.join(reversed(line))
m = re.match(r'^[a-zA-Z]*?\\.*', rev_line[-end_index:])
return m is not None
def is_invalid_concept(concept):
return '/' in concept or 'et al' in concept
def can_annotate(methods, last_line_nr, current_line_nr, concept):
cond = True
if methods is not None:
if 'dist' in methods:
cond = cond and (last_line_nr == -1 or current_line_nr > last_line_nr + DISTANCE)
if 'stop' in methods:
cond = cond and concept not in THESIS_STOPWORDS
return cond
# *****************************************************************************
# * Command line arguments
# *****************************************************************************
parser = argparse.ArgumentParser(description="Index concepts in your LaTeX manuscript")
parser.add_argument(
'-i', '--input',
type=str,
required=True,
help='main LaTeX file')
parser.add_argument(
'-c', '--concepts',
type=str,
required=True,
help='concepts CSV (generated by extract_concepts.py')
parser.add_argument(
'-o', '--output',
type=str,
required=True,
help='output directory (a copy of the project with \\index{...} entries)')
parser.add_argument(
'-e', '--exclude',
type=str,
action='append',
help='exclude LaTeX files (path from main LaTeX directory; can be defined multiple times)')
parser.add_argument(
'-a', '--allowed-env',
type=str,
action='append',
default=['sloppypar'],
help='allowed environments for indexing concepts (text inside other environments will be skipped)')
parser.add_argument(
'-m', '--method',
type=str,
action='append',
choices=['dist', 'stop'],
help='optionally choose multiple methods to reduce the number of page entries per concept')
args, _ = parser.parse_known_args()
# *****************************************************************************
# * LaTeX project copy
# *****************************************************************************
print("==> Copying files from main LaTeX directory to output directory", file=sys.stderr)
source_dir = os.path.dirname(os.path.abspath(args.input))
target_dir = os.path.join(args.output, os.path.basename(source_dir))
if os.path.exists(target_dir):
print("Target directory already exists: %s" % target_dir, file=sys.stderr)
sys.exit(1)
copytree(source_dir, target_dir)
# *****************************************************************************
# * Detect LaTeX files to edit
# *****************************************************************************
print("==> Identifying LaTeX files to edit", file=sys.stderr)
relative_paths = glob.iglob('%s/**/*.tex' % target_dir, recursive=True)
relative_paths = set([path.replace(target_dir, '').lstrip('/')
for path in relative_paths])
ignore_paths = [glob.glob(path) for path in args.exclude]
ignore_paths = set(chain.from_iterable(ignore_paths))
stats = defaultdict(lambda: 0)
# *****************************************************************************
# * Build Aho-Corasick tree
# *****************************************************************************
print("==> Preparing Aho-Corasick concept matcher", file=sys.stderr)
matcher = ahocorasick.Automaton()
with open(args.concepts) as con_f:
reader = csv.DictReader(con_f)
for idx, row in enumerate(reader):
matcher.add_word(row['match'], (row['match'], row['concept']))
stats['index terms'] += 1
matcher.make_automaton()
# *****************************************************************************
# * Annotate index entries
# *****************************************************************************
print("==> Inserting \\index{...} entries", file=sys.stderr)
stats['annotation distribution'] = defaultdict(lambda: 0)
for path in sorted(relative_paths.difference(ignore_paths)):
abs_path = os.path.join(target_dir, path)
print(" %s" % path)
stats['edited files'] += 1
last_line = defaultdict(lambda: -1)
with open(abs_path, 'r') as tex_f, tempfile.NamedTemporaryFile('w', delete=False) as tmp_f:
count_env = defaultdict(lambda: 0)
for line_nr, line in enumerate(tex_f):
check_begin(line, count_env, args.allowed_env)
check_end(line, count_env, args.allowed_env)
if is_inside_env(count_env):
tmp_f.write(line)
stats['environment blocks skipped'] += 1
continue
total_annotation_length = 0
for end_index, (match, concept) in matcher.iter_long(line):
end_index += total_annotation_length
start_index = end_index - len(match) + 1
if is_invalid_concept(concept) \
or is_inside_command(line, start_index, end_index, '{', '}') \
or is_inside_command(line, start_index, end_index, '[', ']') \
or is_part_of_command(line, end_index):
stats['invalid matches'] += 1
continue
concept_freq = stats['annotation distribution'][concept]
if re.match(r'\W', line[end_index+1]) and re.match(r'\W', line[end_index-len(match)]) \
and can_annotate(args.method, last_line[concept], line_nr, concept):
annotation = '\\index{' + concept + '}'
line = line[0:end_index+1] + annotation + line[end_index+1:]
stats['indexed matches'] += 1
stats['annotation distribution'][concept] += 1
total_annotation_length += len(annotation)
last_line[concept] = line_nr
tmp_f.write(line)
tmp_filename = tmp_f.name
copy2(tmp_filename, abs_path)
os.unlink(tmp_filename)
# *****************************************************************************
# * Show statistics
# *****************************************************************************
print('==> A new indexed version of your document is available in %s' % target_dir, file=sys.stderr)
for name in sorted(stats.keys()):
if type(stats[name]) is int:
print(' %s: %d' % (name, stats[name]))
top_n = 50
for name in sorted(stats.keys()):
if type(stats[name]) is defaultdict:
print('\n %s (top %d)\n' % (name.upper(), top_n))
for idx, (concept, freq) in enumerate(sorted(stats[name].items(), key=operator.itemgetter(1), reverse=True)):
if idx >= top_n:
break
print(' %3d %s' % (freq, concept))