-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalisi_testuale.py
125 lines (97 loc) · 2.84 KB
/
analisi_testuale.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
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import string
from langdetect import detect
def language_detect(text):
if type(text) != str:
text = str(text)
else:
pass
if "[Instrumental]" in text:
return "instrumental"
if "nan" in text:
return "nan"
try:
lang = detect(text)
print(lang)
return lang
except:
return "nan"
def grafici1(df):
plt.figure(figsize=(20, 8))
sns.boxplot(data=df,
x="Genere",
y="Lex",
palette="Dark2_r",
)
plt.title('Lexical Diversity by Genre')
plt.tight_layout()
plt.show()
plt.figure(figsize=(20, 8))
sns.boxplot(data=df,
x="Genere",
y="lunghezza",
palette="Dark2_r",
)
plt.title('Lunghezza by Genre')
plt.tight_layout()
plt.show()
def grafici2(df):
ds = df.groupby(['Genere'])['lunghezza'].agg(['mean', 'std']).reset_index()
print(ds)
plt.figure(figsize=(20, 8))
sns.barplot(y=ds['mean'],
x=ds.Genere,
palette="Dark2_r",
)
plt.title('Numero parole medio per testi by Genre')
plt.tight_layout()
plt.show()
ds = df.groupby(['Genere'])['Lex'].agg(['mean', 'std']).reset_index()
plt.figure(figsize=(20, 8))
sns.barplot(y=ds['mean'],
x=ds.Genere,
palette="Dark2_r"
)
plt.title('Media Lexical Diversity by Genre')
plt.tight_layout()
plt.show()
ds = df.groupby(['Lingua'])['Lex'].agg(['mean', 'std']).reset_index()
print(ds)
plt.figure(figsize=(20, 8))
sns.barplot(y=ds['mean'],
x=ds.Lingua,
palette="Dark2_r"
)
plt.title('Lexical Diversity by Language')
plt.tight_layout()
plt.show()
ds = df.groupby(['Anno'])['Lex'].agg(['mean', 'std']).reset_index()
print(ds)
plt.figure(figsize=(20, 8))
sns.barplot(y=ds['mean'],
x=ds.Anno,
palette="Dark2_r"
)
plt.title('Lexical Diversity by year')
plt.tight_layout()
plt.show()
ds = df.groupby(['Anno'])['Lex'].agg(['mean', 'std']).reset_index()
print(ds)
plt.figure(figsize=(20, 8))
sns.lineplot(y=ds['mean'],
x=ds.Anno,
palette="Dark2_r"
)
plt.title('Lineplot Lexical Diversity by year')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
df = pd.read_csv("dataset/Dataset.csv")
for index, row in df.iterrows():
tok = row["Testo"].replace("\n", " ").translate(str.maketrans('', '', string.punctuation)).split(" ")
df.loc[index, 'lunghezza'] = len(tok)
df.loc[index, 'Lex'] = len(set(tok))
grafici1(df)
grafici2(df)