-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_concepts.py
executable file
·217 lines (169 loc) · 6.11 KB
/
extract_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
#!/usr/bin/env python
#
# extract_concepts.py
# José Devezas <[email protected]>
# 2020-06-12
import argparse
import csv
import os
import re
import sys
from collections import Counter
import nltk
from RAKE import Rake
from yake import KeywordExtractor
def get_continuous_chunks(sentence):
chunked = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sentence)))
continuous_chunk = []
current_chunk = []
for i in chunked:
if type(i) is nltk.Tree:
current_chunk.append(" ".join([token for token, pos in i.leaves()]))
elif current_chunk:
named_entity = " ".join(current_chunk)
if named_entity not in continuous_chunk:
continuous_chunk.append(named_entity)
current_chunk = []
else:
continue
if continuous_chunk:
named_entity = " ".join(current_chunk)
if named_entity not in continuous_chunk:
continuous_chunk.append(named_entity)
return continuous_chunk
def nltk_extract_concepts(text, exclude, number):
print("==> Segmenting into sentences and extracting entities", file=sys.stderr)
entities = []
for sentence in nltk.sent_tokenize(text):
entities += get_continuous_chunks(sentence)
entities = [e for e in entities if e != '' and e not in exclude and len(e) >= args.min_length]
print("==> Counting entities", file=sys.stderr)
entity_count = Counter(entities)
print("==> Identifying top %d entities" % number, file=sys.stderr)
return entity_count.most_common(args.number)
def rake_extract_concepts(text, exclude, number):
r = Rake('/usr/share/postgresql/10/tsearch_data/english.stop')
concepts = r.run(text, minCharacters=2, maxWords=4, minFrequency=3)
count = 0
for keyword, weight in concepts:
if weight > 1 and count < number and keyword not in exclude:
yield keyword, weight
count += 1
def yake_extract_concepts(text, exclude, number):
y = KeywordExtractor(lan='en', n=5, windowsSize=1, top=number)
concepts = y.extract_keywords(text)
for keyword, weight in concepts:
if keyword not in exclude:
yield keyword, weight
parser = argparse.ArgumentParser(description="Extract concepts from your LaTeX manuscript")
parser.add_argument(
'-i', '--input',
type=str,
required=True,
help='main LaTeX file')
parser.add_argument(
'-o', '--output',
type=str,
required=True,
help='output CSV to store keywords and their weights')
parser.add_argument(
'-f', '--from',
type=str,
default='Introduction',
help='exact title of the first section (default is "Introduction")')
parser.add_argument(
'-t', '--to',
type=str,
default='Appendix',
help='exact title of the first section to ignore (default is "Appendix")')
parser.add_argument(
'-n', '--number',
type=int,
default=200,
help='number of concepts to extract (default is 200)')
parser.add_argument(
'-e', '--exclude',
type=str,
default='Figure,Table,Figures,Tables,Section,Sections',
help='number of concepts to extract (default is "Figure,Table,Figures,Tables,Section,Sections")')
parser.add_argument(
'-ml', '--min-length',
type=int,
default=0,
help='minimum number of characters of a keyword (default is 0)')
parser.add_argument(
'-m', '--method',
type=str,
choices=['nltk', 'rake', 'yake'],
default='rake',
help='concept extraction method (default is "yake")')
parser.add_argument(
'-s', '--select',
action='store_true',
default=False,
help='interactively select or edit concepts to keep (default is false)')
args, _ = parser.parse_known_args()
nltk.download([
'averaged_perceptron_tagger',
'maxent_ne_chunker',
'words'
], quiet=True)
print("==> Converting LaTeX files to plain text", file=sys.stderr)
text = ''
detex_exclude = ['table', 'figure', 'equation', 'minipage', 'multicols', 'lstlisting']
with os.popen('detex -e "%s" %s' % (','.join(detex_exclude), args.input)) as f:
ignore = True
for line in f:
if line == getattr(args, 'from') + '\n':
ignore = False
if line == args.to + '\n':
break
if ignore:
continue
text += line
text = re.sub(r'\n{2,}', '\n\n', text)
text = re.sub(r'\[.*?\]', '', text)
exclude = set(args.exclude.split(','))
if args.method == 'nltk':
print("==> Extracting concepts using NLTK", file=sys.stderr)
concepts = nltk_extract_concepts(text, exclude, args.number)
elif args.method == 'rake':
print("==> Extracting concepts using RAKE", file=sys.stderr)
concepts = rake_extract_concepts(text, exclude, args.number)
elif args.method == 'yake':
print("==> Extracting concepts using YAKE", file=sys.stderr)
concepts = yake_extract_concepts(text, exclude, args.number)
concepts = list(concepts)
with open(args.output, 'w') as f:
csv = csv.writer(f)
csv.writerow(['concept', 'match', 'weight'])
added = set([])
for idx, (concept, weight) in enumerate(concepts):
if concept in added:
continue
matches = [concept]
if args.select:
while True:
print('\n[%4d/%d] %s -> %s ' % (idx, len(concepts), ','.join(matches), concept))
ans = input('[k/enter=KEEP, a=KEEP ALL FOLLOWING, d=DELETE, e=RENAME INDEX ENTRY, w=EDIT MATCHES] ')
if ans == 'a':
args.select = False
break
if ans == 'd':
break
if ans == 'e':
concept = input('index entry rename> ')
continue
if ans == 'w':
matches = input('edit matches (comma-separated)> ').split(',')
continue
if ans in ('k', ''):
if concept in added:
break
added.add(concept)
for match in matches:
csv.writerow([concept, match, weight])
break
else:
for match in matches:
csv.writerow([concept, match, weight])