-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewman.py
executable file
·108 lines (92 loc) · 3.13 KB
/
newman.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
#!/usr/bin/env python
#
# NEWMAN: Natural English With Mutating Abridged Nouns
#
# Copyright 2010 Chris Eberle <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys, os, getopt, config
from search import *
from debug import *
def batch(batchFile, vocabulary, grammar, generator):
"""
Run a batch search, exit on the first error.
"""
searches = []
fh = open(batchFile, 'r')
for line in fh:
line = line.strip()
if len(line) > 0:
searches.append(line)
fh.close()
if len(searches) == 0:
print "No searches to batch :("
sys.exit(1)
for searchString in searches:
print '-'*80
print searchString.center(80)
print '-'* 80
if search(searchString, vocabulary, grammar, generator) != 0:
sys.exit(1)
print ''
def usage():
"""
Print out the usage for this program
"""
print 'Usage: %s OPTIONS' % (os.path.basename(sys.argv[0]))
print ' -s SEARCH Search for a phrase'
print ' -b FILE Batch search using a file (one search per line)'
print ' -l WORD Use wordnet to lookup a word'
print ' -d Enable debugging'
print ' -h Show this helpful message'
def main():
"""
The main program method
"""
try:
opts, args = getopt.getopt(sys.argv[1:], 'b:s:l:dh', ['batch=', 'search=', 'lookup=', 'debug', 'help'])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(1)
searchString = None
batchFile = None
lookupString = None
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-b', '--batch'):
batchFile = a
elif o in ('-s', '--search'):
searchString = a.strip()
elif o in ('-l', '--lookup'):
lookupString = a.strip()
elif o in ('-d', '--debug'):
set_debug(True)
else:
assert False, "unhandled option"
if (searchString == None or len(searchString) == 0) and (lookupString == None or len(lookupString) == 0) \
and (batchFile == None or len(batchFile) == 0):
usage()
sys.exit(1)
debug('Initializing...')
vocabulary, grammar, generator = config.configure()
if lookupString != None and len(lookupString) > 0:
sys.exit(lookup(lookupString, vocabulary))
elif batchFile != None and len(batchFile) > 0:
batch(batchFile, vocabulary, grammar, generator)
else:
sys.exit(search(searchString, vocabulary, grammar, generator))
if __name__ == '__main__':
main()