Skip to content

Commit

Permalink
prepare for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
fschncvg committed Oct 23, 2024
1 parent 8025e3b commit 13b2e0f
Show file tree
Hide file tree
Showing 27 changed files with 97 additions and 135 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# STYLOTOOL - an easy-to-use stylistic device detection tool for stylometry
# 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.

Expand All @@ -7,7 +7,9 @@ For an example on usage, see `example_chiasmus.sh` and the config file `example_

# Installation
The package is available on PyPi and can be installed using pip.
TODO: add installation instructions
```
piop install freestylo
```

# Participation
The package is free and open-source software and contributions are very welcome.
Expand Down
4 changes: 4 additions & 0 deletions build_and_upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

python3 -m build
python3 -m twine upload dist/*
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "stylotool"
name = "freestylo"
version = "0.0.1"
authors = [
{ name = "Felix Schneider", email = "[email protected]"},
Expand All @@ -14,8 +14,8 @@ classifiers = [
]

[project.urls]
Homepage = "https://github.com/cvjena/stylotool"
Issues = "https://github.com/cvjena/stylotool/issues"
Homepage = "https://github.com/cvjena/freestylo"
Issues = "https://github.com/cvjena/freestylo/issues"

[build-system]
requires = [
Expand All @@ -26,7 +26,8 @@ requires = [
"pytest",
"scikit-learn",
"scipy",
"cltk<1.0"
"cltk<1.0",
"wget"

]
build-backend = "setuptools.build_meta"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from stylotool.TextObject import TextObject
from freestylo.TextObject import TextObject

"""
This class is used to find alliterations candidates in a text.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from stylotool.TextObject import TextObject
from freestylo.TextObject import TextObject
from freestylo.Configs import get_model_path
import numpy as np

"""
Expand Down Expand Up @@ -45,7 +46,7 @@ def find_candidates(self):

def load_classification_model(self, model_path):
import pickle
with open(model_path, "rb") as f:
with open(get_model_path(model_path), "rb") as f:
self.model = pickle.load(f)

def serialize(self) -> list:
Expand Down
52 changes: 52 additions & 0 deletions src/freestylo/Configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import logging
import json
import wget

model_list = [
"chiasmus_de.pkl",
"metaphor_de.torch",
]

github_model_base = "https://github.com/cvjena/freestylo/raw/refs/heads/main/models/"

def get_model_path(model_to_load : str) -> str:
if os.path.exists(model_to_load):
return model_to_load

user_path = os.path.expanduser("~")
config_path = os.path.join(user_path, ".config/freestylo/")
config_file = os.path.join(config_path, "config.json")
if not os.path.exists(config_file):

os.makedirs(config_path, exist_ok=True)
with open(config_file, "w") as f:
json.dump(
{"model_path": os.path.join(user_path, ".freestylo/models/")},
f,
indent=4)

with open(config_file, "r") as f:
config = json.load(f)

model_path = config["model_path"]

if not os.path.exists(model_path):
os.makedirs(model_path, exist_ok=True)

for model in model_list:
if not os.path.exists(os.path.join(model_path, model)):
logging.info(f"Downloading model {model} from {github_model_base}")
wget.download(github_model_base+model, model_path)
logging.info("done")


model_to_load = os.path.join(model_path, model_to_load)
if not os.path.exists(model_to_load):
raise FileNotFoundError(f"Model {model_to_load} not found")
return model_to_load





Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from stylotool.TextObject import TextObject
from freestylo.TextObject import TextObject

"""
this class is used to find polysyndeton candidates in a text.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import torch
import stylotool.SimilarityNN as SimilarityNN
from stylotool.TextObject import TextObject
import freestylo.SimilarityNN as SimilarityNN
from freestylo.TextObject import TextObject
from freestylo.Configs import get_model_path


# TODO: automatically select cuda device
Expand Down Expand Up @@ -33,6 +34,7 @@ def serialize(self) -> list:


def load_model(self, model_path):
model_path = get_model_path(model_path)
self.model = SimilarityNN.SimilarityNN(300, 128, 1, 128, self.device)
self.model.load_state_dict(torch.load(model_path, weights_only=True))
self.model = self.model.to(self.device)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from stylotool.TextObject import TextObject
from freestylo.TextObject import TextObject

"""
this class is used to find polysyndeton candidates in a text.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import spacy
from stylotool.TextObject import TextObject
from stylotool.MGHPreprocessor import MGHPreprocessor
from freestylo.TextObject import TextObject
from freestylo.MGHPreprocessor import MGHPreprocessor

"""
This class is used to preprocess text.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 0 additions & 30 deletions src/stylotool/Configs.py

This file was deleted.

28 changes: 0 additions & 28 deletions src/stylotool/stylotool.egg-info/PKG-INFO

This file was deleted.

23 changes: 0 additions & 23 deletions src/stylotool/stylotool.egg-info/SOURCES.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/stylotool/stylotool.egg-info/dependency_links.txt

This file was deleted.

18 changes: 0 additions & 18 deletions src/stylotool/stylotool.egg-info/top_level.txt

This file was deleted.

6 changes: 3 additions & 3 deletions test/test_alliteration_annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import stylotool.TextObject as to
import stylotool.TextPreprocessor as tp
import stylotool.AlliterationAnnotation as aa
import freestylo.TextObject as to
import freestylo.TextPreprocessor as tp
import freestylo.AlliterationAnnotation as aa


def test_alliteration_annotation():
Expand Down
8 changes: 4 additions & 4 deletions test/test_chiasmus_annotation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stylotool.ChiasmusAnnotation as ca
import stylotool.TextObject as to
import stylotool.TextPreprocessor as tp
import freestylo.ChiasmusAnnotation as ca
import freestylo.TextObject as to
import freestylo.TextPreprocessor as tp
import numpy as np
import os

Expand All @@ -18,7 +18,7 @@ def test_chiasmus_annotation():
text=text)
chiasmus.allowlist = ["NOUN", "VERB", "ADJ", "ADV"]
chiasmus.find_candidates()
chiasmus.load_classification_model(os.path.expanduser("~/.stylotool/models/chiasmus_de.pkl"))
chiasmus.load_classification_model(os.path.expanduser("chiasmus_de.pkl"))
chiasmus.score_candidates()

scores = [c.score for c in chiasmus.candidates]
Expand Down
6 changes: 3 additions & 3 deletions test/test_epiphora_annotation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stylotool.TextObject as to
import stylotool.EpiphoraAnnotation as ea
import stylotool.TextPreprocessor as tp
import freestylo.TextObject as to
import freestylo.EpiphoraAnnotation as ea
import freestylo.TextPreprocessor as tp
import numpy as np


Expand Down
8 changes: 4 additions & 4 deletions test/test_metaphor_annotations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stylotool.TextObject as to
import stylotool.MetaphorAnnotation as ma
import stylotool.TextPreprocessor as tp
import freestylo.TextObject as to
import freestylo.MetaphorAnnotation as ma
import freestylo.TextPreprocessor as tp
import numpy as np
import os

Expand All @@ -16,7 +16,7 @@ def test_metaphor_annotation():
metaphor = ma.MetaphorAnnotation(
text=text)
metaphor.find_candidates()
metaphor.load_model(os.path.expanduser("~/.stylotool/models/metaphor_de.torch"))
metaphor.load_model(os.path.expanduser("~/.freestylo/models/metaphor_de.torch"))
metaphor.score_candidates()
results = metaphor.candidates

Expand Down
6 changes: 3 additions & 3 deletions test/test_polysyndeton_annotation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stylotool.TextObject as to
import stylotool.PolysyndetonAnnotation as pa
import stylotool.TextPreprocessor as tp
import freestylo.TextObject as to
import freestylo.PolysyndetonAnnotation as pa
import freestylo.TextPreprocessor as tp
import numpy as np


Expand Down
6 changes: 3 additions & 3 deletions test/test_text_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from stylotool.TextObject import TextObject
from stylotool.TextPreprocessor import TextPreprocessor
from stylotool.MGHPreprocessor import MGHPreprocessor
from freestylo.TextObject import TextObject
from freestylo.TextPreprocessor import TextPreprocessor
from freestylo.MGHPreprocessor import MGHPreprocessor

import os
import json
Expand Down

0 comments on commit 13b2e0f

Please sign in to comment.