-
Notifications
You must be signed in to change notification settings - Fork 6
/
sample.py
executable file
·126 lines (97 loc) · 3.2 KB
/
sample.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
#!/usr/bin/python
# sample.py
# Saito 2015
"""This program is for grabbing and saving a sample of geo-located tweets
USAGE:
$ ./sample.py [-h][-d][-v][-f FILENAME][-o OUTPUT][-vis]
Print command line help:
$ ./sample.py --help (or just -h)
Example: This uses parameter file 'params.txt', prints results to
command line and writes them to 'out.txt':
$ ./sample.py --verbose --filename params.txt --output out.txt
The program requires a file in this folder called consumerkeyandsecret
which contains only a consumer key on the first line and consumer
secret (the longer one) on the second line. See README.
The program can optionally take a parameter file as input. Please see
the file "params.txt" for an example.
Example of params.txt:
{"latitude" : 37.7821,
"longitude": -122.4093,
"radius" : 10,
"search_term" : "#SF+tech",
"result_type" : "mixed",
"count" : 15}
"""
import sys
import argparse
import geosearchclass
def get_parser():
""" Creates a command line parser
--doc -d
--help -h
--filename -f
--verbose -v
--output -o
--visualize -vis
--default
"""
parser = argparse.ArgumentParser(
description='Perform a geo-located search.')
parser.add_argument(
'-d', '--doc', action='store_true',
help='print module documentation and exit')
parser.add_argument(
'-f', '--filename',
help='''specify a FILENAME to use as the parameter file.
If not specified, will use 'params.txt'.''')
parser.add_argument(
'-v', '--verbose', action='store_true',
help='additionally print output to command line')
parser.add_argument(
'--default', action='store_true',
help="""ignore parameter file and use default search
terms from geosearchclass""")
parser.add_argument(
'-o', '--output',
help='''specify an OUTPUT file to write to.
Default is output.txt''')
parser.add_argument(
'-j', '--json',
help='''specify an OUTPUT JSON file to write to.''')
parser.add_argument('-vis', '--visualize',
action='store_true', help='visualize using nlp tools')
# automatically grabs arguments from sys.argv[]
return parser
def main():
parser = get_parser()
args = parser.parse_args()
if args.doc:
print __doc__
sys.exit()
g = geosearchclass.GeoSearchClass()
if args.filename:
print 'Using parameters from ' + str(args.filename)
# turn parameter file into dictionary
g.set_params_from_file(args.filename)
else:
if args.default:
print 'Using default search terms'
else:
print 'Using parameters from params.txt'
g.set_params_from_file('params.txt')
g.search()
# print formatted results with extra info to terminal
if args.verbose:
g.print_search_results()
if args.output:
g.write_search_results(args.output)
else:
g.write_search_results()
if args.json:
g.json_search_results(args.json)
if args.visualize:
import utils
filtered_words = utils.tokenize_and_filter(g.search_results)
utils.visualize(filtered_words)
if __name__ == '__main__':
main()