A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.

I've been touching the Twitter API for a long time, so I finally decided to try morphological analysis. The following is the source where I have a feeling that I can develop various things from here and play.

meca.py



#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
import sys
import MeCab

C_KEY = "********************************"
C_SECRET = "********************************"
A_KEY = "********************************"
A_SECRET = "********************************"

def Home_timeline():
	url = "https://api.twitter.com/1.1/statuses/home_timeline.json"
	params = {
				"lang": "ja",
				"count": "100"
				}
	tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
	req = tw.get(url, params = params)
	tweets = json.loads(req.text)
	for tweet in tweets:
		f = open("test.txt" , "aw")
		lists = (tweet["text"].encode("utf-8"))
			
		f.write(lists)
		f.flush()
		f.close()

def Mecab_file():	
	f = open("test.txt","rb")
	data = f.read()
	f.close()
	
	mt = MeCab.Tagger("-Ochasen")
	print mt.parse(data)


Home_timeline()
Mecab_file()

It's still not beautiful, but I think the process it's doing is easy to understand. I don't know if f.flush () is needed, but just in case. After that, it is handed over to Mecab for morphological analysis. The result of morphological analysis is displayed by parse, but I want to modify it so that it is easy to see.

When importing the MeCab module, codec interfered, so I only import sys.

Recommended Posts

A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.
A Python program that collects tweets containing specific keywords daily and saves them in csv
A Python script that reads a SQL file, executes BigQuery and saves the csv
Create an exe file that works in a Windows environment without Python with PyInstaller
A note that runs an external program in Python and parses the resulting line
A Python script that saves a clipboard (GTK) image to a file.
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
A Python script that crawls RSS in Azure Status and posts it to Hipchat
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
File overwrite confirmation with option that takes a file object as an argument with Python argparse
A memorandum to run a python script in a bat file
What's in that variable (when running a Python script)
Run a Python file with relative import in PyCharm
I made a program to collect images in tweets that I liked on twitter with Python
Collect tweets using tweepy in Python and save them in MongoDB
Create code that outputs "A and pretending B" in python
A set of script files that do wordcloud in Python3
Morphological analysis using Igo + mecab-ipadic-neologd in Python (with Ruby bonus)
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
Get 1000 posts in Python order from all Slack channels and put them together in a txt file
[Python] Morphological analysis with MeCab
Japanese morphological analysis with Python
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
A story about a magic trick in LT that performs live coding of natural language processing and dependency analysis in an instant from nothing.
Process the contents of the file in order with a shell script
I made a package that can compare morphological analyzers with Python
How to create a heatmap with an arbitrary domain in Python
Draw a watercolor illusion with edge detection in Python3 and openCV3
Read a file in Python with a relative path from the program
Get the formula in an excel file as a string in Python
A python script that deletes ._DS_Store and ._ * files created on Mac
Write a C language linked list in an object-oriented style (with code comparison between Python and Java)
Create an Excel file with Python3
Text mining with Python ① Morphological analysis
Create a binary file in Python
Write a batch script with Python3.5 ~
Replace the directory name and the file name in the directory together with a Linux command.
Read and format a csv file mixed with comma tabs with Python pandas
A memo organized by renaming the file names in the folder with python
Tips: [Python] Randomly restore and extract an array from a fasta file
Extract lines that match the conditions from a text file with python
Read a Python # .txt file for a super beginner in Python with a working .py
Creating an exe file with Python PyInstaller: PC freezes in parallel processing
A python program that resizes a video and turns it into an image
Open an Excel file in Python and color the map of Japan
The eval () function that calculates a string as an expression in python
Get the return value of an external shell script (ls) with python3
Create an application that inputs, displays, and deletes forms by using an array as a DB with Python / Flask.