Create wordcloud from your tweet with python3

Hit the twitter API with python3, Morphological analysis with MeCab Create a word cloud

Premise that the library is included

-Install anaconda -MeCab and wordcloud --twitter api

Get tweet using twitter API and output to csv

get_tweets.py


# coding: utf-8

import requests
from requests_oauthlib import OAuth1Session
import json
import csv

CK = "hhhhhhhhhhhhhhhhhhhhhh"
CS = "oooooooooooooooooooooooooooooooooooooooooo"
AT = "gggggggggggggggggggggggggggggggggggggggggggggggggg"
AS = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
oauth = OAuth1Session(CK, CS, AT, AS)

# url = "https://stream.twitter.com/1.1/statuses/filter.json" # get stream tweets #This may not have worked
# url = "https://stream.twitter.com/1.1/statuses/sample.json" # get sample tweets #This may not have worked
# url = "https://api.twitter.com/1.1/statuses/update.json" # post a tweet
# url = "https://api.twitter.com/1.1/search/tweets.json?" # search tweets
#You should be able to get the tweet of a specific public user by changing the username below(I wonder if I have to follow?)
url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username" # @Is unnecessary

params = {
	# "track": "a"
	# "lang": "ja"
	"count": "200"
	# "status": "Hello, world!"
}
req = oauth.get(
	url,
	# stream = True,
	params = params
	)
twitter = json.loads(req.text)
maxid = twitter[0]["id"] + 1

c = 0
tweets_list = []
for i in range(3):
	print(i)
	params = {
		"count": "200",
		"max_id": maxid
	}
	req = oauth.get(
		url,
		# max_id = maxid,
		params = params
		)
	twitter = json.loads(req.text)
	for tweet in twitter:
		tweets_list.append([
			c,
			tweet["id"],
			tweet["created_at"],
			tweet["text"]
			])
		maxid = tweet["id"] - 1
		c += 1

with open("tweets.csv", "w") as f:
	writer = csv.writer(f, lineterminator="\n")
	writer.writerow(tweets_list)

Create wordcloud by excluding replies and retweets from the acquired tweets

It is assumed that "@" and "RT" are included in the judgment of reply and retweet. If this process is unnecessary, you can write in list comprehension notation

wordcloud_tweets.py


import MeCab
from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv

stop_words = ["To do", "Absent", "Become", "Already", "Shiyo", "Can", "Became", "Ku", "Finally", "is there", "May", "think", "today"]
pos_list = [10, 11, 31, 32, 34]
pos_list.extend(list(range(36,50)))
pos_list.extend([59, 60, 62, 67])
def create_mecab_list(text):
	mecab_list = []
	mecab = MeCab.Tagger("-Ochasen -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd")
	mecab.parse("")
	# encoding = text.encode('utf-8')
	node = mecab.parseToNode(text)
	while node:
		# for sw in stop_words:
		# 	if node.surface == sw:
		# 		node = node.next
		if len(node.surface) > 1:
			if node.posid in pos_list:
				morpheme = node.surface
				mecab_list.append(morpheme)
		node = node.next
	return mecab_list

text_tweet = []
with open("./tweets.csv", "r") as file:
	reader = csv.reader(file)
	for tweets_text in reader:
		tweets_list = csv.reader(tweets_text)
		for ele in tweets_list:
			if "@" in ele[3]:
				continue
			if "RT" in ele[3]:
				continue
			text_tweet.append(ele[3])
text = "".join(text_tweet)
string = " ".join(create_mecab_list(text))#.decode("utf-8")

fpath = "/Library/Fonts/Hiragino Maru Go ProN W4.ttc"
wordcloud = WordCloud(
	background_color="black",
	stopwords=set(stop_words),
	max_font_size=56,
	relative_scaling=.4,
	width=500,
	height=300,
	font_path=fpath
	).generate(string)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file("./wordcloud.png ")

Recommended Posts

Create wordcloud from your tweet with python3
Create folders from '01' to '12' with python
Tweet from python with Twitter Developer + Tweepy
Create a decision tree from 0 with Python (1. Overview)
Memo to create your own Box with Pepper's Python
Create 3d gif with python3
Generate a random sentence from your tweet with trigram
Tweet with image in Python
Create a directory with python
With skype, notify with skype from python!
I made wordcloud with Python.
Create plot animation with Python + Matplotlib
Call C from Python with DragonFFI
Create Awaitable with Python / C API
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create a virtual environment with Python!
Create an Excel file with Python3
Run Aprili from Python with Orange
I liked the tweet with python. ..
Call python from nim with Nimpy
Read fbx from python with cinema4d
[Memo] Tweet on twitter with python
Collecting information from Twitter with Python (Twitter API)
Receive textual data from mysql with python
Create a Python function decorator with Class
Get html from element with Python selenium
Create your own Linux commands in Python
Automatically create Python API documentation with Sphinx
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Build a blockchain with Python ① Create a class
[LLDB] Create your own command in Python
Create a dummy image with Python + PIL.
Create your own DNS server with Twisted
Create a python environment on your Mac
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Create your own Composite Value with SQLAlchemy
Business efficiency starting from scratch with Python
Decrypt files encrypted with openssl from python with openssl
HTML document your Python program with Sphinx
Create Python + uWSGI + Nginx environment with Docker
Create and decrypt Caesar cipher with python
Working with Azure CosmosDB from Python Part.2
Image acquisition from camera with Python + OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Getting started with Dynamo from Python boto
Tweet analysis with Python, Mecab and CaboCha
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
Create your first app with Django startproject
Create a deb file from a python package
Try calling Python from Ruby with thrift
[Python] Quickly create an API with Flask
Scraping from an authenticated site with python
Publish your own Python library with Homebrew
Create an English word app with python
Use C ++ functions from python with pybind11
Create a tool to automatically furigana with html using Mecab from Python3