LoginSignup
0
0

More than 3 years have passed since last update.

ローカルマシンで感情分析

Last updated at Posted at 2021-02-26

最初はgoogle colaboratryを使っていたのですが、諸事情あってローカルマシンで文章の感情分析をしました。

環境

Virtual BoxのCentOS7
Python2.7.5
pip 21.0.1

必要なモジュールをインストール

pip3 install urllib3

urllibだとインストールできないので注意

pip3 bs4
pip3 re
pip3 matplotlib

辞書を作成

emotionsというディレクトリを作成。その下に感情に対する単語が書かれた
辞書を作成。name_uncoded.txtというように作成。

コード

コードはhttps://qiita.com/e10persona/items/7a7643b266c2bdfbf7d0
を参考にしました。

from bs4 import BeautifulSoup
import re
import numpy as np
import matplotlib.pyplot as plt


def get_emotional_words():
    emotions = ["donshoku", "inntou", "kinsenyoku", "ikari", "hitan", "taida", "kyoei", "kouman"]
    emotional_words = {}
    for emotion in emotions:
        emotional_words[emotion] = []
        with open("emotions/" + emotion + "_uncoded.txt", "r") as f:
            for line in f:
                line = line.replace('\n', '')
                emotional_words[emotion].append(line)
    return emotional_words


def count_emotional_words(sentences, emotional_words):
    count_emotions = [0] * len(emotional_words.keys())
    for idx, emotion in enumerate(emotional_words.keys()):
        for word in emotional_words[emotion]:
            count_emotions[idx] += sentences.count(word)
    return count_emotions


def main(url, title):
    sentences = url
    emotional_words = get_emotional_words()
    count_emotions = count_emotional_words(sentences, emotional_words)
    emotional_kanji = ["貪食", "淫蕩", "金銭欲", "怒り", "悲嘆", "怠惰", "虚栄", "高慢"]
    labels = list(emotional_kanji)
    values = count_emotions
    print(labels)
    print(count_emotions)

文章を入力

bun = "エロいこと考えてたらお腹が減りました"↲
main(bun, "文章")

適当な文章を入力

分析結果

['貪食','淫蕩','金銭欲','怒り','悲嘆','怠惰','虚栄','高慢']
[19,20,19,19,19,19,0,0]
辞書をちゃんと作り込まなかったので微妙なスコアになりましたが、分析はできました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0