Skip to content

Commit

Permalink
change html generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Jul 16, 2024
1 parent de6716b commit 9534524
Show file tree
Hide file tree
Showing 14 changed files with 112,872 additions and 7,327 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
poems
-----

All of the poems in here are good, or interesting. There are currently 8,348 poems by 546 poets.
All of the poems in here are good, or interesting. There are currently 8,396 poems by 548 poets.
25 changes: 9 additions & 16 deletions poems/author.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from dataclasses import dataclass, field
from datetime import datetime

@dataclass
class Author():
"""Author dataclass"""
tag: str
name: str
birth: str
death: str
birth: dict
death: dict
gender: str
religion: str
nationality: str
Expand All @@ -27,24 +28,16 @@ def dates(self):
if not self.birth:
return ""
else:
return f"(born {self.birth})"

birth_is_circa = True if "~" in self.birth else False
death_is_circa = True if "~" in self.death else False
return f"(born {self.birth['year']})"

b_numeric = int(self.birth.strip("~"))
d_numeric = int(self.death.strip("~"))

birth_string, death_string = str(abs(b_numeric)), str(abs(d_numeric))

birth_string = f'{"c. " if birth_is_circa else ""}{abs(b_numeric)}'
death_string = f'{"c. " if death_is_circa else ""}{abs(d_numeric)}'
birth_string = f"{'c. ' if 'circa' in self.birth else ''}{abs(self.birth['year'])}"
death_string = f"{'c. ' if 'circa' in self.death else ''}{abs(self.death['year'])}"

if b_numeric < 0:
if d_numeric < 0:
if self.birth["year"] < 0:
if self.death["year"] < 0:
death_string += " BC"
else:
birth_string += " BC"
death_string += " AD"

return f"({birth_string} -- {death_string})"
return f"({birth_string} {death_string})"
31 changes: 23 additions & 8 deletions poems/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import json
import os
import pathlib
import warnings
import yaml

from pandas import DataFrame
from .context import Context
from .poem import Poem, Author
from .utils import make_author_stats

here, this_file = os.path.split(__file__)

with open(f"{here}/weights.json", "r+") as f:
CONTEXT_WEIGHTS = json.load(f)
CONTEXT_WEIGHTS = yaml.safe_load(pathlib.Path(f"{here}/data/weights.yml").read_text())

class Catalog():

Expand Down Expand Up @@ -88,7 +90,7 @@ def apply_context(self, context: dict, forced=[], verbose=False):
if keyword == context[category]:
if keyword in forced:
if verbose:
print(f"Forcing context '{keyword}'")
print(f"FORCING CONTEXT: {category}='{keyword}'")
multiplier = 1e18
else:
multiplier = weight
Expand All @@ -102,36 +104,49 @@ def apply_context(self, context: dict, forced=[], verbose=False):
self.df.loc[:, "probability"] = self.df.likelihood / self.df.likelihood.sum()


def apply_history(self, history: DataFrame, latency: int = 30 * 86400, verbose: bool = False):
def apply_history(self, history: DataFrame, cooldown: int = 30 * 86400, manage_attrition: bool = False, verbose: bool = False):

timestamp = Context.now().timestamp

author_stats = make_author_stats(history, self)

last_occurence = DataFrame(columns=["timestamp"], dtype=float)
for index, entry in history.iterrows():
last_occurence.loc[entry.author, "timestamp"] = entry.timestamp

last_occurence = last_occurence.sort_values("timestamp")

indices_to_drop = []
treated_authors = []
dropped_authors = []

for _, entry in history.iterrows():

res = self.df.loc[(self.df.author==entry.author) & (self.df.title==entry.title)]
author_mask = self.df.author == entry.author

res = self.df.loc[author_mask & (self.df.title==entry.title)]

if not len(res):
warnings.warn(f"Could not remove poem '{entry.title}' by '{entry.author}'.")

indices_to_drop.extend(res.index)

if entry.timestamp > timestamp - latency:
if entry.author not in dropped_authors:
indices_to_drop.extend(self.df.loc[self.df.author == entry.author].index)
# apply to all poems by this author, if not done for this author yet:
if entry.author not in dropped_authors:

if entry.timestamp > timestamp - cooldown:
indices_to_drop.extend(self.df.loc[author_mask].index)
dropped_authors.append(entry.author)

if manage_attrition:
if entry.author not in treated_authors:
self.df.loc[author_mask, "likelihood"] *= 1 - author_stats.loc[entry.author, "attrition"]
treated_authors.append(entry.author)

if verbose:
print(f"Dropped authors {dropped_authors}.")


self.df.loc[indices_to_drop, "likelihood"] = 0

def __repr__(self):
Expand Down
8 changes: 4 additions & 4 deletions poems/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

here, this_filename = os.path.split(__file__)

HOLIDAYS = yaml.safe_load(pathlib.Path(f"{here}/holidays.yml").read_text())
HOLIDAYS = yaml.safe_load(pathlib.Path(f"{here}/data/holidays.yml").read_text())
MONTHS = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
WEEKDAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

Expand Down Expand Up @@ -63,9 +63,9 @@ def to_dict(self):
"season": self.season,
"liturgy": self.liturgy,
"holiday": self.holiday,
"year": f"{self.year:04}",
"month": f"{self.month:02}",
"day": f"{self.day:02}",
"year": self.year,
"month": self.month,
"day": self.day,
"year_day": self.year_day,
"weekday": self.weekday,
"month_epoch": self.month_epoch}
Expand Down
8 changes: 2 additions & 6 deletions poems/curator.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import json, os
import os, pathlib, yaml
import numpy as np
from .errors import AuthorNotFoundError, PoemNotFoundError
from .catalog import Catalog
from .poem import Author, Poem

here, this_file = os.path.split(__file__)

with open(f"{here}/weights.json", "r+") as f:
CONTEXT_WEIGHTS = json.load(f)

class Curator():

def __init__(self, filepath=f"{here}/poems.json"):

def __init__(self, filepath=f"{here}/data/poems.json"):
self.catalog = Catalog(filepath=filepath)


Expand Down
8 changes: 4 additions & 4 deletions poems/holidays.yml → poems/data/holidays.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dates:
24: saint_catherine_of_genoa
25: annunciation
26: saint_margaret_clitherow
27: blessed_francis_faà_di_bruno
27: blessed_francis_faa_di_bruno
28: saint_hesychius_of_jerusalem
29: saint_berthold
30: saint_peter_regalado
Expand Down Expand Up @@ -284,7 +284,7 @@ dates:
4: saint_rose_of_viterbo
5: blessed_teresa_of_kolkata
6: saint_eleutherius
7: blessed_frédéric_ozanam
7: blessed_frederic_ozanam
8: marymas
9: saint_peter_claver
10: pedro_de_corpa_and_companions
Expand Down Expand Up @@ -312,7 +312,7 @@ dates:
october:
1: saint_therese_of_lisieux
2: guardian_angels
3: saint_theodora_guérin
3: saint_theodora_guerin
4: saint_francis_of_assisi
5: blessed_francis_xavier_seelos
6: saint_bruno
Expand All @@ -334,7 +334,7 @@ dates:
22: saint_john_paul_ii
23: saint_john_of_capistrano
24: saint_anthony_mary_claret
25: saint_antônio_de_sant’anna_galvão
25: saint_antonio_de_santanna_galvao
26: saint_bean
27: saint_frumentius
28: saints_simon_and_jude
Expand Down
Loading

0 comments on commit 9534524

Please sign in to comment.