A tool for stylistic device detection."
Project description
FreeStylo - an easy-to-use stylistic device detection tool for stylometry
An easy-to-use package for detecting stylistic devices in text. This package is designed to be used in stylometry, the study of linguistic style.
For those proficient in python, this package provides a collection of approaches to detect stylistic devices in text. For those less proficient in python, this package provides a simple interface to detect stylistic devices in text with simple commands and user-friendly configuration.
Installation
This package needs python 3.12 to run. It is recommended to create a virtual environment for the package. The package is available on PyPi and can be installed using pip.
pip install freestylo
Usage Examples
Standalone Tool
If after installation, run the following command in the root of the repository:
freestylo --input test/documents/chiasmustext.txt --output ./output.json --config example_config.json
This creates the file output.json in the root of the repository, which contains the detected stylistic devices in the text file test/documents/chiasmustext.txt.
Afterwards, run the following command to get an overview over the results:
freestylo --mode report --data output.json --device chiasmustext
The report mode is currently just implemented for Chiasmus.
The package can be used both as a library and as a stand-alone command-line tool. Both from the library and from the command-line tool, the results can be saved in a JSON file. This json file will contain the complete tokenized text. When using the functions from the library, the result will be a python container with a similar structure to the JSON file.
The standalone version can be configured using a simple JSON configuration file. The file should specify the language of the text and the stylistic devices to detect. The following is an example configuration file:
{
"language": "de",
"annotations": {
"chiasmus": {
"window_size": 30,
"allowlist": ["NOUN", "VERB", "ADJ", "ADV"],
"denylist": [],
"model": "/chiasmus_de.pkl"
}
}
}
Library
The library comprises a collection of functions to detect the stylistic devices, as well as preprocessing based on spaCy. Should you want to use different preprocessing or use the package with a different language than the supported ones, a TextObject can be created and filled with the needed manually computed contents. The stylistic device detectors can then be applied to the TextObject.
The tests folder contains a test for every stylistic device detector.
These tests show how to use the different detectors and how to create a TextObject.
All classes and functions are documented by docstrings.
A typical example code would look like this:
from freestylo import TextObject as to
from freestylo import TextPreprocessor as tp
from freestylo import ChiasmusAnnotation as ca
from freestylo import MetaphorAnnotation as ma
# first, create a TextObject from the raw text
text = to.TextObject(
textfile = "example_textfile.txt",
language="en")
# create a TextPreprocessor object and process the text
# this does the tokenizing, lemmatizing, POS-tagging, etc.
preprocessor = tp.TextPreprocessor(language="en")
preprocessor.process_text(text)
# you can also use a different preprocessing of your choice
# without the TextPreprocessor object
# just fill the TextObject with the needed contents
# those could be provided e.g. by spaCy, nltk, cltk,
# or any other method of your choice
# many digital corpora are already tokenized and POS-tagged
# they may come in various formats, such as TEI XML, CoNLL, etc.
# if you have a text in those formats, you can fill the TextObject
# with the needed contents
# you can then fill the missing values in the TextObject
# with e.g. word vectors or other features created with a method of your choice.
# you can now add various annotations to the text object
# here, we add a chiasmus annotation
chiasmus = ca.ChiasmusAnnotation(
text=text)
chiasmus.allowlist = ["NOUN", "VERB", "ADJ", "ADV"]
chiasmus.find_candidates()
chiasmus.load_classification_model("chiasmus_model.pkl")
chiasmus.score_candidates()
# here, we add a metaphor annotation
metaphor = ma.MetaphorAnnotation(
text=text)
metaphor.find_candidates()
metaphor.load_model("metaphor_model.pkl")
metaphor.score_candidates()
# finally, save the annotated text to a json file
text.serialize("annotated_text.json")
The file test/test_external_source.py shows an an example of using the library without the text preprocessor. Instead the TextObject is filled by hand with the needed contents.
Currently supported stylistic devices are:
- Alliteration
- Chiasmus
- Epiphora
- Metaphor
- Polysyndeton
Create your own detectors!
The package is designed to be easily extendable with your own stylistic device detectors.
The src folder contains example scripts that show how you can retrain the models for the existing chiasmus and metaphor detectors.
You can also create your own stylistic device detectors by referring to the existing ones.
Especially the Alliteration Detector provides a very simple example that can be used as a template for your own detectors.
If you create and want to contribute your own detecors, pull requests are very welcome!
Participation
The package is free and open-source software and contributions are very welcome. It is designed to be a living project that is constantly improved and extended. If you have implemented your own stylistic device detector, please consider contributing it to the package. For details please refer to the contribution guidelines. Also, if you have any suggestions for improvements or if you find any bugs, please open an issue on the GitHub page.
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 freestylo-0.8.2.tar.gz.
File metadata
- Download URL: freestylo-0.8.2.tar.gz
- Upload date:
- Size: 60.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08aee545aa00422790c7e279a9a86dd935d10a5e62e48b9825e3887ec03ebdd2
|
|
| MD5 |
d51823632b2b31fc4c335fdbaf6c4118
|
|
| BLAKE2b-256 |
a0685426ede3365443685b72a3d67b311eb32c2e1b7087e2375631cc5ed5346d
|
Provenance
The following attestation bundles were made for freestylo-0.8.2.tar.gz:
Publisher:
package-and-publish.yml on cvjena/freestylo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freestylo-0.8.2.tar.gz -
Subject digest:
08aee545aa00422790c7e279a9a86dd935d10a5e62e48b9825e3887ec03ebdd2 - Sigstore transparency entry: 208611549
- Sigstore integration time:
-
Permalink:
cvjena/freestylo@73f7ef920892e963608d6b93f8a38d15a94641b9 -
Branch / Tag:
refs/tags/0.8.2 - Owner: https://github.com/cvjena
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package-and-publish.yml@73f7ef920892e963608d6b93f8a38d15a94641b9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file freestylo-0.8.2-py3-none-any.whl.
File metadata
- Download URL: freestylo-0.8.2-py3-none-any.whl
- Upload date:
- Size: 35.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
541e767ccf777bc020f7af4fb2b409e27517339e4dd08333025267f6d21a5aa1
|
|
| MD5 |
a61298aba4337b3da80c92ccc874d226
|
|
| BLAKE2b-256 |
7af0335fa8ad03c03e5bbc89d678af0ec90af380258165faad03286456cf6895
|
Provenance
The following attestation bundles were made for freestylo-0.8.2-py3-none-any.whl:
Publisher:
package-and-publish.yml on cvjena/freestylo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freestylo-0.8.2-py3-none-any.whl -
Subject digest:
541e767ccf777bc020f7af4fb2b409e27517339e4dd08333025267f6d21a5aa1 - Sigstore transparency entry: 208611551
- Sigstore integration time:
-
Permalink:
cvjena/freestylo@73f7ef920892e963608d6b93f8a38d15a94641b9 -
Branch / Tag:
refs/tags/0.8.2 - Owner: https://github.com/cvjena
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package-and-publish.yml@73f7ef920892e963608d6b93f8a38d15a94641b9 -
Trigger Event:
push
-
Statement type: