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 | |
| Wiki | |
| Perverted Justice | |
| Apricity | |
| TripAdvisor | |
| Blog |
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
326190d4ce9babce80e1c9aeddf0e506eb6aa575cd42059b1c9e15942142b7f5
|
|
| MD5 |
88ba3f25bffb8debd24bd538211f7ec6
|
|
| BLAKE2b-256 |
d756ab2637b267a68e20b39a3f34e12209a637abdf4be6c289f4bd8086ca3294
|
Provenance
The following attestation bundles were made for lambdag-0.2.0.tar.gz:
Publisher:
python-publish.yml on andreanini/lambdag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lambdag-0.2.0.tar.gz -
Subject digest:
326190d4ce9babce80e1c9aeddf0e506eb6aa575cd42059b1c9e15942142b7f5 - Sigstore transparency entry: 2187389164
- Sigstore integration time:
-
Permalink:
andreanini/lambdag@8efcc63a34542b01a45dda6edcdacb59a12b043c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/andreanini
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8efcc63a34542b01a45dda6edcdacb59a12b043c -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a42988ec5a30860c5f4edfc4556d12cb83710657142ab93d526bd19ad0d1465f
|
|
| MD5 |
e9e5961b9c9d85f0b9ded6ab1fe8b319
|
|
| BLAKE2b-256 |
6b96e9fd4ae5d5deded240b63beddea658efdacc5058e609304e1f87c457ae07
|
Provenance
The following attestation bundles were made for lambdag-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on andreanini/lambdag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lambdag-0.2.0-py3-none-any.whl -
Subject digest:
a42988ec5a30860c5f4edfc4556d12cb83710657142ab93d526bd19ad0d1465f - Sigstore transparency entry: 2187389178
- Sigstore integration time:
-
Permalink:
andreanini/lambdag@8efcc63a34542b01a45dda6edcdacb59a12b043c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/andreanini
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@8efcc63a34542b01a45dda6edcdacb59a12b043c -
Trigger Event:
release
-
Statement type: