-
Notifications
You must be signed in to change notification settings - Fork 247
/
text_properties.py
1012 lines (843 loc) · 43.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ----------------------------------------------------------------------------
# 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 gc
import importlib
import pathlib
import re
import string
import warnings
from collections import defaultdict
from typing import Any, Callable, 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 tqdm import tqdm
from typing_extensions import TypedDict
from deepchecks.core.errors import DeepchecksValueError
from deepchecks.nlp.utils.text import cut_string, hash_text, normalize_text, remove_punctuation
from deepchecks.utils.function import run_available_kwargs
from deepchecks.utils.strings import format_list
__all__ = ['calculate_builtin_properties', 'get_builtin_properties_types']
from deepchecks.utils.validation import is_sequence_not_str
MODELS_STORAGE = pathlib.Path(__file__).absolute().parent / '.nlp-models'
FASTTEXT_LANG_MODEL = 'https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin'
DEFAULT_SENTENCE_SAMPLE_SIZE = 300
MAX_CHARS = 512 # Bert accepts max of 512 tokens, so without counting tokens we go for the lower bound.
textblob_cache = {}
words_cache = {}
sentences_cache = {}
secret_cache = {}
def _split_to_words_with_cache(text: str) -> List[str]:
"""Tokenize a text into words and cache the result."""
hash_key = hash_text(text)
if hash_key not in words_cache:
words = re.split(r'\W+', normalize_text(text, remove_stops=False, ignore_whitespace=False))
words = [w for w in words if w] # remove empty strings
words_cache[hash_key] = words
return words_cache[hash_key]
def _split_to_sentences_with_cache(text: str) -> Union[List[str], None]:
"""Tokenize a text into sentences and cache the result."""
hash_key = hash_text(text)
if hash_key not in sentences_cache:
if not nltk_download('punkt', quiet=True):
_warn_if_missing_nltk_dependencies('punkt', 'property')
return None
sentences_cache[hash_key] = sent_tokenize(text)
return sentences_cache[hash_key]
def _sample_for_property(text: str, mode: str = 'words', limit: int = 10000, return_as_list=False,
random_seed: int = 42) -> Union[str, List[str]]:
"""Get a sample a single text sample for a text property.
Parameters
----------
text : str
The text to sample from.
mode : str, default 'words'
The mode to sample in. Can be either 'words' or 'sentences'.
limit : int, default 10000
The maximum number of words or sentences to sample.
"""
np.random.seed(random_seed)
if pd.isna(text):
return None
if mode == 'words':
all_units = _split_to_words_with_cache(text)
if len(all_units) > limit:
all_units = np.random.choice(all_units, size=limit, replace=False)
elif mode == 'sentences':
all_units = _split_to_sentences_with_cache(text)
if len(all_units) > limit:
all_units = np.random.choice(all_units, size=limit, replace=False)
else:
raise DeepchecksValueError(f'Unexpected mode - {mode}')
return ' '.join(all_units) if not return_as_list else list(all_units)
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 _warn_if_missing_nltk_dependencies(dependency: str, property_name: str):
"""Warn if NLTK dependency is missing."""
warnings.warn(f'NLTK {dependency} not found, {property_name} cannot be calculated.'
' Please check your internet connection.', UserWarning)
def get_create_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_create_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,
device_map=device
)
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, device_map=device)
model = onnx.ORTModelForSequenceClassification.from_pretrained(
model_name,
export=True,
cache_dir=models_storage,
device_map=device
)
# 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, device_map=device)
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, device_map=device)
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, device_map=device)
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, device_map=device)
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(text: str) -> int:
"""Return text length."""
return len(text)
def average_word_length(text: str) -> float:
"""Return average word length."""
words = _split_to_words_with_cache(text)
return np.mean([len(word) for word in words]) if words else 0
def percentage_special_characters(text: str) -> float:
"""Return percentage of special characters (as float between 0 and 1)."""
return len([c for c in text if c in string.punctuation]) / len(text) if len(text) != 0 else 0
def max_word_length(text: str) -> int:
"""Return max word length."""
words = _split_to_words_with_cache(text)
return max(len(w) for w in words) if words else 0
def _get_fasttext_model(models_storage: Union[pathlib.Path, str, None] = None):
"""Return fasttext model."""
fasttext = _import_optional_property_dependency(module='fasttext', property_name='language')
model_name = FASTTEXT_LANG_MODEL.rsplit('/', maxsplit=1)[-1]
model_path = get_create_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, timeout=240)
if response.status_code != 200:
raise RuntimeError('Failed to donwload fasttext model')
model_path.write_bytes(response.content)
# This weird code is to suppress a warning from fasttext about a deprecated function
try:
fasttext.FastText.eprint = lambda *args, **kwargs: None
fasttext_model = fasttext.load_model(str(model_path))
except Exception as exp:
raise exp
return fasttext_model
def language(
text: str,
lang_certainty_threshold: float = 0.8,
fasttext_model: Optional[Dict[object, Any]] = None
) -> Union[str, None]:
"""Return text language, represented as a string."""
if not text:
return None
# Load the model if it wasn't received as a parameter. This is done to avoid loading the model
# each time the function is called.
if fasttext_model is None:
fasttext_model = _get_fasttext_model()
# Predictions are the first prediction (k=1), only if the probability is above the threshold
prediction = fasttext_model.predict(text.replace('\n', ' '), k=1, threshold=lang_certainty_threshold)[0]
# label is empty for detection below threshold:
language_code = prediction[0].replace('__label__', '') if prediction else None
return language_code
def is_english(
text: str,
lang_certainty_threshold: float = 0.8,
fasttext_model: Optional[Dict[object, Any]] = None,
language_property_result: Optional[str] = None
) -> Union[bool, None]:
"""Return whether text is in English or not."""
if not text:
return None
if language_property_result is None:
language_property_result = language(text, lang_certainty_threshold, fasttext_model)
return language_property_result == 'en'
def sentiment(text: str) -> float:
"""Return float representing sentiment."""
hash_key = hash_text(text)
if textblob_cache.get(hash_key) is None:
# TextBlob uses only the words and not the relations between them, so we can sample the text
# to speed up the process:
words = _sample_for_property(text, mode='words')
textblob_cache[hash_key] = textblob.TextBlob(words).sentiment
return textblob_cache.get(hash_key).polarity
def subjectivity(text: str) -> float:
"""Return float representing subjectivity."""
hash_key = hash_text(text)
if textblob_cache.get(hash_key) is None:
# TextBlob uses only the words and not the relations between them, so we can sample the text
# to speed up the process:
words = _sample_for_property(text, mode='words')
textblob_cache[hash_key] = textblob.TextBlob(words).sentiment
return textblob_cache.get(hash_key).subjectivity
def predict_on_batch(text_batch: Sequence[str], classifier,
output_formatter: Callable[[Dict[str, Any]], float]) -> Sequence[float]:
"""Return prediction of huggingface Pipeline classifier."""
# TODO: make this way smarter, and not just a hack. Count tokens, for a start. Then not just sample sentences.
# If text is longer than classifier context window, sample it:
text_list_to_predict = []
reduced_batch_size = len(text_batch) # Initialize the reduced batch size
retry_count = 0
for text in text_batch:
if len(text) > MAX_CHARS:
sentences = _sample_for_property(text, mode='sentences', limit=10, return_as_list=True)
text_to_use = ''
for sentence in sentences:
if len(text_to_use) + len(sentence) > MAX_CHARS:
break
text_to_use += sentence + '. '
# if even one sentence is too long, use part of the first one:
if len(text_to_use) == 0:
text_to_use = cut_string(sentences[0], MAX_CHARS)
text_list_to_predict.append(text_to_use)
else:
text_list_to_predict.append(text)
while reduced_batch_size >= 1:
try:
if reduced_batch_size == 1 or retry_count == 3:
results = []
for text in text_list_to_predict:
try:
v = classifier(text)[0]
results.append(output_formatter(v))
except Exception: # pylint: disable=broad-except
results.append(np.nan)
return results # Return the results if prediction is successful
v_list = classifier(text_list_to_predict, batch_size=reduced_batch_size)
results = []
for v in v_list:
results.append(output_formatter(v))
return results # Return the results if prediction is successful
except Exception: # pylint: disable=broad-except
reduced_batch_size = max(reduced_batch_size // 2, 1) # Reduce the batch size by half
text_list_to_predict = [] # Clear the list of texts to predict for retry
retry_count += 1
return [np.nan] * len(text_batch) # Prediction failed, return NaN values for the original batch size
TOXICITY_MODEL_NAME = 'unitary/toxic-bert'
FLUENCY_MODEL_NAME = 'prithivida/parrot_fluency_model'
FORMALITY_MODEL_NAME = 's-nlp/roberta-base-formality-ranker'
def toxicity(
text_batch: Sequence[str],
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None,
toxicity_classifier: Optional[object] = None
) -> Sequence[float]:
"""Return float representing toxicity."""
if toxicity_classifier is None:
toxicity_classifier = get_transformer_pipeline(
property_name='toxicity', model_name=TOXICITY_MODEL_NAME, device=device, models_storage=models_storage)
def output_formatter(v):
return v['score']
return predict_on_batch(text_batch, toxicity_classifier, output_formatter)
def fluency(
text_batch: Sequence[str],
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None,
fluency_classifier: Optional[object] = None
) -> Sequence[float]:
"""Return float representing fluency."""
if fluency_classifier is None:
fluency_classifier = get_transformer_pipeline(
property_name='fluency', model_name=FLUENCY_MODEL_NAME, device=device, models_storage=models_storage)
def output_formatter(v):
return v['score'] if v['label'] == 'LABEL_1' else 1 - v['score']
return predict_on_batch(text_batch, fluency_classifier, output_formatter)
def formality(
text_batch: Sequence[str],
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None,
formality_classifier: Optional[object] = None
) -> Sequence[float]:
"""Return float representing formality."""
if formality_classifier is None:
formality_classifier = get_transformer_pipeline(
property_name='formality', model_name=FORMALITY_MODEL_NAME, device=device, models_storage=models_storage)
def output_formatter(v):
return v['score'] if v['label'] == 'formal' else 1 - v['score']
return predict_on_batch(text_batch, formality_classifier, output_formatter)
def lexical_density(text: str) -> float:
"""Return a float representing lexical density.
Lexical density is the percentage of unique words in a given text. For more
information: https://en.wikipedia.org/wiki/Lexical_density
"""
if pd.isna(text):
return np.nan
if not nltk_download('punkt', quiet=True):
_warn_if_missing_nltk_dependencies('punkt', 'Lexical Density')
return np.nan
all_words = _split_to_words_with_cache(text)
if len(all_words) == 0:
return np.nan
total_unique_words = len(set(all_words))
return round(total_unique_words * 100 / len(all_words), 2)
def unique_noun_count(text: Sequence[str]) -> int:
"""Return the number of unique noun words in the text."""
if pd.isna(text):
return np.nan
if not nltk_download('averaged_perceptron_tagger', quiet=True):
_warn_if_missing_nltk_dependencies('averaged_perceptron_tagger', 'Unique Noun Count')
return np.nan
unique_words_with_tags = set(textblob.TextBlob(text).tags)
return sum(1 for (_, tag) in unique_words_with_tags if tag.startswith('N'))
def readability_score(text: str, cmudict_dict: dict = None) -> float:
"""Return a float representing the 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 pd.isna(text):
return np.nan
if cmudict_dict is None:
if not nltk_download('cmudict', quiet=True):
_warn_if_missing_nltk_dependencies('cmudict', 'Readability Score')
return np.nan
cmudict_dict = corpus.cmudict.dict()
text_sentences = _sample_for_property(text, mode='sentences', limit=DEFAULT_SENTENCE_SAMPLE_SIZE,
return_as_list=True)
sentence_count = len(text_sentences)
words = _split_to_words_with_cache(text)
word_count = len(words)
syllable_count = sum([len(cmudict_dict[word]) for word in words if word 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)
return round(flesch_reading_ease, 3)
else:
return np.nan
def average_words_per_sentence(text: str) -> float:
"""Return the average words per sentence in the text."""
if pd.isna(text):
return np.nan
text_sentences = _sample_for_property(text, mode='sentences', limit=DEFAULT_SENTENCE_SAMPLE_SIZE,
return_as_list=True)
if text_sentences:
text_sentences = [remove_punctuation(sent) for sent in text_sentences]
total_words = sum([len(_split_to_words_with_cache(sentence)) for sentence in text_sentences])
return round(total_words / len(text_sentences), 3)
else:
return np.nan
def unique_urls_count(text: str) -> int:
"""Return the number of unique URLS in the text."""
if pd.isna(text):
return np.nan
url_pattern = r'https?:\/\/(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
return len(set(re.findall(url_pattern, text)))
def urls_count(text: str) -> int:
"""Return the number of URLS in the text."""
if pd.isna(text):
return np.nan
url_pattern = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
return len(re.findall(url_pattern, text))
def unique_email_addresses_count(text: str) -> int:
"""Return the number of unique email addresses in the text."""
if pd.isna(text):
return np.nan
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'
return len(set(re.findall(email_pattern, text)))
def email_addresses_count(text: str) -> int:
"""Return the number of email addresses in the text."""
if pd.isna(text):
return np.nan
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b'
return len(re.findall(email_pattern, text))
def unique_syllables_count(text: str, cmudict_dict: dict = None) -> int:
"""Return the number of unique syllables in the text."""
if pd.isna(text):
return np.nan
if not nltk_download('punkt', quiet=True):
_warn_if_missing_nltk_dependencies('punkt', 'Unique Syllables Count')
return np.nan
if cmudict_dict is None:
if not nltk_download('cmudict', quiet=True):
_warn_if_missing_nltk_dependencies('cmudict', 'Unique Syllables Count')
return np.nan
cmudict_dict = corpus.cmudict.dict()
text = remove_punctuation(text.lower())
words = word_tokenize(text)
syllables = {word: True for word in words if word in cmudict_dict}
return len(syllables)
def reading_time(text: str) -> int:
"""Return an integer representing time in seconds to read the text.
The formula is based on Demberg & Keller, 2008 where it is assumed that
reading a character taken 14.69 milliseconds on average.
"""
if pd.isna(text):
return np.nan
ms_per_char = 14.69
words = text.split()
nchars = map(len, words)
rt_per_word = map(lambda nchar: nchar * ms_per_char, nchars)
ms_reading_time = sum(list(rt_per_word))
return round(ms_reading_time / 1000, 2)
def sentences_count(text: str) -> int:
"""Return the number of sentences in the text."""
if pd.isna(text):
return np.nan
if not nltk_download('punkt', quiet=True):
_warn_if_missing_nltk_dependencies('punkt', 'Sentences Count')
return np.nan
return len(_split_to_sentences_with_cache(text))
def average_syllable_length(text: str, cmudict_dict: dict = None) -> float:
"""Return a the average number of syllables per sentences per text sample."""
if pd.isna(text):
return np.nan
if not nltk_download('punkt', quiet=True):
_warn_if_missing_nltk_dependencies('punkt', 'Average Syllable Length')
return np.nan
if cmudict_dict is None:
if not nltk_download('cmudict', quiet=True):
_warn_if_missing_nltk_dependencies('cmudict', 'Average Syllable Length')
return np.nan
cmudict_dict = corpus.cmudict.dict()
sentence_count = len(_split_to_sentences_with_cache(text))
text = remove_punctuation(text.lower())
words = word_tokenize(text)
syllable_count = sum([len(cmudict_dict[word]) for word in words if word in cmudict_dict])
return round(syllable_count / sentence_count, 2)
def _batch_wrapper(text_batch: Sequence[str], func: Callable, **kwargs) -> List[Any]:
"""Wrap the non-batched properties execution with batches API."""
results = []
language_property_result = []
if 'language_property_result' in kwargs:
language_property_result = kwargs.pop('language_property_result')
language_property_exists = len(language_property_result) > 0
for i, text in enumerate(text_batch):
kwargs['language_property_result'] = language_property_result[i] if language_property_exists else None
results.append(run_available_kwargs(func, text=text, **kwargs))
return results
class TextProperty(TypedDict):
name: str
method: Callable[..., Sequence[Any]]
output_type: str
DEFAULT_PROPERTIES: Tuple[TextProperty, ...] = \
(
{'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': 'Average Words Per Sentence', 'method': average_words_per_sentence, 'output_type': 'numeric'},
{'name': 'Readability Score', 'method': readability_score, 'output_type': 'numeric'},
{'name': 'Lexical Density', 'method': lexical_density, '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': 'Unique Noun Count', 'method': unique_noun_count, 'output_type': 'numeric'},
)
ALL_PROPERTIES: Tuple[TextProperty, ...] = \
(
{'name': 'Is English', 'method': is_english, 'output_type': 'categorical'},
{'name': 'URLs Count', 'method': urls_count, 'output_type': 'numeric'},
{'name': 'Email Addresses Count', 'method': email_addresses_count, 'output_type': 'numeric'},
{'name': 'Unique URLs Count', 'method': unique_urls_count, 'output_type': 'numeric'},
{'name': 'Unique Email Addresses Count', 'method': unique_email_addresses_count, 'output_type': 'numeric'},
{'name': 'Unique Syllables Count', 'method': unique_syllables_count, 'output_type': 'numeric'},
{'name': 'Reading Time', 'method': reading_time, 'output_type': 'numeric'},
{'name': 'Sentences Count', 'method': sentences_count, 'output_type': 'numeric'},
{'name': 'Average Syllable Length', 'method': average_syllable_length, 'output_type': 'numeric'},
) + DEFAULT_PROPERTIES
LONG_RUN_PROPERTIES = ('Toxicity', 'Fluency', 'Formality', 'Unique Noun Count')
BATCH_PROPERTIES = ('Toxicity', 'Fluency', 'Formality')
LARGE_SAMPLE_SIZE = 10_000
ENGLISH_ONLY_PROPERTIES = (
'Sentiment', 'Subjectivity', 'Toxicity', 'Fluency', 'Formality', 'Readability Score',
'Unique Noun Count', 'Unique Syllables Count', 'Sentences Count', 'Average Syllable Length'
)
CMUDICT_PROPERTIES = ('Average Syllable Length', 'Unique Syllables Count', 'Readability Score')
TEXT_PROPERTIES_DESCRIPTION = {
'Text Length': 'Number of characters in the text',
'Average Word Length': 'Average number of characters in a word',
'Max Word Length': 'Maximum number of characters in a word',
'% Special Characters': 'Percentage of special characters in the text',
'Language': 'Language of the text, using the fasttext language detection model',
'Sentiment': 'Sentiment of the text, calculated using the TextBlob sentiment analysis model.'
' Ranging from -1 (negative) to 1 (positive)',
'Subjectivity': 'Subjectivity of the text, calculated using the TextBlob sentiment analysis model. Ranging from 0 '
'(objective) to 1 (subjective)',
'Average Words Per Sentence': 'Average number of words per sentence in the text',
'Readability Score': 'How easy to read a text sample is, from 0 (hard to read) to 100 (easy).'
' Based on Flesch reading-ease score',
'Lexical Density': 'Percentage of unique words in the text',
'Toxicity': 'A measure of how harmful or offensive a text sample is (0 to 1), uses the Detoxify library '
'unitary/toxic-bert model',
'Fluency': 'A measure of the fluency of the text (0 to 1), using the prithivida/parrot_fluency_model'
' model from the authors of the Parrot Paraphraser library',
'Formality': 'The formality / register of the text (0 to 1), using the s-nlp/roberta-base-formality-ranker'
' model by the Skolkovo Institute of Science and Technology',
'Unique Noun Count': 'Number of unique noun words in the text',
'URLs Count': 'Number of URLS per text sample',
'Email Addresses Count': 'Number of email addresses per text sample',
'Unique URLs Count': 'Number of unique URLS per text sample',
'Is English': 'Whether the text is in English (1) or not (0)',
'Unique Email Addresses Count': 'Number of unique email addresses per text sample',
'Unique Syllables Count': 'Number of unique syllables per text sample',
'Reading Time': 'Time taken in seconds to read a text sample',
'Sentences Count': 'Number of sentences per text sample',
'Average Syllable Length': 'Average number of syllables per sentence per text sample',
}
def _select_properties(
*,
n_of_samples: int,
include_properties: Optional[List[str]] = None,
ignore_properties: Optional[List[str]] = None,
include_long_calculation_properties: bool = False,
device: Optional[str] = None,
) -> Sequence[TextProperty]:
"""Select properties."""
all_properties = ALL_PROPERTIES
default_properties = DEFAULT_PROPERTIES
if include_properties is not None and ignore_properties is not None:
raise ValueError('Cannot use properties and ignore_properties parameters together.')
if include_properties is not None:
if not is_sequence_not_str(include_properties) \
and not all(isinstance(prop, str) for prop in include_properties):
raise DeepchecksValueError('include_properties must be a sequence of strings.')
if ignore_properties is not None:
if not is_sequence_not_str(ignore_properties) \
and not all(isinstance(prop, str) for prop in ignore_properties):
raise DeepchecksValueError('ignore_properties must be a sequence of strings.')
include_properties = [prop.lower() for prop in include_properties] if include_properties else None
ignore_properties = [prop.lower() for prop in ignore_properties] if ignore_properties else None
if include_properties is not None:
properties = [prop for prop in all_properties if prop['name'].lower() in include_properties]
if len(properties) < len(include_properties):
not_found_properties = sorted(set(include_properties) - set(prop['name'].lower() for prop in properties))
raise DeepchecksValueError('include_properties contains properties that were not found: '
f'{not_found_properties}.')
elif ignore_properties is not None:
properties = [prop for prop in default_properties if prop['name'].lower() not in ignore_properties]
if len(properties) + len(ignore_properties) != len(default_properties):
not_found_properties = \
[prop for prop in ignore_properties if prop not in [prop['name'] for prop in default_properties]]
raise DeepchecksValueError('ignore_properties contains properties that were not found: '
f'{not_found_properties}.')
else:
properties = default_properties
# include_long_calculation_properties is only applicable when include_properties is None
if not include_long_calculation_properties and include_properties is None:
return [
prop for prop in properties
if prop['name'] not in LONG_RUN_PROPERTIES
]
heavy_properties = [
prop for prop in properties
if prop['name'] in LONG_RUN_PROPERTIES
]
if heavy_properties and n_of_samples > 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. '
'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)
return properties
def calculate_builtin_properties(
raw_text: Sequence[str],
include_properties: Optional[List[str]] = None,
ignore_properties: Optional[List[str]] = None,
include_long_calculation_properties: bool = False,
ignore_non_english_samples_for_english_properties: bool = True,
device: Optional[str] = None,
models_storage: Union[pathlib.Path, str, None] = None,
batch_size: Optional[int] = 16
) -> 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 Words Per Sentence', 'URLs Count', Unique URLs Count', 'Email Address Count',
'Unique Email Address Count', 'Unique Syllables Count', 'Reading Time', 'Sentences Count',
'Average Syllable Length']
List of default 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 Words Per Sentence']
To calculate all the default properties, the include_properties and ignore_properties parameters should
be None. If you pass either include_properties or ignore_properties then only the properties specified
in the list will be calculated or ignored.
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 from the list of default properties. If None, no properties will be ignored and
all the default properties will be calculated. Cannot be used together with include_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, unless they are specified in the include_properties parameter explicitly.
ignore_non_english_samples_for_english_properties : bool, default True
Whether to ignore samples that are not in English when calculating English properties. If False, samples
that are not in English will be calculated as well. This parameter is ignored when calculating non-English
properties.
English-Only properties WILL NOT work properly on non-English samples, and this parameter should be used
only when you are sure that all the samples are in English.
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.
batch_size : int, default 8
The batch size.
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.
"""
text_properties = _select_properties(
include_properties=include_properties,
ignore_properties=ignore_properties,
device=device,
include_long_calculation_properties=include_long_calculation_properties,
n_of_samples=len(raw_text)
)
properties_types = {
it['name']: it['output_type']
for it in text_properties
}
kwargs = dict(device=device, models_storage=models_storage)
english_properties_names = set(ENGLISH_ONLY_PROPERTIES)
text_properties_names = [it['name'] for it in text_properties]
calculated_properties = {k: [] for k in text_properties_names}
# Prepare kwargs for properties that require outside resources:
if 'fasttext_model' not in kwargs:
kwargs['fasttext_model'] = _get_fasttext_model(models_storage=models_storage)
if 'cmudict_dict' not in kwargs:
properties_requiring_cmudict = list(set(CMUDICT_PROPERTIES) & set(text_properties_names))
if properties_requiring_cmudict:
if not nltk_download('cmudict', quiet=True):
_warn_if_missing_nltk_dependencies('cmudict', format_list(properties_requiring_cmudict))
for prop in properties_requiring_cmudict:
calculated_properties[prop] = [np.nan] * len(raw_text)
cmudict_dict = corpus.cmudict.dict()
kwargs['cmudict_dict'] = cmudict_dict
if 'Toxicity' in text_properties_names and 'toxicity_classifier' not in kwargs:
kwargs['toxicity_classifier'] = get_transformer_pipeline(
property_name='toxicity', model_name=TOXICITY_MODEL_NAME, device=device, models_storage=models_storage)
if 'Formality' in text_properties_names and 'formality_classifier' not in kwargs:
kwargs['formality_classifier'] = get_transformer_pipeline(
property_name='formality', model_name=FORMALITY_MODEL_NAME, device=device, models_storage=models_storage)
if 'Fluency' in text_properties_names and 'fluency_classifier' not in kwargs:
kwargs['fluency_classifier'] = get_transformer_pipeline(
property_name='fluency', model_name=FLUENCY_MODEL_NAME, device=device, models_storage=models_storage)
is_language_property_requested = 'Language' in [prop['name'] for prop in text_properties]
# Remove language property from the list of properties to calculate as it will be calculated separately:
if is_language_property_requested:
text_properties = [prop for prop in text_properties if prop['name'] != 'Language']
warning_message = (
'Failed to calculate property {0}. '
'Dependencies required by property are not installed. '
'Error:\n{1}'
)
import_warnings = set()
for i in tqdm(range(0, len(raw_text), batch_size)):
batch = raw_text[i:i + batch_size]
batch_properties = defaultdict(list)
# filtering out empty sequences
nan_indices = {i for i, seq in enumerate(batch) if pd.isna(seq) is True}
filtered_sequences = [e for i, e in enumerate(batch) if i not in nan_indices]
samples_language = _batch_wrapper(text_batch=filtered_sequences, func=language, **kwargs)
if is_language_property_requested:
batch_properties['Language'].extend(samples_language)
calculated_properties['Language'].extend(samples_language)
kwargs['language_property_result'] = samples_language # Pass the language property to other properties
non_english_indices = set()
if ignore_non_english_samples_for_english_properties:
non_english_indices = {i for i, (seq, lang) in enumerate(zip(filtered_sequences, samples_language))
if lang != 'en'}
for prop in text_properties:
if prop['name'] in import_warnings: # Skip properties that failed to import:
batch_properties[prop['name']].extend([np.nan] * len(batch))
else:
if prop['name'] in english_properties_names \
and ignore_non_english_samples_for_english_properties is True:
filtered_sequences = [e for i, e in enumerate(filtered_sequences) if i not in non_english_indices]
kwargs['batch_size'] = batch_size
try:
if prop['name'] in BATCH_PROPERTIES:
value = run_available_kwargs(func=prop['method'], text_batch=filtered_sequences, **kwargs)
else:
value = _batch_wrapper(text_batch=filtered_sequences, func=prop['method'], **kwargs)
batch_properties[prop['name']].extend(value)
except ImportError as e:
warnings.warn(warning_message.format(prop['name'], str(e)))
batch_properties[prop['name']].extend([np.nan] * len(batch))
import_warnings.add(prop['name'])
result_index = 0
for index, seq in enumerate(batch):
if index in nan_indices or (index in non_english_indices and
ignore_non_english_samples_for_english_properties and
prop['name'] in english_properties_names):
calculated_properties[prop['name']].append(np.nan)
else:
calculated_properties[prop['name']].append(batch_properties[prop['name']][result_index])
result_index += 1
filtered_sequences = [e for i, e in enumerate(batch) if i not in nan_indices]
# Clear property caches:
textblob_cache.clear()
words_cache.clear()
sentences_cache.clear()
# Clean all remaining RAM:
gc.collect()
if not calculated_properties:
raise RuntimeError('Failed to calculate any of the properties.')
properties_types = {
k: v
for k, v in properties_types.items()
if k in calculated_properties
}
return calculated_properties, properties_types
def get_builtin_properties_types():