Skip to main content

The Python library containing the implementation of the authorship verification method 'LambdaG', including a kneser-ney language model implementation.

Project description

LambdaG - Grammar as a behavioral biometric: Using cognitively motivated grammar models for authorship verification

This is the official repository for the paper "Grammar as a behavioral biometric: Using cognitively motivated grammar models for authorship verification". The paper proposes an authorship verification (AV) method - called LambdaG - which seeks to answer the question of whether two given documents are written by the same author, or not. In contrast to existing AV methods which often suffer from high complexity, low explainability and especially from a lack of clear scientific justification, LambdaG represents a simpler method based on modeling the grammar of an author following Cognitive Linguistics principles.

Given two documents, 𝒟𝒜 and 𝒟𝒰 as well as some reference documents $𝔻_{\text{ref}}$, the ratio of the likelihoods of 𝒟𝒰 given 𝒜's grammar versus the grammar of a reference population’s grammar is calculated. Based on this ratio a final prediction of whether 𝒜=𝒰 holds is made.

Additionaly, this implementation also exposes two novel normalisation techniques (Square Root Correction and the Hapax Correction), proposed in the paper "Normalisation-Based Likelihood Ratio Estimation for Forensic Authorship Verification", for deriving likelihood ratios from LambdaG without the need of a training dataset.

Usage

Python implementation

To use LambdaG with Python install the LambdaG package from PyPI:

pip install lambdag

or alternatively directly from this repository:

pip install git+https://github.com/AndreaNini/LambdaG

Afterwards you can use LambdaG as follows, for example:

from sklearn.metrics import accuracy_score, roc_auc_score
from lambdag.corpus import load_corpus
from lambdag import LambdaGMethod

# instantiate method object
method = LambdaGMethod(
    basis="tokens", 
    order=10, 
    num_references=30, 
    calibration="logistic_regression"
)

# train method 
# (*not* needed when using `square_root` or `hapax` calibration)
train_problems, train_labels, train_author_texts = load_corpus("corpus/path/train")
method.fit(train_problems, train_author_texts, train_labels)

# evaluate on test corpus
test_problems, test_labels, test_author_texts = load_corpus("corpus/path/test")
test_probas = method.predict_proba(test_problems, test_author_texts)

print(f"Accuracy: {accuracy_score(test_labels, test_probas[:,1]>=0.5):.3f}")

A text heatmap with colour-coded tokens depending on the $\lambda_G$ value can be made using the lambdaG_visualize() function (see example). This feature requires the optional visualization dependencies, which can be installed with pip install lambdag[visualize].

For further examples see examples.

R implementation

An alternative implementation in R is provided in the idiolect package.

Data

This is selection of datasets used in the paper that can be shared freely. Please note that, however, for copyright and GDPR reasons these are released only after pre-processing for content-masking (using the POSnoise algorithm). If you are interested in the other datasets please email us.

Name Link to repository
Enron corpus DOI
Wiki DOI
Perverted Justice DOI
Apricity DOI
TripAdvisor DOI
Blog DOI

Please keep in mind that, for best results, LambdaG should be used on texts that have been preprocessed with POSNoise (paper, original implementation).

Citation

If you use our code in your work, please consider citing:

@article{nini2025grammarbehavioralbiometricusing,
    title={Grammar as a Behavioral Biometric: {{Using}} Cognitively Motivated Grammar Models for Authorship Verification}, 
    author={Nini, Andrea and Halvani, Oren and Graner, Lukas and Titze, Sophie and Gherardi, Valerio and Ishihara, Shunichi},
    year={2026},
    journal = {Humanities and Social Sciences Communications},
    volume = {13},
    number = {1},
    pages = {455},
    issn = {2662-9992},
    doi = {10.1057/s41599-025-06340-3},
}

and (especially when using square_root and hapax calibration methods):

@misc{barlow2026normalisationbasedlikelihoodratioestimation,
      title={Normalisation-Based Likelihood Ratio Estimation for Forensic Authorship Verification}, 
      author={Sadie Barlow and Andrea Nini and Edoardo Manino},
      year={2026},
      eprint={2607.09501},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2607.09501}, 
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambdag-0.2.0.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambdag-0.2.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file lambdag-0.2.0.tar.gz.

File metadata

  • Download URL: lambdag-0.2.0.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lambdag-0.2.0.tar.gz
Algorithm Hash digest
SHA256 326190d4ce9babce80e1c9aeddf0e506eb6aa575cd42059b1c9e15942142b7f5
MD5 88ba3f25bffb8debd24bd538211f7ec6
BLAKE2b-256 d756ab2637b267a68e20b39a3f34e12209a637abdf4be6c289f4bd8086ca3294

See more details on using hashes here.

Provenance

The following attestation bundles were made for lambdag-0.2.0.tar.gz:

Publisher: python-publish.yml on andreanini/lambdag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lambdag-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lambdag-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lambdag-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a42988ec5a30860c5f4edfc4556d12cb83710657142ab93d526bd19ad0d1465f
MD5 e9e5961b9c9d85f0b9ded6ab1fe8b319
BLAKE2b-256 6b96e9fd4ae5d5deded240b63beddea658efdacc5058e609304e1f87c457ae07

See more details on using hashes here.

Provenance

The following attestation bundles were made for lambdag-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on andreanini/lambdag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page