Skip to content

Lyrical subject

poetry_analysis.lyrical_subject

add_metadata(poem, lyric_features)

Add metadata from the poem to the annotations.

Source code in src/poetry_analysis/lyrical_subject.py
72
73
74
75
76
77
def add_metadata(poem, lyric_features):
    """Add metadata from the poem to the annotations."""
    lyric_features["ID"] = int(poem.get("ID"))
    lyric_features["URN"] = poem.get("URN")
    lyric_features["Tittel på dikt"] = poem.get("Tittel på dikt")
    return lyric_features

detect_lyrical_subject(poem_text)

Map the presence of certain words denoting a lyrical subject in a poem to categorical labels.

Source code in src/poetry_analysis/lyrical_subject.py
52
53
54
55
56
57
58
59
60
def detect_lyrical_subject(poem_text: str) -> dict:
    """Map the presence of certain words denoting a lyrical subject in a poem to categorical labels."""
    lyrical_subject = {}
    for label, words in WORDBAGS.items():
        regx_pattern = "|".join(words)
        matches = re.findall(regx_pattern, poem_text.lower())
        is_present = bool(any(matches))
        lyrical_subject[label] = is_present
    return lyrical_subject

process_poems(poems, text_field='textV3')

Annotate whether or not the lyrical subject is a feature in a list of poems.

Source code in src/poetry_analysis/lyrical_subject.py
63
64
65
66
67
68
69
def process_poems(poems, text_field="textV3"):
    """Annotate whether or not the lyrical subject is a feature in a list of poems."""

    for poem in poems:
        poem_text = poem.get(text_field)
        lyric_features = detect_lyrical_subject(poem_text)
        yield add_metadata(poem, lyric_features)