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.