Skip to content

Commit

Permalink
add english metaphors
Browse files Browse the repository at this point in the history
  • Loading branch information
fschncvg committed Nov 6, 2024
1 parent ee50079 commit dba9aa1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ install_requires =
wget
fasttext
[options.entry_points]
console_scripts =
freestylo = freestylo.freestylo_main:main
[options.packages.find]
where = src
exclude =
Expand Down
48 changes: 47 additions & 1 deletion src/freestylo/freestylo_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,35 @@ def main():
parser.add_argument("--config", help="Configuration file")
args = parser.parse_args()

print("Loading text from", args.input)
print("Loading configuration from", args.config)
print("Saving results to", args.output)

print("Loading config...")
# load config
with open(args.config) as f:
config = json.load(f)
print("Done")

max_length = None
if "nlp_max_length" in config:
max_length = config["nlp_max_length"]



# Load text

print("Loading text...")
text = to.TextObject(
textfile = args.input,
language=config["language"])
print("Done")

# Preprocess text
preprocessor = tp.TextPreprocessor(language=config["language"])
print("Preprocessing text...")
preprocessor = tp.TextPreprocessor(language=config["language"], max_length=max_length)
preprocessor.process_text(text)
print("Done")
# Annotate
annotation_dict = config["annotations"]
for annotation in annotation_dict:
Expand All @@ -60,63 +76,93 @@ def main():
add_metaphor_annotation(text, annotation_dict[annotation])
elif annotation == "epiphora":
add_epiphora_annotation(text, annotation_dict[annotation])
elif annotation == "polysyndeton":
add_polysyndeton_annotation(text, annotation_dict[annotation])
elif annotation == "alliteration":
add_alliteration_annotation(text, annotation_dict[annotation])
text.serialize(args.output)
print("Added all annotations")

# Serialize results
print("Serializing results")
text.serialize(args.output)
print("Done")


print("Finished")

def add_chiasmus_annotation(text, config):
"""
This function adds chiasmus annotations to the text.
"""
print("Adding chiasmus annotation")
chiasmus = ca.ChiasmusAnnotation(
text=text,
window_size = config["window_size"])
chiasmus.allowlist = config["allowlist"]
chiasmus.denylist = config["denylist"]
print("Finding candidates")
chiasmus.find_candidates()
print("Loading model")
chiasmus.load_classification_model(config["model"])
print("Scoring candidates")
chiasmus.score_candidates()
print("Done")

def add_metaphor_annotation(text, config):
"""
This function adds metaphor annotations to the text.
"""
print("Adding metaphor annotation")
metaphor = ma.MetaphorAnnotation(text)
print("Finding candidates")
metaphor.find_candidates()
print("Loading model")
metaphor.load_model(config["model"])
print("Scoring candidates")
metaphor.score_candidates()
print("Done")

def add_epiphora_annotation(text, config):
"""
This function adds epiphora annotations to the text.
"""
print("Adding epiphora annotation")
epiphora = ea.EpiphoraAnnotation(
text = text,
min_length = config["min_length"],
conj = config["conj"],
punct_pos = config["punct_pos"])
print("Finding candidates")
epiphora.find_candidates()
print("Done")

def add_polysyndeton_annotation(text, config):
"""
This function adds polysyndeton annotations to the text.
"""
print("Adding polysyndeton annotation")
polysyndeton = pa.PolysyndetonAnnotation(
text = text,
min_length = config["min_length"],
conj = config["conj"],
sentence_end_tokens = config["sentence_end_tokens"])
print("Finding candidates")
polysyndeton.find_candidates()
print("Done")

def add_alliteration_annotation(text, config):
"""
This function adds alliteration annotations to the text.
"""
print("Adding alliteration annotation")
alliteration = aa.AlliterationAnnotation(
text = text,
max_skip = config["max_skip"],
min_length = config["min_length"])
print("Finding candidates")
alliteration.find_candidates()
print("Done")

if __name__ == '__main__':
main()

0 comments on commit dba9aa1

Please sign in to comment.