Skip to content

API reference

text_complexity_computer.TextComplexityComputer

Assess the complexity of a French text.

Parameters:

Name Type Description Default
scaler ScalerName | None

Scaler applied to the metrics before classification: "MinMaxScaler" (default), "StandardScaler" or None to use raw metrics.

'MinMaxScaler'
verbosity int

Verbosity level when reading the Biber word lists (0 is silent).

1
Example

tcc = TextComplexityComputer() tcc.compute("Alibaba et les 40 voleurs.") array([1])

get_metrics_scores

get_metrics_scores(
    text: str,
    metrics: Iterable[str] | str | None = 'all',
    with_biberpy: bool = True,
) -> pd.DataFrame

Compute the metrics scores of a text.

Available metrics (outside biberpy):

  • mls: mean length of sentences
  • ps_30: percentage of sentences longer than 30 words
  • nws_90: length of the 90th percentile sentence
  • mlt: mean length of T-units
  • tu_s: T-units per sentence
  • ctu_tu: complex T-units per T-unit
  • dc_c: dependent clauses per clause
  • c_s: clauses per sentence
  • c_tu: clauses per T-unit
  • cp_c: coordinate phrases per clause
  • cp_tu: coordinate phrases per T-unit
  • pa: percentage of words not in a reference list (Gougenheim list)
  • nlm: number of letters per word
  • uni_gram_lem: uni-gram model with Lexique3 on lemmas
  • msttr: mean sequential type-token ratio
  • mattr: alias of mtld kept for compatibility with the bundled model
  • mtld: measure of textual lexical diversity
  • fk_ease: Flesch-Kincaid reading ease
  • bingui: punctuation marks per sentence
  • km_score: Kandel and Moles formula

The biberpy dimensions are the values of biberpy.dimnames.

Parameters:

Name Type Description Default
text str

Text to compute.

required
metrics Iterable[str] | str | None

Metrics to compute (outside biberpy): "all" (default), a metric name, a list of metric names or None.

'all'
with_biberpy bool

Whether to compute the biberpy dimensions.

True

Returns:

Type Description
DataFrame

A one-row DataFrame of the selected metrics, columns sorted by name. Values are scaled when a scaler

DataFrame

is set; scaling uses every metric, so unselected ones count as 0.

Raises:

Type Description
ValueError

If a metric name is unknown.

get_sp_object

get_sp_object(text: str) -> Doc

Return the spaCy document of the cleaned text.

compute

compute(text: str) -> np.ndarray

Estimate the difficulty level of a text.

Parameters:

Name Type Description Default
text str

Text to compute.

required

Returns:

Type Description
ndarray

A one-element array with the estimated difficulty level.

text_complexity_computer.models

Numpy inference for the bundled scaler and random forest.

The estimators were trained with scikit-learn 1.3.2 and exported to .npz arrays by scripts/export_models.py. Inference reproduces scikit-learn's arithmetic, so predictions do not depend on the installed scikit-learn version.

RandomForestClassifier

Random forest inference equivalent to scikit-learn's RandomForestClassifier.predict.

All the trees are stored as one flat node array; roots holds the index of each tree root.

predict_proba

predict_proba(features: DataFrame) -> np.ndarray

Return the class probabilities, averaged over the trees.

predict

predict(features: DataFrame) -> np.ndarray

Return the most probable class of each row of features.

Scaler

Min-max or standard scaler, equivalent to scikit-learn's MinMaxScaler and StandardScaler.

transform

transform(features: DataFrame) -> np.ndarray

Scale the features, matched by column name.

Metric functions

text_complexity_computer.calculation_functions.syntactic_complexity

mean_length_sentences

mean_length_sentences(sp_object: Doc) -> float

Mean Length of Sentences (MLS)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the mean length of sentences

nws_90

nws_90(sp_object: Doc) -> int

Length of the 90th percentile sentence (NWS_90)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
int int

length of the 90th percentile sentence

ps_30

ps_30(sp_object: Doc) -> float

Percentage of sentences longer than 30 words (PS_30)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the percentage of sentences longer than 30 words

check_if_complex

check_if_complex(sp_root_token: Token) -> bool

Return whether a subordinate clause depends, directly or not, on the token.

mean_length_tunit

mean_length_tunit(sp_object: Doc) -> float

Mean Length of T-Units (MLT)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average size of T-Units

tu_s

tu_s(sp_object: Doc) -> float

T-unit per Sentence (TU_S)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of T-Units per sentence

ctu_tu

ctu_tu(sp_object: Doc) -> float

Complex T-units per T-unit (CTU_TU)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Complex T-Units per T-Unit

dc_c

dc_c(sp_object: Doc) -> float

Dependent Clause per Clause (DC_C)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Dependent Clauses per Clause

c_s

c_s(sp_object: Doc) -> float

Clauses per Sentences (C_S)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Clauses per phrase

c_tu

c_tu(sp_object: Doc) -> float

Clauses per T-Unit (C_TU)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Clauses per T-Unit

cp_c

cp_c(sp_object: Doc) -> float

Coordinate phrase per Clause (CP_C)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Coordinate phrases per Clause

cp_tu

cp_tu(sp_object: Doc) -> float

Coordinate phrase per T-Unit (CP_TU)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of Coordinate phrases per T-Unit

text_complexity_computer.calculation_functions.vocabulary_complexity

pa

pa(sp_object: Doc, language: str) -> float

Percentage of words not in a reference list (PA) of easy words using the Gougenheim list (Gougenheim et al., 1964).

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required
language str

The language to use for the easy word reference list.

required

Returns:

Name Type Description
float float

The average number of word not in a reference list.

nlm

nlm(sp_object: Doc) -> float

Average size of words (NLM)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

average size of words

nmm_8

nmm_8(sp_object: Doc) -> float

Average number of words with more than 8 letters (NMM_8)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

Average number of words with more than 8 letters

uni_gram_lem

uni_gram_lem(sp_object: Doc, language: str) -> float

Uni-gram Model on Lemma (Uni_gram_Lem) (using a uni-gram model) using frequencies Lexique383 (New B. et al., 2005).

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required
language str

The language to use for the frequency list.

required

Returns:

Name Type Description
float float

the average probability value of words according to a frequency list.

text_complexity_computer.calculation_functions.lexical_diversity

get_filtered_words

get_filtered_words(
    sp_object: Doc, without_stop: bool = False
) -> list[Token]

Get a list of all filtered words. What is defined here as filtered is without punctuations and without stopwords.

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required
without_stop bool

does not take into account stopwords. (Default to False)

False

Returns:

Type Description
list[Token]

list[spacy.tokens.token.Token]: the list of filtered words as spaCy token.

type_token_ratio

type_token_ratio(sp_object: Iterable[Token]) -> float

Type-Token Ratio (TTR) (Templin et al., 1957)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the TTR

mean_sequential_ttr

mean_sequential_ttr(
    sp_object: Doc, segment_size: int = 50
) -> float

Mean Sequential TTR (MSTTR) (Johnson et al., 1944)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required
segment_size int

the size of the segment for the sequential mean

50

Returns:

Name Type Description
float float

the MSTTR

moving_average_ttr

moving_average_ttr(
    sp_object: Doc, window_size: int = 100
) -> float

Moving Average TTR (MATTR) (Covington and McFall, 2008)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required
window_size int

the size of the window for the moving average

100

Returns:

Name Type Description
float float

the MATTR

measure_textual_lexical_diversity_unidir

measure_textual_lexical_diversity_unidir(
    sp_object: Sequence[Token],
) -> float

One-directional MTLD pass over a sequence of tokens.

measure_textual_lexical_diversity

measure_textual_lexical_diversity(sp_object: Doc) -> float

Measure of Textual Lexical Diversity (MTLD) (P.M. McCarthy and S. Jarvis, 2010)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the MTLD

text_complexity_computer.calculation_functions.readability

bingui

bingui(sp_object: Doc) -> float

BINGUI (bingui)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the average number of punctuations [,:;().!?] per sentence

fk_ease

fk_ease(sp_object: Doc) -> float

Flesch-Kincaid Reading Ease score.

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Type Description
float

Flesch-Kincaid Reading Ease score

Original author: Nicolas Garneau

lm_100

lm_100(sp_object: Doc) -> float

Average number of syllable per 100 words (lm_100)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the number of syllable per 100 words

km_formula

km_formula(sp_object: Doc) -> float

Kandel and Moles formula (KM)

Parameters:

Name Type Description Default
sp_object Doc

spaCy object based on the text that will be computed.

required

Returns:

Name Type Description
float float

the result of Kales and Moles formula

text_complexity_computer.calculation_functions.metrics_utils

get_word_texts

get_word_texts(
    sp_object: Iterable[Token],
    min_size: int = 0,
    without_stop: bool = False,
) -> list[str]

Return the lowercased real words (not punctuation nor words with an apostrophe).

Parameters:

Name Type Description Default
sp_object Iterable[Token]

spaCy document, span or list of tokens of the text.

required
min_size int

Minimum number of letters of a word to keep it.

0
without_stop bool

Whether to drop the stopwords.

False

Original author: Nicolas Garneau

get_num_words

get_num_words(
    sp_object: Iterable[Token],
    min_size: int = 0,
    without_stop: bool = False,
) -> int

Return the number of real words (not punctuation nor words with an apostrophe).

Parameters:

Name Type Description Default
sp_object Iterable[Token]

spaCy document, span or list of tokens of the text.

required
min_size int

Minimum number of letters of a word to count it.

0
without_stop bool

Whether to ignore the stopwords.

False

Original author: Nicolas Garneau

get_num_words_and_letters

get_num_words_and_letters(
    sp_object: Iterable[Token],
) -> tuple[int, int]

Return the number of real words and their total number of letters.

get_num_words_and_unique_words

get_num_words_and_unique_words(
    sp_object: Iterable[Token], without_stop: bool = False
) -> tuple[int, int]

Return the number of real words and of unique real words.

get_num_sentences

get_num_sentences(sp_object: Doc) -> int

Return the number of sentences, ignoring the segments ending with a comma.

Original author: Nicolas Garneau

syllables_estimate

syllables_estimate(string: str) -> int

Estimate the number of syllables as the number of vowel groups.

get_num_syllables

get_num_syllables(sp_object: Doc | Token) -> int

Return the number of syllables of a document or a single token.

Punctuation and words with an apostrophe are ignored.

Original author: Nicolas Garneau

safe_divide

safe_divide(
    x: float, y: float, returned_value: float = 0.0
) -> float

Return x / y, or returned_value when y is 0.

text_complexity_computer.tools

clean_text

clean_text(text: str) -> str

Clean a text before computing its metrics.

Changes made:

  • remove the bullet list markers and some special characters
  • add a space after points
  • collapse repeated whitespaces

Parameters:

Name Type Description Default
text str

Text to clean.

required

Returns:

Type Description
str

The cleaned text.