Utility functions¶
poetry_analysis.utils
¶
convert_to_syllables(phonemes, ipa=False)
¶
Turn a sequence of phonemes into syllable groups.
Source code in src/poetry_analysis/utils.py
89 90 91 92 93 94 95 96 97 98 | |
endswith(sequence, suffix)
¶
Check if a sequence ends with a given suffix.
Source code in src/poetry_analysis/utils.py
50 51 52 53 54 55 56 57 58 59 | |
gather_stanza_annotations(func)
¶
Decorator to apply a function to each stanza in a text.
Source code in src/poetry_analysis/utils.py
243 244 245 246 247 248 249 250 251 252 253 254 | |
group_consecutive_numbers(nums)
¶
Group consecutive numbers into sublists.
Examples:
>>> list_of_numbers = [1, 2, 3, 5, 6, 8, 9, 10]
>>> result = group_consecutive_numbers(list_of_numbers)
>>> print(result)
[[1, 2, 3], [5, 6], [8, 9, 10]]
Source code in src/poetry_analysis/utils.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
is_punctuation(char)
¶
Check if a character is a punctuation mark.
Source code in src/poetry_analysis/utils.py
62 63 64 | |
is_valid_onset(phonelist)
¶
WORK IN PROGRESS Check if a sequence of characters forms a valid onset in Norwegian orthography.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phonelist
|
str
|
A string representing the onset (e.g., "bl", "tr"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the onset is valid, False otherwise. |
Source code in src/poetry_analysis/utils.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
make_comparable_string(item)
¶
Convert a list of strings into a single comparable string.
Source code in src/poetry_analysis/utils.py
81 82 83 84 85 86 | |
normalize(text)
¶
Lowercase, remove punctuation and tokenize a string of text.
Source code in src/poetry_analysis/utils.py
262 263 264 265 266 267 | |
split_orthographic_text_into_syllables(words)
¶
WORK IN PROGRESS Split orthographic text into syllables using basic rules. This is a simplified implementation and may not handle all edge cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
list of str
|
A list of orthographic words, already tokenized |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of syllables for each word in the text. |
Source code in src/poetry_analysis/utils.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
split_paragraphs(text)
¶
Split a text into paragraphs and paragraphs into lines.
Source code in src/poetry_analysis/utils.py
230 231 232 233 234 235 236 | |
split_stanzas(text)
¶
Split a poem into stanzas and stanzas into verses.
Source code in src/poetry_analysis/utils.py
257 258 259 | |
strip_punctuation(string)
¶
Remove punctuation from a string
Source code in src/poetry_analysis/utils.py
72 73 74 75 76 77 78 | |
strip_redundant_whitespace(text)
¶
Strip redundant whitespace and reduce it to a single space.
Source code in src/poetry_analysis/utils.py
67 68 69 | |
syllabify(transcription)
¶
Flatten list of syllables from a list of transcribed words.
Source code in src/poetry_analysis/utils.py
101 102 103 104 105 106 107 108 | |