-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_cmd.py
94 lines (75 loc) · 2.47 KB
/
tweet_cmd.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
#!/usr/bin/env python
"""
Interactive Interface for the Twitter Sentiment Analysis App
Usage:
my_program fetch <username>
my_program wordfrequency
my_program sentiment
my_program (-i | --interactive)
my_program (-h | --help | --version)
Options:
-i, --interactive Interactive Mode
-h, --help Show this screen and exit.
"""
import sys
import cmd
from docopt import docopt, DocoptExit
from termcolor import colored as color
from tweet import *
from util import *
from analysis import *
def docopt_cmd(func):
"""
This decorator is used to simplify the try/except block and pass the result
of the docopt parsing to the called action.
"""
def fn(self, arg):
try:
opt = docopt(fn.__doc__, arg)
except DocoptExit as e:
# The DocoptExit is thrown when the args do not match.
# We print a message to the user and the usage block.
print('Invalid Command!')
print(e)
return
except SystemExit:
# The SystemExit exception prints the usage for --help
# We do not need to do the print here.
return
return func(self, opt)
fn.__name__ = func.__name__
fn.__doc__ = func.__doc__
fn.__dict__.update(func.__dict__)
return fn
class AnalyzerCmd (cmd.Cmd):
intro = ''' Twitter Sentiment Analysis\n
list of commands\n
fetch <twitter_handle> - gets the twets for specified username\n
wordfrequency - Frequency of words in the tweets\n
sentiment - Alchemy analysis on the tweets\n\n
OR\n
Type help to view list of commands\n'''
prompt = 'TWEET SENTIMENT ANALYSIS:> '
file = None
@docopt_cmd
def do_fetch(self, args):
"""Usage: fetch <username>"""
self.tweets = get_tweets((args['<username>']))
@docopt_cmd
def do_sentiment(self, args):
"""Usage: sentiment """
print(alchemy(' '.join(self.tweets)))
@docopt_cmd
def do_wordfrequency(self, args):
"""Usage: wordfrequency """
print(color(most_freq_words(tweets_to_words(self.tweets)), 'yellow'))
def do_home(self):
pass
def do_quit(self, args):
"""Quits out of Interactive Mode."""
print('Exiting ....')
exit()
opt = docopt(__doc__, sys.argv[1:])
if opt['--interactive']:
AnalyzerCmd().cmdloop()
print(opt)