/
text_properties.py
531 lines (449 loc) · 21.3 KB
/
text_properties.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2023 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""Module containing the text properties for the NLP module."""
import importlib
import pathlib
import string
import warnings
from typing import Dict, List, Optional, Sequence, Tuple, Union
import numpy as np
import pandas as pd
import requests
import textblob
from nltk import corpus
from nltk import download as nltk_download
from nltk import sent_tokenize, word_tokenize
from deepchecks.nlp.utils.text import remove_punctuation
from deepchecks.utils.function import run_available_kwargs
__all__ = ['calculate_default_properties']
MODELS_STORAGE = pathlib.Path(__file__).absolute().parent / '.nlp-models'
FASTTEXT_LANG_MODEL = 'https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin'
def _import_optional_property_dependency(
module: str,
property_name: str,
package_name: Optional[str] = None,
error_template: Optional[str] = None
):
try:
lib = importlib.import_module(module)
except ImportError as error:
package_name = package_name or module.split('.', maxsplit=1)[0]
error_template = error_template or (
'property {property_name} requires the {package_name} python package. '
'To get it, run:\n'
'>> pip install {package_name}\n\n'
'You may install dependencies for all text properties by running:\n'
'>> pip install deepchecks[nlp-properties]\n'
)
raise ImportError(error_template.format(
property_name=property_name,
package_name=package_name
)) from error
else:
return lib
def get_creat_model_storage(models_storage: Union[pathlib.Path, str, None] = None):
"""Get the models storage directory and create it if needed."""
if models_storage is None:
models_storage = MODELS_STORAGE
else:
if isinstance(models_storage, str):
models_storage = pathlib.Path(models_storage)
if not isinstance(models_storage, pathlib.Path):
raise ValueError(
f'Unexpected type of the "models_storage" parameter - {type(models_storage)}'
)
if not models_storage.exists():
models_storage.mkdir(parents=True)
if not models_storage.is_dir():
raise ValueError('"model_storage" expected to be a directory')
return models_storage
def get_transformer_model(
property_name: str,
model_name: str,
device: Optional[str] = None,
quantize_model: bool = False,
models_storage: Union[pathlib.Path, str, None] = None
):
"""Get the transformer model and decide if to use optimum.onnxruntime.
optimum.onnxruntime is used to optimize running times on CPU.
"""
models_storage = get_creat_model_storage(models_storage)
if device not in (None, 'cpu'):
transformers = _import_optional_property_dependency('transformers', property_name=property_name)
# TODO: quantize if 'quantize_model' is True
return transformers.AutoModelForSequenceClassification.from_pretrained(
model_name,
cache_dir=models_storage
)
onnx = _import_optional_property_dependency(
'optimum.onnxruntime',
property_name=property_name,
error_template=(
f'The device was set to {device} while computing the {property_name} property,'
'in which case deepchecks resorts to accelerating the inference by using optimum,'
'bit it is not installed. Either:\n'
'\t- Set the device according to your hardware;\n'
'\t- Install optimum by running "pip install optimum";\n'
'\t- Install all dependencies needed for text properties by running '
'"pip install deepchecks[nlp-properties]";\n'
)
)
if quantize_model is False:
model_path = models_storage / 'onnx' / model_name
if model_path.exists():
return onnx.ORTModelForSequenceClassification.from_pretrained(model_path)
model = onnx.ORTModelForSequenceClassification.from_pretrained(
model_name,
export=True,
cache_dir=models_storage
)
# NOTE:
# 'optimum', after exporting/converting a model to the ONNX format,
# does not store it onto disk we need to save it now to not reconvert
# it each time
model.save_pretrained(model_path)
return model
model_path = models_storage / 'onnx' / 'quantized' / model_name
if model_path.exists():
return onnx.ORTModelForSequenceClassification.from_pretrained(model_path)
not_quantized_model = get_transformer_model(
property_name,
model_name,
device,
quantize_model=False,
models_storage=models_storage
)
quantizer = onnx.ORTQuantizer.from_pretrained(not_quantized_model)
quantizer.quantize(
save_dir=model_path,
# TODO: make it possible to provide a config as a parameter
quantization_config=onnx.configuration.AutoQuantizationConfig.avx512_vnni(
is_static=False,
per_channel=False
)
)
return onnx.ORTModelForSequenceClassification.from_pretrained(model_path)
def get_transformer_pipeline(
property_name: str,
model_name: str,
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None
):
"""Return a transformers pipeline for the given model name."""
transformers = _import_optional_property_dependency('transformers', property_name=property_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
model = get_transformer_model(
property_name=property_name,
model_name=model_name,
device=device,
models_storage=models_storage
)
return transformers.pipeline(
'text-classification',
model=model,
tokenizer=tokenizer,
device=device
)
def text_length(raw_text: Sequence[str]) -> List[int]:
"""Return list of integers of text lengths."""
return [len(text) for text in raw_text]
def word_length(raw_text: Sequence[str]) -> List[int]: # Not yet used as returns list per sample and not number
"""Return list of integers of word lengths."""
return [len(word) for text in raw_text for word in text.split()]
def average_word_length(raw_text: Sequence[str]) -> List[float]:
"""Return list of floats of average word length."""
return [np.mean([len(word) for word in text.split()]) for text in raw_text]
def percentage_special_characters(raw_text: Sequence[str]) -> List[float]:
"""Return list of floats of percentage of special characters."""
return [len([c for c in text if c in string.punctuation]) / len(text) for text in raw_text]
def max_word_length(raw_text: Sequence[str]) -> List[int]:
"""Return list of integers of max word length."""
return [max([len(word) for word in text.split()]) for text in raw_text]
def language(raw_text: Sequence[str],
models_storage: Union[pathlib.Path, str, None] = None,
lang_certainty_threshold: float = 0.8
) -> List[str]:
"""Return list of strings of language."""
fasttext = _import_optional_property_dependency(module='fasttext', property_name='language')
model_name = FASTTEXT_LANG_MODEL.rsplit('/', maxsplit=1)[-1]
model_path = get_creat_model_storage(models_storage)
model_path = model_path / 'fasttext'
if not model_path.exists():
model_path.mkdir(parents=True)
model_path = model_path / model_name
# Save the model to a file
if not model_path.exists():
response = requests.get(FASTTEXT_LANG_MODEL)
with open(model_path, 'wb') as f:
f.write(response.content)
# This weird code is to suppress a warning from fasttext about a deprecated function
try:
fasttext.FastText.eprint = lambda *args, **kwargs: None
model = fasttext.load_model(str(model_path))
except Exception as exp:
raise exp
# Predictions are the first prediction (k=1), only if the probability is above the threshold
predictions = model.predict(list(raw_text), k=1, threshold=lang_certainty_threshold)
# x is empty for detection below threshold
language_codes = [x[0].replace('__label__', '') if x else np.nan for x in predictions[0]]
return language_codes
def sentiment(raw_text: Sequence[str]) -> List[str]:
"""Return list of floats of sentiment."""
return [textblob.TextBlob(text).sentiment.polarity for text in raw_text]
def subjectivity(raw_text: Sequence[str]) -> List[str]:
"""Return list of floats of subjectivity."""
return [textblob.TextBlob(text).sentiment.subjectivity for text in raw_text]
def toxicity(
raw_text: Sequence[str],
device: Optional[int] = None,
models_storage: Union[pathlib.Path, str, None] = None
) -> List[float]:
"""Return list of floats of toxicity."""
model_name = 'unitary/toxic-bert'
classifier = get_transformer_pipeline(
'toxicity',
model_name,
device=device,
models_storage=models_storage
)
return [x['score'] for x in classifier(raw_text)]
def fluency(
raw_text: Sequence[str],
device: Optional[int] = None,
models_storage: Union[pathlib.Path, str, None] = None
) -> List[float]:
"""Return list of floats of fluency."""
model_name = 'prithivida/parrot_fluency_model'
classifier = get_transformer_pipeline(
'fluency',
model_name,
device=device,
models_storage=models_storage
)
return [x['score'] if x['label'] == 'LABEL_1' else 1 - x['score'] for x in classifier(raw_text)]
def formality(
raw_text: Sequence[str],
device: Optional[int] = None,
models_storage: Union[pathlib.Path, str, None] = None
) -> List[float]:
"""Return list of floats of formality."""
model_name = 's-nlp/roberta-base-formality-ranker'
classifier = get_transformer_pipeline(
'formality',
model_name,
device=device,
models_storage=models_storage
)
return [x['score'] if x['label'] == 'formal' else 1 - x['score'] for x in classifier(raw_text)]
def lexical_density(raw_text: Sequence[str]) -> List[str]:
"""Return a list of floats of lexical density per text sample.
Lexical density is the percentage of unique words in a given text. For more
information: https://en.wikipedia.org/wiki/Lexical_density
"""
if not nltk_download('punkt', quiet=True):
warnings.warn('nltk punkt not found, lexical density cannot be calculated.'
' Please check your internet connection.', UserWarning)
return [np.nan] * len(raw_text)
result = []
for text in raw_text:
if not pd.isna(text):
all_words = textblob.TextBlob(text).words
if len(all_words) == 0:
result.append(np.nan)
else:
total_unique_words = len(set(all_words))
text_lexical_density = round(total_unique_words * 100 / len(all_words), 2)
result.append(text_lexical_density)
else:
result.append(np.nan)
return result
def unique_noun_count(raw_text: Sequence[str]) -> List[str]:
"""Return a list of integers of number of unique noun words in the text."""
if not nltk_download('averaged_perceptron_tagger', quiet=True):
warnings.warn('nltk averaged_perceptron_tagger not found, unique noun count cannot be calculated.'
' Please check your internet connection.', UserWarning)
return [np.nan] * len(raw_text)
result = []
for text in raw_text:
if not pd.isna(text):
unique_words_with_tags = set(textblob.TextBlob(text).tags)
result.append(sum(1 for (_, tag) in unique_words_with_tags if tag.startswith('N')))
else:
result.append(np.nan)
return result
def readability_score(raw_text: Sequence[str]) -> List[str]:
"""Return a list of floats of Flesch Reading-Ease score per text sample.
In the Flesch reading-ease test, higher scores indicate material that is easier to read
whereas lower numbers mark texts that are more difficult to read. For more information:
https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests#Flesch_reading_ease
"""
if not nltk_download('punkt', quiet=True):
warnings.warn('nltk punkt not found, readability score cannot be calculated.'
' Please check your internet connection.', UserWarning)
return [np.nan] * len(raw_text)
if not nltk_download('cmudict', quiet=True):
warnings.warn('nltk cmudict not found, readability score cannot be calculated.'
' Please check your internet connection.', UserWarning)
return [np.nan] * len(raw_text)
result = []
cmudict_dict = corpus.cmudict.dict()
for text in raw_text:
if not pd.isna(text):
sentence_count = len(sent_tokenize(text))
text = remove_punctuation(text)
words = word_tokenize(text)
word_count = len(words)
syllable_count = sum([len(cmudict_dict[word.lower()]) for word in words if word.lower() in cmudict_dict])
if word_count != 0 and sentence_count != 0 and syllable_count != 0:
avg_syllables_per_word = syllable_count / word_count
avg_words_per_sentence = word_count / sentence_count
flesch_reading_ease = 206.835 - (1.015 * avg_words_per_sentence) - (84.6 * avg_syllables_per_word)
result.append(round(flesch_reading_ease, 3))
else:
result.append(np.nan)
else:
result.append(np.nan)
return result
def average_sentence_length(raw_text: Sequence[str]) -> List[str]:
"""Return a list of floats denoting the average sentence length per text sample."""
if not nltk_download('punkt', quiet=True):
warnings.warn('nltk punkt not found, average sentence length cannot be calculated.'
' Please check your internet connection.', UserWarning)
return [np.nan] * len(raw_text)
result = []
for text in raw_text:
if not pd.isna(text):
sentences = [remove_punctuation(sent) for sent in sent_tokenize(text)]
total_words = sum([len(word_tokenize(sentence)) for sentence in sentences])
if len(sentences) != 0:
asl = total_words / len(sentences)
result.append(round(asl, 0))
else:
result.append(np.nan)
else:
result.append(np.nan)
return result
DEFAULT_PROPERTIES = (
{'name': 'Text Length', 'method': text_length, 'output_type': 'numeric'},
{'name': 'Average Word Length', 'method': average_word_length, 'output_type': 'numeric'},
{'name': 'Max Word Length', 'method': max_word_length, 'output_type': 'numeric'},
{'name': '% Special Characters', 'method': percentage_special_characters, 'output_type': 'numeric'},
{'name': 'Language', 'method': language, 'output_type': 'categorical'},
{'name': 'Sentiment', 'method': sentiment, 'output_type': 'numeric'},
{'name': 'Subjectivity', 'method': subjectivity, 'output_type': 'numeric'},
{'name': 'Toxicity', 'method': toxicity, 'output_type': 'numeric'},
{'name': 'Fluency', 'method': fluency, 'output_type': 'numeric'},
{'name': 'Formality', 'method': formality, 'output_type': 'numeric'},
{'name': 'Lexical Density', 'method': lexical_density, 'output_type': 'numeric'},
{'name': 'Unique Noun Count', 'method': unique_noun_count, 'output_type': 'numeric'},
{'name': 'Readability Score', 'method': readability_score, 'output_type': 'numeric'},
{'name': 'Average Sentence Length', 'method': average_sentence_length, 'output_type': 'numeric'},
)
LONG_RUN_PROPERTIES = ['Toxicity', 'Fluency', 'Formality', 'Unique Noun Count']
ENGLISH_ONLY_PROPERTIES = ['Sentiment', 'Subjectivity', 'Toxicity', 'Fluency', 'Formality']
LARGE_SAMPLE_SIZE = 10_000
def _get_default_properties(
include_properties: Optional[List[str]] = None,
ignore_properties: Optional[List[str]] = None
):
"""Return the default properties.
Default properties are defined here and not outside the function so not to import all the packages
if they are not needed.
"""
properties = DEFAULT_PROPERTIES
# Filter by properties or ignore_properties:
if include_properties is not None and ignore_properties is not None:
raise ValueError('Cannot use properties and ignore_properties parameters together.')
elif include_properties is not None:
properties = [prop for prop in properties if prop['name'] in include_properties]
elif ignore_properties is not None:
properties = [prop for prop in properties if prop['name'] not in ignore_properties]
return properties
def calculate_default_properties(
raw_text: Sequence[str],
include_properties: Optional[List[str]] = None,
ignore_properties: Optional[List[str]] = None,
include_long_calculation_properties: Optional[bool] = False,
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None
) -> Tuple[Dict[str, List[float]], Dict[str, str]]:
"""Calculate properties on provided text samples.
Parameters
----------
raw_text : Sequence[str]
The text to calculate the properties for.
include_properties : List[str], default None
The properties to calculate. If None, all default properties will be calculated. Cannot be used together
with ignore_properties parameter. Available properties are:
['Text Length', 'Average Word Length', 'Max Word Length', '% Special Characters', 'Language',
'Sentiment', 'Subjectivity', 'Toxicity', 'Fluency', 'Formality', 'Lexical Density', 'Unique Noun Count',
'Readability Score', 'Average Sentence Length']
Note that the properties ['Toxicity', 'Fluency', 'Formality', 'Language', 'Unique Noun Count'] may
take a long time to calculate. If include_long_calculation_properties is False, these properties will be
ignored, even if they are in the include_properties parameter.
ignore_properties : List[str], default None
The properties to ignore. If None, no properties will be ignored. Cannot be used together with
properties parameter.
include_long_calculation_properties : bool, default False
Whether to include properties that may take a long time to calculate. If False, these properties will be
ignored, even if they are in the include_properties parameter.
device : int, default None
The device to use for the calculation. If None, the default device will be used.
models_storage : Union[str, pathlib.Path, None], default None
A directory to store the models.
If not provided, models will be stored in `DEEPCHECKS_LIB_PATH/nlp/.nlp-models`.
Also, if a folder already contains relevant resources they are not re-downloaded.
Returns
-------
Dict[str, List[float]]
A dictionary with the property name as key and a list of the property values for each text as value.
Dict[str, str]
A dictionary with the property name as key and the property's type as value.
"""
raw_text = list(raw_text)
default_text_properties = _get_default_properties(
include_properties=include_properties,
ignore_properties=ignore_properties
)
if not include_long_calculation_properties:
default_text_properties = [
prop for prop in default_text_properties
if prop['name'] not in LONG_RUN_PROPERTIES
]
else: # Check if the run may take a long time and warn
heavy_properties = [prop for prop in default_text_properties if prop['name'] in LONG_RUN_PROPERTIES]
if heavy_properties and len(raw_text) > LARGE_SAMPLE_SIZE:
h_prop_names = [prop['name'] for prop in heavy_properties]
warning_message = f'Calculating the properties {h_prop_names} on a large dataset may take a long time.' \
f' Consider using a smaller sample size or running this code on better hardware.'
if device is None or device == 'cpu':
warning_message += ' Consider using a GPU or a similar device to run these properties.'
warnings.warn(warning_message, UserWarning)
calculated_properties = {}
for prop in default_text_properties:
try:
calculated_properties[prop['name']] = run_available_kwargs(
prop['method'],
raw_text=raw_text,
device=device,
models_storage=models_storage
)
except ImportError as e:
warnings.warn(f'Failed to calculate property {prop["name"]}.\nError: {e}')
if not calculated_properties:
raise RuntimeError('Failed to calculate any of the properties.')
# TODO: Add tests
properties_types = {
prop['name']: prop['output_type']
for prop in default_text_properties
if prop['name'] in calculated_properties
}
return calculated_properties, properties_types