-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizing.py
134 lines (105 loc) · 4.03 KB
/
visualizing.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Script to visualize annotation results over time.
"""
# === Imports ===
import pandas as pd
from pandas_ods_reader import read_ods
import pygal
import numpy as np
from pygal.style import BlueStyle
from pygal.style import CleanStyle
from pygal.style import LightSolarizedStyle
import re
# === Parameters ===
annotationsfile = "eltec-fra_samplesents_2022-Apr-11-08-36_annotated.ods"
# === Functions ===
def read_file(annotationsfile):
data = read_ods(annotationsfile)
#print(data.head())
num = int(sum(data["annotation"].value_counts()))
return data, num
def simple_viz(data, num):
data = data.drop(["filename", "year", "sentence", "sentnum"], axis=1)
overall = data["annotation"].value_counts()
overall_pc = np.around(np.divide(overall, np.sum(overall))*100,1)
pie = pygal.Pie(inner_radius=.4, legend_at_bottom=True, legend_at_bottom_columns=4, style=CleanStyle, print_values=True)
pie.title = "Types of sentences across all novels (percentages; n="+str(num)+")"
pie.add("narrator speech", overall_pc["n"])
pie.add("character speech", overall_pc["c"])
pie.add("mixed speech", overall_pc["x"])
pie.add("other", overall_pc["u"])
pie.render_to_file("sentence-types_overall.svg")
print("-- Saved visualization for overall data.")
def decade_viz(data, num):
data = data.drop(["filename", "year", "sentence", "sentnum"], axis=1)
data = data.fillna("na")
decdata = data.groupby(by="decade")
n = []
c = []
x = []
u = []
na = []
for name,group in decdata:
decade = group["annotation"].value_counts()
decade_pc = np.around(np.divide(decade, np.sum(decade))*100,0)
try:
n.append(decade_pc["n"])
except:
n.append(0)
try:
c.append(decade_pc["c"])
except:
c.append(0)
try:
x.append(decade_pc["x"])
except:
x.append(0)
try:
u.append(decade_pc["u"])
except:
u.append(0)
try:
na.append(decade_pc["na"])
except:
na.append(0)
bar = pygal.StackedBar(legend_at_bottom=True, legend_at_bottom_columns=4, style=CleanStyle, print_values=True)
bar.title = "Types of sentences per decade (percentages; n="+str(num)+")"
bar.y_title = "Percentages"
bar.x_labels = ["1840s","1850s","1860s","1870s","1880s","1890s","1900s","1910s"]
bar.add("narrator", n)
bar.add("character", c)
bar.add("mixed", x, formatter=lambda x: "".format(x))
bar.add("other", u, formatter=lambda x: "".format(x))
#bar.add("not annotated yet", na, formatter=lambda x: "".format(x))
bar.render_to_file("sentence-types_decades.svg")
print("-- Saved visualization for data by decade.")
def sentlen_viz(data, num):
data = data.drop(["filename", "decade", "year", "sentnum"], axis=1)
# Data preparation
types = data.groupby(by="annotation")
sentlendata = {}
for name, group in types:
sentlist = list(group["sentence"])
sentlens = []
for sent in sentlist:
sentlens.append(len(re.split("\W+", sent)))
sentlendata[name] = np.mean(sentlens)
#print(sentlendata)
# Visualization
bar = pygal.Bar(legend_at_bottom=True, legend_at_bottom_columns=4, style=CleanStyle, print_values=True)
bar.y_title = "Number of tokens"
bar.title = "Average sentence length by type of sentence (tokens; n="+str(num)+")"
bar.add("narrator", np.around(sentlendata["n"],1))
bar.add("character", np.around(sentlendata["c"],1))
bar.add("mixed", np.around(sentlendata["x"],1))
bar.add("other", np.around(sentlendata["u"],1))
bar.render_to_file("sentence-length-by-type.svg")
print("-- Saved visualization for sentence length by type.")
# === Main ===
def main(annotationsfile):
print("\nLaunched.")
data, num = read_file(annotationsfile)
simple_viz(data, num)
decade_viz(data, num)
sentlen_viz(data, num)
main(annotationsfile)