forked from azk0019/CourseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
303 lines (263 loc) · 7.84 KB
/
main.py
File metadata and controls
303 lines (263 loc) · 7.84 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import annotate
from argparse import ArgumentTypeError, ArgumentParser
from compress import compress_patterns
import logging
import nltk
from os import remove
from pathlib import Path
import scrape
import subprocess
from tempfile import NamedTemporaryFile, TemporaryFile
def restricted_float(x):
try:
x = float(x)
except ValueError:
raise ArgumentTypeError("%r not a floating-point literal" % (x,))
if x < 0.0 or x > 1.0:
raise ArgumentTypeError("%r not in range [0.0, 1.0]" % (x,))
return x
def mine_patterns(
mining_method,
spmf_path,
output_path,
support,
cleanup_re,
dblp_file,
tag,
jaccard_threshold,
is_sequence,
separator,
):
fp_file = NamedTemporaryFile(delete=False)
fp_file.close()
subprocess.run([
'java',
'-Xmx2048m',
'-jar',
'./lib/spmf.jar',
'run',
mining_method,
spmf_path,
fp_file.name,
str(support),
])
subprocess.run(['sed', '-i', cleanup_re, fp_file.name])
fp_file = open(fp_file.name, 'r')
output_file = open(output_path, 'w+')
try:
compress_patterns(
dblp_file,
fp_file,
output_file,
tag,
jaccard_threshold,
is_sequence,
separator,
)
finally:
fp_file.close()
output_file.close()
remove(fp_file.name)
def scrape_patterns(args):
# Download meta-files required by the tokenizer library.
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
dblp_file = open(args.dblp_file, 'rb')
article_file = open(args.article_file, 'w+b')
try:
scrape.filter_articles(
dblp_file,
article_file,
int(args.from_year) if args.from_year else None,
)
finally:
dblp_file.close()
article_file.close()
def mine_dblp_patterns(args):
dblp_file = open(args.dblp_file, 'rb')
title_spmf_file = NamedTemporaryFile(mode = 'w+', delete=False)
author_spmf_file = NamedTemporaryFile(mode = 'w+', delete=False)
try:
scrape.mine_patterns(dblp_file, title_spmf_file, author_spmf_file)
finally:
dblp_file.close()
title_spmf_file.close()
author_spmf_file.close()
with open(args.dblp_file, 'rb') as dblp_file:
mine_patterns(
'CloSpan',
title_spmf_file.name,
args.title_file,
args.title_support,
's/ / /g; s/ #SUP: [0-9]\+//g',
dblp_file,
'label',
args.title_distance,
True,
' ',
)
with open(args.dblp_file, 'rb') as dblp_file:
mine_patterns(
'FPClose',
author_spmf_file.name,
args.author_file,
args.author_support,
's/ \; #SUP: [0-9]\+//g',
dblp_file,
'author',
args.author_distance,
False,
' ; ',
)
remove(title_spmf_file.name)
remove(author_spmf_file.name)
def annotate_pattern(args):
dblp_file = open(args.dblp_file, 'rb')
title_file = open(args.title_file, 'r')
author_file = open(args.author_file, 'r')
try:
annotate.annotate_pattern(
args.type,
args.query,
dblp_file,
title_file,
author_file,
args.n_context,
args.n_synonyms,
args.n_examples,
)
finally:
dblp_file.close()
title_file.close()
author_file.close()
if __name__ == '__main__':
parser = ArgumentParser(
description='Semantic annotations for DBLP patterns.'
)
parser.add_argument(
'--log',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default='ERROR',
help='the log level (Default: ERROR)',
)
subparsers = parser.add_subparsers(dest='cmd')
subparsers.required = True
scrape_parser = subparsers.add_parser(
'scrape', help='Scrape inproceeding articles from a DBLP dataset.'
)
scrape_parser.add_argument(
'--dblp_file',
required=True,
help='REQUIRED: the path to the DBLP input file',
)
scrape_parser.add_argument(
'--article_file',
required=True,
help='REQUIRED: the path where the selected articles will be printed in XML format',
)
scrape_parser.add_argument(
'--from_year',
help='selects articles from no earlier than the provided year',
)
scrape_parser.set_defaults(func=scrape_patterns)
mine_parser = subparsers.add_parser(
'mine', help='Mine author and title patterns from a DBLP dataset.'
)
mine_parser.add_argument(
'--dblp_file',
required=True,
help='REQUIRED: the path to the DBLP input file',
)
mine_parser.add_argument(
'--title_file',
required=True,
help='REQUIRED: the path where the title patterns will be printed',
)
mine_parser.add_argument(
'--author_file',
required=True,
help='REQUIRED: the path where the author patterns will be printed',
)
mine_parser.add_argument(
'--title_support',
type=restricted_float,
required=True,
help='REQUIRED: the minimum support [0, 1] for title patterns.',
)
mine_parser.add_argument(
'--author_support',
type=restricted_float,
required=True,
help='REQUIRED: the minimum support [0, 1] for author patterns.',
)
mine_parser.add_argument(
'--title_distance',
type=restricted_float,
required=True,
help='REQUIRED: the Jaccard threshold [0, 1] to use when compressing title patterns',
)
mine_parser.add_argument(
'--author_distance',
type=restricted_float,
required=True,
help='REQUIRED: the Jaccard threshold [0, 1] to use when compressing author patterns',
)
mine_parser.set_defaults(func=mine_dblp_patterns)
annotate_parser = subparsers.add_parser(
'annotate', help='Enrich patterns with semantic annotations.'
)
annotate_parser.add_argument(
'--dblp_file',
required=True,
help='REQUIRED: the XML input file with all the transactions',
)
annotate_parser.add_argument(
'--title_file',
required=True,
help='REQUIRED: the input file that stores the patterns for titles',
)
annotate_parser.add_argument(
'--author_file',
required=True,
help='REQUIRED: the input file that stores the patterns for authors',
)
annotate_parser.add_argument(
'-q',
'--query',
required=True,
help='REQUIRED: the query pattern to enrich with semantic annotations',
)
annotate_parser.add_argument(
'--type',
required=True,
choices=['author', 'title'],
help='REQUIRED: the type of the query pattern',
)
annotate_parser.add_argument(
'-n1',
'--n_context',
type=int,
required=True,
help='REQUIRED: the number of context indicators to select',
)
annotate_parser.add_argument(
'-n2',
'--n_synonyms',
type=int,
required=True,
help='REQUIRED: the number of semantically similar patterns to select',
)
annotate_parser.add_argument(
'-n3',
'--n_examples',
type=int,
required=True,
help='REQUIRED: the number of representative transactions to select',
)
annotate_parser.set_defaults(func=annotate_pattern)
args = parser.parse_args()
numeric_level = getattr(logging, args.log.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level)
args.func(args)