1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# encoding=utf8 |
3
|
|
|
""" |
4
|
|
|
Show the number of mentions per month for a word: |
5
|
|
|
word_usage -w twizzle |
6
|
|
|
word_usage -w bae -y 2014 |
7
|
|
|
word_usage -w twizzle -s favorite |
8
|
|
|
word_usage -w twizzle -s favourite |
9
|
|
|
""" |
10
|
|
|
from __future__ import print_function |
11
|
|
|
from most_frequent_words import commafy |
12
|
|
|
import argparse |
13
|
|
|
from word_charts import load_csv, filter_year, filter_search_term |
14
|
|
|
from collections import OrderedDict |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def filter_word(tweets, desired_word): |
18
|
|
|
found = [] |
19
|
|
|
for tweet in tweets: |
20
|
|
|
if desired_word == tweet["word"]: |
21
|
|
|
found.append(tweet) |
22
|
|
|
print("Total " + desired_word + ":\t" + commafy(len(found))) |
23
|
|
|
return found |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def print_per_month(tweets): |
27
|
|
|
counts = OrderedDict() |
28
|
|
|
for tweet in tweets: |
29
|
|
|
year = tweet["created_at"][-4:] |
30
|
|
|
month = tweet["created_at"][4:7] |
31
|
|
|
month_year = month + " " + year |
32
|
|
|
if month_year not in counts: |
33
|
|
|
counts[month_year] = 0 |
34
|
|
|
counts[month_year] += 1 |
35
|
|
|
|
36
|
|
|
# from pprint import pprint |
37
|
|
|
xs, ys = [], [] |
38
|
|
|
for month_year in counts: |
39
|
|
|
print(month_year, counts[month_year]) |
40
|
|
|
xs.append(month_year) |
41
|
|
|
ys.append(counts[month_year]) |
42
|
|
|
|
43
|
|
|
return xs, ys |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
if __name__ == "__main__": |
47
|
|
|
parser = argparse.ArgumentParser( |
48
|
|
|
description="Show the number of mentions per month for a word.", |
49
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
50
|
|
|
parser.add_argument( |
51
|
|
|
'-c', '--csv', default='M:/bin/data/all.csv', |
52
|
|
|
help='Input CSV file') |
53
|
|
|
parser.add_argument( |
54
|
|
|
'-w', '--word', default='bae', |
55
|
|
|
help='Word to search for') |
56
|
|
|
parser.add_argument( |
57
|
|
|
'-y', '--year', type=int, default=None, |
58
|
|
|
help="Only from this year") |
59
|
|
|
parser.add_argument( |
60
|
|
|
'-s', '--search_term', |
61
|
|
|
help="Only for this search term") |
62
|
|
|
parser.add_argument( |
63
|
|
|
'-p', '--plot', action='store_true', |
64
|
|
|
help="Create a bar graph of results (requires Plotly authentication: " |
65
|
|
|
"https://plot.ly/python/getting-started/") |
66
|
|
|
args = parser.parse_args() |
67
|
|
|
|
68
|
|
|
tweets = load_csv(args.csv) |
69
|
|
|
print("Total tweets:\t" + commafy(len(tweets))) |
70
|
|
|
|
71
|
|
|
tweets = filter_word(tweets, args.word) |
72
|
|
|
|
73
|
|
|
if args.search_term: |
74
|
|
|
tweets = filter_search_term(tweets, args.search_term) |
75
|
|
|
|
76
|
|
|
if args.year: |
77
|
|
|
tweets = filter_year(tweets, args.year) |
78
|
|
|
|
79
|
|
|
# Sort by ID = sort chronologically |
80
|
|
|
tweets = sorted(tweets, key=lambda k: k['id_str']) |
81
|
|
|
|
82
|
|
|
xs, ys = print_per_month(tweets) |
83
|
|
|
|
84
|
|
|
if args.plot: |
85
|
|
|
print("Plotting...") |
86
|
|
|
import plotly.plotly as py # pip install plotly + needs auth |
87
|
|
|
import plotly.graph_objs as go |
88
|
|
|
layout = go.Layout( |
89
|
|
|
title=args.word |
90
|
|
|
) |
91
|
|
|
trace0 = go.Scatter( |
92
|
|
|
x=xs, |
93
|
|
|
y=ys, |
94
|
|
|
mode='lines+markers', |
95
|
|
|
name=args.word |
96
|
|
|
) |
97
|
|
|
data = [trace0] |
98
|
|
|
fig = go.Figure(data=data, layout=layout) |
99
|
|
|
plot_url = py.plot(fig, filename='word_usage_' + args.word) |
100
|
|
|
print(plot_url) |
101
|
|
|
|
102
|
|
|
# End of file |
103
|
|
|
|