前提・実現したいこと
nltkのVADERというルールベースを用いて英単語の感情分析を行っています。
実行もできているのですが、csvファイルに書き込む際に本来、英単語の1列隣に結果を出力したいのですが、
1列横の2行下の位置に出てしまいます。うまくできないでしょうか??
発生している問題・エラーメッセージ
good
{'neg': 0.0 'neu': 0.0 'pos': 1.0 'compound': 0.4404}
該当のソースコード
python
1# -*- coding: utf-8 -*- 2 3import sys 4import os 5import math 6import codecs 7import time 8# import numpy as np 9from nltk.sentiment.vader import SentimentIntensityAnalyzer 10 11def main(): 12 13 start = time.time() 14 f = open("emotion.csv","w") 15 for word in codecs.open("adjective_1.csv","r", "cp932", "ignore"): 16 analyzer = SentimentIntensityAnalyzer() 17 write_word = analyzer.polarity_scores(word) 18 #print(test) 19 # f.write(write_word) 20 f.write("{},{}\n".format(word, write_word)) 21 f.close() 22 finish = time.time() 23 elapsed_time = finish - start 24 #print('elapsed_time = {}'.format(elapsed_time) 25 m, s = divmod(elapsed_time, 60) 26 h, m = divmod(m, 60) 27 print('elapsed_time: %d:%02d:%02d' % (h, m, s)) 28 29 30if __name__ == '__main__': 31 main() 32
あなたの回答
tips
プレビュー