Language Tools

Resiliparse language tools API documentation.

Fast character n-gram language detection for 101 languages and profile training.

Use detect_fast() to detect languages, supported_langs() to list their codes, and train_language_examples() to train a separate vector. Normalize input text to NFC for best results. Ranks measure profile distance; lower values indicate closer matches, and values above 1200 are usually unreliable. Training does not change the built-in profiles.

resiliparse.parse.lang.detect_fast(text, cutoff=1200, n_results=1, langs=None)

Perform very fast (linear-time) language detection on the input string.

The output is a tuple of the detected language code and its calculated out-of-place rank, indicating the distance from the closest language profile. Higher ranks mean less accurate detection; values above 1200 are usually false results. Ranks are integer distances, not confidence probabilities.

The given Unicode string should be in composed normal form (NFC) for best results. No normalization or case folding is performed automatically. Longer texts generally produce more reliable predictions.

For texts shorter than 150 Unicode code points, raw ranks above 500 receive a bias of up to 50 toward languages with more Wikipedia users. The length includes ignored characters. The rank is the sum of absolute profile-bin differences plus this bias.

With n_results=1, no match returns ("unknown", 0). Other values return a list of up to n_results matches in ascending rank order; zero returns an empty list. The cutoff is inclusive. langs=None considers all 101 profiles; an empty iterable considers none. Unsupported language codes are ignored, and filter order does not affect ranking. For one result, equal ranks favor the first profile in model order (descending Wikipedia user counts). Multiple results use deterministic heap order for ties. Empty or entirely ignored text can still match a profile.

Parameters:
  • text – Input text

  • cutoff – Inclusive OOP rank cutoff, default 1200

  • n_results – Nonnegative number of best matches to return, default 1

  • langs – Restrict detection to these language codes, or None for all

Returns:

Detected language and rank, or a list of matches

Raises:
  • OverflowError – If cutoff or n_results is negative or exceeds the platform’s unsigned integer range

  • TypeError – If text is not a str or langs cannot be used as a language filter

resiliparse.parse.lang.supported_langs()

Get all languages supported by the fast language detector.

Returns a fresh list of 101 language codes sorted lexicographically. These codes can be passed to detect_fast() through its langs parameter.

Returns:

Sorted list of supported languages

resiliparse.parse.lang.train_language_examples(examples, vec_len=256)

Train a language vector for fast detection on an iterable of example texts.

Each text is converted to a normalized character n-gram vector, and the resulting vectors are averaged with equal weight, truncating fractions. Each example bin is its n-gram count multiplied by 256, divided by the number of retained letters and collapsed spaces, truncated and capped at 255. Empty strings contribute zero vectors and count toward the average.

Letters retain their case; consecutive whitespace collapses to an ASCII space. Other characters are ignored without breaking n-grams, but interrupt whitespace collapsing. Normalize to NFC before training for best results.

The returned list has exactly vec_len elements. Use 256 bins for the built-in detector’s dimensions. This function does not modify the built-in profiles.

Parameters:
  • examples – Example texts for this language

  • vec_len – Positive output vector length, default 256

Returns:

Vector of trained values

Raises:
  • ValueError – If examples is empty, vec_len is zero, or the 32-bit example counter wraps to zero

  • OverflowError – If vec_len is negative or exceeds the platform’s unsigned integer range

  • TypeError – If examples is not iterable or contains a non-string value