-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·88 lines (77 loc) · 2.21 KB
/
run.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
#!/usr/bin/env python
"""\
%(app)s
Usage:
%(cmd)s [options] <wordset_dictionary_path>
%(cmd)s (-h | --help)
%(cmd)s --version
Options:
-h --help Show this screen
--version Show version
"""
# vi:syntax=python
from __future__ import print_function, division
# System Imports
import sys
import os
import io
import json
# External Imports
import docopt
__version__ = '0.1'
__appname__ = 'animal-word-factory'
def main():
"""
Read command line options and pass to the main command line entry point.
"""
appname = __appname__ if __appname__ else sys.argv[0]
args = docopt.docopt(
__doc__ % {
'app': appname,
'cmd': sys.argv[0],
},
version='%s %s' % (
appname,
__version__,
)
)
run_main(args)
def get_basedir():
"""
Locate the current directory of this file
"""
return os.path.dirname(os.path.abspath(sys.modules[__name__].__file__))
def run_main(args):
"""
Main Command Line Entry Point
"""
wordset_dictionary_path = args['<wordset_dictionary_path>']
animal_kinds = [
'mammal', 'invertebrate', 'reptile', 'fish', 'bird', 'amphibian',
]
animals = []
for letter in (chr(val) for val in range(ord('a'), ord('z') + 1)):
fpath = os.path.join(wordset_dictionary_path, 'data', '%s.json' % (
letter,
))
with io.open(fpath, 'r', encoding='utf-8') as fobj:
dictdata = json.load(fobj)
for word in dictdata:
worddetails = dictdata[word]
meanings = worddetails.get('meanings', [])
found = False
for meaning in meanings:
if meaning['speech_part'] != 'noun':
continue
if any(
animalkind in meaning['def'].lower().split(' ')
for animalkind in animal_kinds
):
found = True
break
if found:
animals.append(word)
print('\n'.join(animals))
if __name__ == '__main__':
main()
# vim: set ft=python: