-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_wrang.py
More file actions
66 lines (55 loc) · 1.78 KB
/
data_wrang.py
File metadata and controls
66 lines (55 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from textblob import TextBlob
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import csv
# Converts numerical totals to percentages
def convert_to_perc(perc):
total = 0.0
for i in perc:
total += i
for i in range(len(perc)):
perc[i] = perc[i] / total
# Makes a pie chart of numerical data
def make_piechart(arr):
convert_to_perc(arr)
labels = 'Negative', 'Neutral', 'Positive'
fig1, ax1 = plt.subplots()
ax1.pie(arr, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.savefig('piechart.png')
# Makes a bar chart of numerical data
def make_barchart(arr):
labels = ['Negative', 'Neutral', 'Positive']
ypos = np.arange(len(labels))
plt.title("Sentiment Analysis - Bar Chart")
plt.ylabel("Count")
plt.xticks(ypos, labels)
plt.bar(ypos, arr, label='Count')
plt.legend()
plt.savefig('barchart.png')
# Tracks the count of neutral, positive, and negative polarity
def get_pol_sub(value, perc):
if value > 0:
perc[2] += 1
elif value == 0:
perc[1] += 1
else:
perc[0] += 1
# This is an updated implementation for the function in 'sent_ana.py'
def get_text_sub(data):
i = TextBlob(data).sentiment.subjectivity
get_pol_sub(i, sub_arr)
return i
# This is an updated implementation for the function in 'sent_ana.py'
def get_text_pol(data):
i = TextBlob(data).sentiment.polarity
get_pol_sub(i, pol_arr)
return i
# This will get the amount of times a word has been used
# Just need to find a way to pick a word that you want to check for
#def freq_of_words(data, word_to_check):
# i = TextBlob(data)
# i.word_counts[word_to_check]
# return i