A Python library for calculating a large variety of statistics from text(s) using spaCy v.3 pipeline components and extensions. TextDescriptives can be used to calculate several descriptive statistics, readability metrics, and metrics related to dependency distance.
pip install textdescriptives
stanza_versionbranch and will no longer be maintained.
Import the library and add the component to your pipeline using the string name of the "textdescriptives" component factory:
import spacy import textdescriptives as td nlp = spacy.load("en_core_web_sm") nlp.add_pipe("textdescriptives") doc = nlp("The world is changed. I feel it in the water. I feel it in the earth. I smell it in the air. Much that once was is lost, for none now live who remember it.") # access some of the values doc._.readability doc._.token_length
TextDescriptives includes convenience functions for extracting metrics to a Pandas DataFrame or a dictionary.
td.extract_df(doc) # td.extract_dict(doc)
|0||The world (...)||3.28571||3||1.54127||7||6||3.09839||1.08571||1||0.368117||35||23||0.657143||121||5||107.879||-0.0485714||5.68392||3.94286||-2.45429||-0.708571||12.7143||0.4||1.69524||0.422282||0.44381||0.0863679||0.097561||0.121951||0.0487805||0.0487805||0.121951||0.170732||0.121951||0.121951||0.0731707||0.0243902||0.0243902||0.0243902|
Set which group(s) of metrics you want to extract using the
metrics parameter (one or more of
pos_stats, defaults to
extract_df is called on an object created using
nlp.pipe it will format the output with 1 row for each document and a column for each metric. Similarly,
extract_dict will have a key for each metric and values as a list of metrics (1 per doc).
docs = nlp.pipe(['The world is changed. I feel it in the water. I feel it in the earth. I smell it in the air. Much that once was is lost, for none now live who remember it.', 'He felt that his whole life was some kind of dream and he sometimes wondered whose it was and whether they were enjoying it.']) td.extract_df(docs, metrics="dependency_distance")
|0||The world (...)||1.69524||0.422282||0.44381||0.0863679|
|1||He felt (...)||2.56||0||0.44||0|
text column can by exluded by setting
The specific components (
pos_stats) can be loaded individually. This can be helpful if you're only interested in e.g. readability metrics or descriptive statistics and don't want to run the dependency parser or part-of-speech tagger.
nlp = spacy.blank("da") nlp.add_pipe("descriptive_stats") docs = nlp.pipe(['Da jeg var atten, tog jeg patent på ild. Det skulle senere vise sig at blive en meget indbringende forretning', "Spis skovsneglen, Mulle. Du vil jo gerne være med i hulen, ikk'?"]) # extract_df is clever enough to only extract metrics that are in the Doc td.extract_df(docs, include_text = False)
The table below shows the metrics included in TextDescriptives and their attributes on spaCy's
Token objects. For more information, see the docs.
|Dict containing mean, median, and std of token length.|
|Dict containing mean, median, and std of sentence length.|
|Dict containing mean, median, and std of number of syllables per token.|
|Dict containing the number of tokens, number of unique tokens, proportion unique tokens, and number of characters in the Doc.|
|Dict of |
|Dict containing Flesch Reading Ease, Flesch-Kincaid Grade, SMOG, Gunning-Fog, Automated Readability Index, Coleman-Liau Index, LIX, and RIX readability metrics for the Doc.|
|Dict containing the mean and standard deviation of the dependency distance and proportion adjacent dependency relations in the Doc.|
|Dict containing mean, median, and std of token length in the span.|
|Dict containing the number of tokens, number of unique tokens, proportion unique tokens, and number of characters in the span.|
|Dict of |
|Dict containing the mean dependency distance and proportion adjacent dependency relations in the Doc.|
|Dict containing the dependency distance and whether the head word is adjacent for a Token.|