File size: 2,247 Bytes
c6e3750 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import syllable_analysis as sylco
import meter_analysis as metco
import re, unidecode
def prep_encoding(text):
"""
Cleans text by removing whitespace, replacing nonstandard characters with
their standard counterparts, etc
Parameters
----------
text : str
any text
Returns
-------
str
cleaned text
"""
text = unidecode.unidecode(text).strip()
if not re.search(r"\S", text):
text = ""
return text
def encode_line_meter_count(line, to_stdout=False):
"""
Encodes a song line (line of text) into a line of digit words representing
the stress of each word in the line.
Ex:
what so proudly we hailed at the twilight's last gleaming
-> 1 1 10 1 1 1 0 12 1 10
Parameters
----------
line : str
string of words (line)
to_stdout : bool, optional
whether to print to stdout, by default False
Returns
-------
str
string of stress encodings (digits)
"""
line = prep_encoding(line)
if line == "":
if to_stdout:
print(line)
return ""
line_stress_list = metco.get_line_meter(line)
out = " ".join(line_stress_list)
if to_stdout:
print(out)
return out
def encode_line_syllable_count(line, to_stdout=False):
"""
Encodes a song line (line of text) into a line of digits representing
the number of syllables per line.
Ex:
the quick brown fox jumps over the lazy dog
-> 1 1 1 1 1 1 1 2 1
Parameters
----------
line : str
string of words (line)
to_stdout : bool, optional
whether to print to stdout, by default False
Returns
-------
string
string of digits, one digit per word
"""
line = prep_encoding(line)
if line == "":
if to_stdout:
print(line)
return line
words = re.findall(r"\b\w+\b", line)
syllable_counts = [sylco.count_syllables(word) for word in words]
out = " ".join(map(str, syllable_counts))
if to_stdout:
if len(syllable_counts) > 0:
out += " " * (30 - len(out))
out += f": {sum(syllable_counts)}"
print(out)
return out
|