-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_author_variability.py
66 lines (57 loc) · 1.86 KB
/
4_author_variability.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 23 10:27:26 2021
@author: UA - DLSI - RCC
Compute diversity for a sample of books and
present it in a plot as a function of the text length
"""
import sys, os
import configparser
import numpy as np
import matplotlib.pyplot as plt
from div import Text, BestFit
import zipfile
def load_params():
"""
Load configuration file
Returns
-------
dict
plot parameters.
"""
return config['LEXICAL']
if __name__ == '__main__':
if len(sys.argv) > 1:
archive_name = sys.argv[1]
else:
config = configparser.ConfigParser()
config.read('diversity.ini')
archive_name = config.get('AUTHOR', 'archive_name')
archive = zipfile.ZipFile(archive_name, 'r')
res = list()
for filename in archive.namelist():
content = archive.open(filename).read().decode('UTF-8')
text = Text(content)
stats = text.token_diversity(1000)
X = np.array(list(stats.keys()))
Y = np.array(list(stats.values()))
bf = BestFit('power')
p0 = (1000, 1, 10)
bounds = ([100, 0., 1], [2000, 10, 80000])
try:
pars = bf.fit(X, Y, p0=p0, bounds=bounds)
par_text = ', '.join(map(lambda x: f'{x:.1f}', pars))
print(filename, len(text), '\n\t', par_text)
res.append((len(text), pars[0]))
except RuntimeError:
print(filename, len(text), 'best fit not found\n')
X, Y = zip(*res)
plt.plot(X, Y, 'o')
print(f"Pearson's correlation = {np.corrcoef(X, Y)[0,1]:.2f}")
plt.xlabel('thousands of words')
plt.ylabel('Shannon diversity index')
plt.grid()
plt.tight_layout()
basename = '.'.join(os.path.basename(archive_name).split('.')[:-1])
plt.savefig(f'plots/{basename}.png', dpi=300)