forked from clips/styloscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylo_app.py
421 lines (360 loc) · 17.1 KB
/
stylo_app.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os, shutil
import util, visualizations, warnings
from statistics import mean, stdev
import pandas as pd
import gradio as gr
import spacy, pyphen
from spacy.matcher import Matcher
import numpy as np
#______________________________________________________________________________________________
stop_que = False
def stop_function():
"""
Changes flag used to track whether to stop the pipeline to True.
"""
global stop_que
stop_que = True
print("Process cancelled by user!")
return gr.update(visible=False), gr.update(visible=False)
def main(
input_type,
fn,
dataset_name,
subset,
split,
column_name,
lang,
readability_metric,
diversity_metric,
span_size,
unique_output_id,
error_or_canceled=True,
progress=gr.Progress(track_tqdm=True),
):
global stop_que # flag for tracking if cancel button has been pressed
progress(0, desc="Loading data...")
warnings.simplefilter(action='ignore', category=FutureWarning)
# first check if directory where all outputs are stored exists
main_dir_out = 'outputs'
if not os.path.exists(main_dir_out):
os.mkdir(main_dir_out)
# then create unique output dir for process
unique_dir_out = os.path.join(main_dir_out, unique_output_id)
if os.path.exists(unique_dir_out): # this should not happen in theory
shutil.rmtree(unique_dir_out)
os.mkdir(unique_dir_out)
os.mkdir(os.path.join(unique_dir_out, 'visualizations'))
# check cancel flag
if stop_que:
stop_que = False
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
error_or_canceled,
)
#LOAD_DATA_____________________________________________________________________________________
if input_type == 'Corpus':
format = 'csv' if fn[-3:] == 'csv' else 'zip'
if format == "zip":
column_name = 'text'
file_size = os.path.getsize(fn.name)
assert file_size < 1000000000 # ensure uploaded corpus is smaller than 1GB
print(file_size)
texts, infiles = util.load_data(format, fn, column_name, ',')
else: #Huggingface dataset
texts, infiles = util.load_huggingface(dataset_name, subset, split, column_name)
if stop_que:
stop_que = False
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
error_or_canceled
)
#PREPARE_OUTPUT_DIR____________________________________________________________________________
length_dfs = []
lexical_richness_dfs = []
readability_dfs = []
distribution_dfs = {
'punctuation_distribution': [],
'function_word_distribution': [],
'pos_profile': [],
'dependency_profile': [],
'word_length_distribution': [],
}
pos_outputs = []
dependency_outputs = []
#PREPROCESSING_________________________________________________________________________________
# Determine language
if lang == 'Dutch':
nlp = spacy.load("nl_core_news_lg")
dic = pyphen.Pyphen(lang='nl_NL')
elif lang == 'English':
nlp = spacy.load("en_core_web_lg")
dic = pyphen.Pyphen(lang='en')
elif lang == 'French':
nlp = spacy.load("fr_core_news_lg")
dic = pyphen.Pyphen(lang='fr_FR')
else:
nlp = spacy.load("de_core_news_lg")
dic = pyphen.Pyphen(lang='de')
# Initialize the SpaCy matcher with a vocab and passive rules
passive_rules = [
[{'DEP': 'nsubj:pass'}, {'DEP': 'aux:pass'}],
[{'DEP': 'aux:pass'}],
[{'DEP': 'nsubj:pass'}],
]
matcher = Matcher(nlp.vocab)
matcher.add('Passive', passive_rules)
print("Processing data...")
for text in progress.tqdm(texts, unit='documents processed'): # Analyze text by text
if stop_que:
stop_que = False
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
error_or_canceled
)
# check if text is empty
if not text.strip():
dummy_df = pd.DataFrame(data={'__Dummy__': ['dummy']}) # add dummy data to output
length_dfs.append(dummy_df)
lexical_richness_dfs.append(dummy_df)
readability_dfs.append(dummy_df)
for k in distribution_dfs.keys():
distribution_dfs[k].append(dummy_df)
continue # skip to the next text
text = ' '.join(text.split()) # remove redundant whitespace
# tokenization, parsing, etc.
doc = nlp(text)
parsed_sentences = [[(w.text, w.pos_) for w in s] for s in doc.sents]
pos_tags = [w.pos_ for s in doc.sents for w in s]
dependencies = [w.dep_ for s in doc.sents for w in s if w.dep_]
tokenized_sentences = [[t for t, pos in s if pos not in {'PUNCT', 'SYM', 'X'}] for s in parsed_sentences]
tokens = [t for s in parsed_sentences for t, pos in s if pos not in {'PUNCT', 'SYM', 'X'}]
types = set([t.lower() for t in tokens])
syllables = [[util.get_n_syllables(t, dic) for t, pos in s if pos not in {'PUNCT', 'SYM', 'X'}] for s in parsed_sentences]
# store parsing results
pos_outputs.append(' '.join(pos_tags))
dependency_outputs.append(' '.join(dependencies))
#LENGTH STATISTICS_____________________________________________________________________________
# check if text is empty (or contains only punctuation/symbols/other) -> skip
n_tokens = len(tokens)
n_char = len(text.replace(' ', ''))
n_syllables = sum([syl for sent in syllables for syl in sent])
n_polysyllabic = len([i for sent in syllables for i in sent if i > 1])
n_longer_than_6_char = len([t for t in tokens if len(t) > 6])
n_types = len(types)
n_sentences = len(list(doc.sents))
avg_char_per_word = mean([len(t) for t in tokens]) if n_tokens > 0 else 0
std_char_per_word = stdev([len(t) for t in tokens]) if n_tokens > 1 else 0
avg_syl_per_word = mean([s for sent in syllables for s in sent]) if n_tokens > 0 else 0
std_syl_per_word = stdev([s for sent in syllables for s in sent]) if n_tokens > 1 else 0
avg_words_per_sent = mean([len(s) for s in tokenized_sentences])
std_words_per_sent = stdev([len(s) for s in tokenized_sentences]) if n_sentences > 1 else 0
ratio_long_words = 0 if n_tokens == 0 else n_longer_than_6_char/n_tokens
ratio_content_words = util.ratio_content_words(doc)
ratio_passive_sentences = util.get_passive_ratio(doc, matcher)
stats = {
'n_characters': [n_char],
'n_syllables': [n_syllables],
'n_tokens': [n_tokens],
'n_polysyllabic_tokens': [n_polysyllabic],
'n_long_tokens': [n_longer_than_6_char],
'n_types': [n_types],
'n_sentences': [n_sentences],
'avg_characters_per_word': [avg_char_per_word],
'std_characters_per_word': [std_char_per_word],
'avg_syllables_per_word': [avg_syl_per_word],
'std_syllables_per_word': [std_syl_per_word],
'ratio_long_words': [ratio_long_words],
'ratio_content_words': [ratio_content_words],
'ratio_passive_sentences': [ratio_passive_sentences],
'avg_words_per_sentence': [avg_words_per_sent],
'std_words_per_sentence': [std_words_per_sent],
}
length_df = pd.DataFrame(data=stats)
length_dfs.append(length_df)
#LEXICAL DIVERSITY______________________________________________________________________________
if diversity_metric == 'TTR':
score = util.ttr(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'RTTR':
score = util.rttr(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'CTTR':
score = util.cttr(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'STTR':
score = util.sttr(tokens, int(span_size)) if n_tokens != 0 else None
elif diversity_metric == 'Herdan':
score = util.Herdan(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'Summer':
score = util.Summer(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'Dugast':
score = util.Dugast(n_types, n_tokens) if n_tokens != 0 else None
elif diversity_metric == 'Maas':
score = util.Maas(n_types, n_tokens) if n_tokens != 0 else None
else:
ValueError('Please provide one of the following lexical diversity metrics: "TTR", "RTTR", "CTTR", "STTR", "Herdan", "Summer", "Dugast", "Maas"')
lr = {
'score': [score],
}
lexical_richness_dfs.append(pd.DataFrame(data=lr))
#READABILITY___________________________________________________________________________________
if readability_metric == 'ARI':
score = util.ARI(n_char, n_tokens, n_sentences) if n_tokens != 0 else None
elif readability_metric == 'Coleman-Liau':
score = util.ColemanLiau(tokens, tokenized_sentences) if n_tokens != 0 else None
elif readability_metric == 'Flesch reading ease':
score = util.Flesch(avg_words_per_sent, avg_syl_per_word) if n_tokens != 0 else None
elif readability_metric == 'Flesch Kincaid grade level':
score = util.Kincaid(avg_words_per_sent, avg_syl_per_word) if n_tokens != 0 else None
elif readability_metric == 'Gunning Fog':
score = util.Fog(avg_words_per_sent, syllables) if n_tokens != 0 else None
if readability_metric == 'SMOG':
score = util.SMOG(syllables) if n_tokens != 0 else None
elif readability_metric == 'LIX':
score = util.LIX(n_tokens, n_sentences, n_longer_than_6_char) if n_tokens != 0 else None
elif readability_metric == 'RIX':
score = util.RIX(n_longer_than_6_char, n_sentences) if n_tokens != 0 else None
else:
ValueError('Please provide one of the following metrics: "ARI", "Coleman-Liau", "Flesch reading ease", "Flesch Kincaid grade level", "Gunning Fog", "SMOG", "LIX", "RIX".')
interpretation = util.interpret_readability(score, readability_metric)
readability = {
'score': [score],
'interpretation': [interpretation]
}
readability_dfs.append(pd.DataFrame(data=readability))
#DISTRIBUTIONS_________________________________________________________________________________
punct_dist = util.get_punct_dist(text)
function_word_distribution = util.get_function_word_distribution(doc)
pos_profile = util.get_ngram_profile(pos_tags)
dependency_profile = util.get_dependency_distribution(dependencies)
word_length_distribution = util.get_word_length_distribution(tokens)
dist = {
'punctuation_distribution': punct_dist,
'function_word_distribution': function_word_distribution,
'pos_profile': pos_profile,
'dependency_profile': dependency_profile,
'word_length_distribution': word_length_distribution,
}
for dist_name in dist.keys():
df = pd.DataFrame(data=dist[dist_name])
distribution_dfs[dist_name] = distribution_dfs[dist_name] + [df]
#WRITE RESULTS TO OUTPUT_______________________________________________________________________
print("Aggregating results, creating visualizations, and saving raw results...")
progress(1, desc="Aggregating data. Please wait...")
# length statistics
print(' ...length statistics')
length_df = pd.concat(length_dfs, axis=0)
length_df = length_df.drop(columns=['__Dummy__']) if '__Dummy__' in length_df.columns else length_df
length_df.insert(0, 'doc', infiles)
mean_length_df = length_df.mean().to_frame().T
mean_length_df['doc'] = 'mean'
std_length_df = length_df.std().to_frame().T
std_length_df['doc'] = 'std'
length_df = pd.concat([length_df, mean_length_df, std_length_df])
length_df = length_df.round(3)
length_df.to_csv(os.path.join(unique_dir_out, 'length_statistics.csv'), index=False)
# readability statistics
print(' ...readability statistics')
readability_df = pd.concat(readability_dfs, axis=0)
readability_df = readability_df.drop(columns=['__Dummy__']) if '__Dummy__' in readability_df.columns else readability_df
readability_df.insert(0, 'doc', infiles)
mean_readability_df = readability_df.mean().to_frame().T
mean_readability_df['doc'] = 'mean'
mean_readability_df = mean_readability_df.round(3)
std_readability_df = readability_df.std().to_frame().T
std_readability_df['doc'] = 'std'
std_readability_df = std_readability_df.round(3)
readability_df = pd.concat([readability_df, mean_readability_df, std_readability_df])
readability_df.to_csv(os.path.join(unique_dir_out, 'readability_statistics.csv'), index=False)
# lexical richness statistics
print(' ...lexical richness statistics')
lexical_richness_df = pd.concat(lexical_richness_dfs, axis=0)
lexical_richness_df = lexical_richness_df.drop(columns=['__Dummy__']) if '__Dummy__' in lexical_richness_df.columns else lexical_richness_df
lexical_richness_df.insert(0, 'doc', infiles)
mean_lexical_richness_df = lexical_richness_df.mean().to_frame().T
mean_lexical_richness_df['doc'] = 'mean'
mean_lexical_richness_df = mean_lexical_richness_df.round(3)
std_lexical_richness_df = lexical_richness_df.std().to_frame().T
std_lexical_richness_df['doc'] = 'std'
std_lexical_richness_df = std_lexical_richness_df.round(3)
lexical_richness_df = pd.concat([lexical_richness_df, mean_lexical_richness_df, std_lexical_richness_df])
lexical_richness_df.to_csv(os.path.join(unique_dir_out, 'lexical_richness_statistics.csv'), index=False)
# parsing results
parsing_df = pd.DataFrame(data={
'document': infiles,
'part-of-speech tags': pos_outputs,
'syntactic dependencies': dependency_outputs,
})
parsing_df.to_csv(os.path.join(unique_dir_out, 'parsing_results.csv'), index=False)
# distributions
print(' ...distributions')
for k in dist.keys():
distribution_dfs[k] = [df if not df.empty else pd.DataFrame([np.nan], columns=['__Empty__']) for df in distribution_dfs[k]] # when there is an empty dataframe, pd.concat ignores this leading to incongruencies in length
df = pd.concat(distribution_dfs[k], axis=0).fillna(0)
df = df.drop(columns=['__Dummy__']) if '__Dummy__' in df.columns else df
df = df.drop(columns=['__Empty__']) if '__Empty__' in df.columns else df
df.insert(0, 'doc', infiles)
mean_df = df.mean().to_frame().T
std_df = df.std().to_frame().T
mean_df['doc'] = 'mean'
std_df['doc'] = 'std'
df = pd.concat([df, mean_df, std_df])
df = df.round(3)
df.to_csv(os.path.join(unique_dir_out, f'{k}.csv'), index=False)
# visualizations
if k != 'function_word_distribution':
df.insert(0, 'source', ['input corpus']*len(df))
mean_df, std_df = visualizations.prepare_df(df, k, lang)
plt = visualizations.generate_bar_chart(mean_df, std_df, k, unique_dir_out)
if k == 'punctuation_distribution':
punct_plot = plt
elif k == 'dependency_profile':
dep_plot = plt
elif k == 'pos_profile':
pos_plot = plt
elif k == 'word_length_distribution':
len_plot = plt
else:
pass
basic_statistics = pd.DataFrame(data={
'Corpus statistics': ['n Tokens', 'n Sentences', 'n Syllables', 'n Characters', 'Lexical diversity', 'Readability'],
'Mean': [
length_df.iloc[-2]['n_tokens'],
length_df.iloc[-2]['n_sentences'],
length_df.iloc[-2]['n_syllables'],
length_df.iloc[-2]['n_characters'],
lexical_richness_df.iloc[-2]['score'],
readability_df.iloc[-2]['score']
],
'Std.': [
length_df.iloc[-1]['n_tokens'],
length_df.iloc[-1]['n_sentences'],
length_df.iloc[-1]['n_syllables'],
length_df.iloc[-1]['n_characters'],
lexical_richness_df.iloc[-1]['score'],
readability_df.iloc[-1]['score']
]
})
progress(1, desc="Done!")
error_or_canceled='' # False when cast to boolean
return (
shutil.make_archive(base_name=os.path.join(unique_dir_out), format='zip', base_dir=unique_dir_out),
basic_statistics,
dep_plot,
pos_plot,
punct_plot,
len_plot,
error_or_canceled
)