def print_chart(top): """ param top: a list of (word, count) tuples. """ if args.html: print("<ol>") for i, (word, count) in enumerate(top): if args.html: print("<li>{0} ({1})</li>".format(word, commafy(count))) else: print(str(i+1) + ". " + word + " (" + commafy(count) + ")") if args.html: print("</ol>")
def filter_word(tweets, desired_word): found = [] for tweet in tweets: if desired_word == tweet["word"]: found.append(tweet) print("Total " + desired_word + ":\t" + commafy(len(found))) return found
def filter_search_term(tweets, desired_search_term): found = [] for tweet in tweets: if desired_search_term in tweet["search_term"]: found.append(tweet) print("Total " + desired_search_term + ":\t" + commafy(len(found))) return found
def filter_year(tweets, desired_year): found = [] for tweet in tweets: year = int(tweet["created_at"][-4:]) if desired_year == year: found.append(tweet) print("Total in " + str(desired_year) + ":\t" + commafy(len(found))) return found
) parser.add_argument("-c", "--csv", default="M:/bin/data/all.csv", help="Input CSV file") parser.add_argument("-w", "--word", default="bae", help="Word to search for") parser.add_argument("-y", "--year", type=int, default=None, help="Only from this year") parser.add_argument("-s", "--search_term", help="Only for this search term") parser.add_argument( "-p", "--plot", action="store_true", help="Create a bar graph of results (requires Plotly authentication: " "https://plot.ly/python/getting-started/", ) args = parser.parse_args() tweets = load_csv(args.csv) print("Total tweets:\t" + commafy(len(tweets))) tweets = filter_word(tweets, args.word) if args.search_term: tweets = filter_search_term(tweets, args.search_term) if args.year: tweets = filter_year(tweets, args.year) # Sort by ID = sort chronologically tweets = sorted(tweets, key=lambda k: k["id_str"]) xs, ys = print_per_month(tweets) if args.plot:
parser.add_argument( '-y', '--year', type=int, default=None, help="Only from this year") parser.add_argument( '-s', '--search_term', help="Only for this search term") parser.add_argument( '--diff', action='store_true', help="Compare a year to the previous year") parser.add_argument( '--html', action='store_true', help="Output with html markup") args = parser.parse_args() tweets = load_csv(args.csv) print("Total tweets:\t" + commafy(len(tweets))) if args.diff and args.year: last_year = print_top(tweets, number=args.top_number, year=args.year-1, search_term=args.search_term) this_year = print_top(tweets, number=args.top_number, year=args.year, search_term=args.search_term) last_years_words = [e[0] for e in last_year] this_years_words = [e[0] for e in this_year] set1 = set(last_years_words) diff = [x for x in this_years_words if x not in set1] top = [] for word in diff: for e in this_year: