-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
executable file
·86 lines (71 loc) · 2.78 KB
/
test.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
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# Grafeno -- Python concept graphs library
# Copyright 2016 Antonio F. G. Sevilla <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import argparse
arrayize = lambda t: t.split(',')
arg_parser = argparse.ArgumentParser(description='Test script')
group = arg_parser.add_mutually_exclusive_group()
group.add_argument('-s','--string')
group.add_argument('-f','--file',type=argparse.FileType('r'))
arg_parser.add_argument('-p','--parser',help='parser module to use')
arg_parser.add_argument('-t','--transformers',type=arrayize,help='transformer pipeline to use')
arg_parser.add_argument('-l','--linearizers',type=arrayize,help='linearizing pipeline to use')
arg_config = arg_parser.add_argument('-c','--config-file',help='use a config file for pipeline options')
arg_parser.add_argument('-d','--display',action='store_true',help='display a drawing of the graph')
arg_parser.add_argument('-j','--print-json',action='store_true',help='print the graph in json')
try:
import argcomplete
import glob
from os.path import dirname, basename
def config_completer(prefix, **kwargs):
return (basename(c)[:-5] for c in glob.glob(dirname(__file__)+"/configs/*.yaml"))
arg_config.completer = config_completer
argcomplete.autocomplete(arg_parser)
except ImportError:
pass
args = arg_parser.parse_args()
import yaml
from grafeno import pipeline
if args.file:
text = args.file.read()
else:
text = args.string
if args.config_file:
try:
config_file = open('configs/'+args.config_file+'.yaml')
except FileNotFoundError:
config_file = None
if not config_file:
config_file = open(args.config_file)
config = yaml.safe_load(config_file)
else:
config = {}
if args.parser:
config['parser'] = args.parser
if args.transformers:
config['transformers'] = args.transformers
if args.linearizers:
config['linearizers'] = args.linearizers
config['text'] = text
result = pipeline.run(config)
if args.print_json:
print(result.to_json())
if args.display:
result.draw()
if not args.print_json or not args.display:
print(result)