Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rhyme scheme annotation as HTML #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ This repo is designed to be easily integratable with other projects, with functi

```
cd deep_rhyme_detection/
python rhyme.py [language] [input_file] [output_dir]
python rhyme.py [language] [input_file] [output_dir] [format]
```

where the arguments are:

* `language`, the language of the text you want to analyze. This can only currently take the value `english` (one day there will hopefully be support for other languages).
* `input_file`, the path to the file containing the text to analyze. This should be a simple text file where lines in a stanza are separated by line breaks and stanzas are separated by a blank line. Punctuation and the like is fine to include.
* `output_dir`, the path to the directory where the analyzed text will be dumped to a new text file.
* `format`, whether the rhyme scheme annotated output should be `txt` or `html`.



Expand Down
68 changes: 57 additions & 11 deletions deep_rhyme_detection/rhyme.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ def scheme_to_text(self, rhyme_blocks=None, stanza_num=None):
#print(delimited_string)
return delimited_string

def scheme_to_html(self, rhyme_blocks=None, stanza_num=None):
if stanza_num:
print('Calculating rhyme scheme for stanza {}...'.format(stanza_num+1))


colors = ['#4CA999', '#1B0661', '#39DD2B', '#A87DCF', '#736F2D',
'#697D54', '#AAC315', '#A8030C', '#C89FCC', '#49DADA',
'#D94E8C', '#4366AF', '#770D85', '#BDFED0', '#B6426F']
if not rhyme_blocks:
rhyme_blocks = self.get_rhyming_blocks()
rhyme_block_words = [item[0] for item in rhyme_blocks]

# Back-map to formatted (with punctuation and line breaks) original text
formatted_blocks = self.scheme_to_orig(rhyme_blocks)

delimited_html = []
for block in formatted_blocks:
color = colors[block[1]]
delimited = f'<span style="background-color:{color}; color: white">{block[0]} </span>'
# Push <br/> after at the end of each line
if '\n' in delimited:
delimited = delimited.replace('\n', '') + '<br/>'
delimited_html.append(delimited)
html = '\n'.join(delimited_html)
html += '<br/>'
return html

class Poem:
'''
Poems have multiple stanzas. This class gives functions for handling text with many
Expand Down Expand Up @@ -214,11 +241,15 @@ def get_rhyme_scheme_text(self):
# self.get_rhyme_blocks()
self.rhyme_scheme = [stanza.scheme_to_text(stanza_num=i) for i, stanza in enumerate(self.stanzas)]

def get_rhyme_scheme_html(self):
self.rhyme_scheme_html = [stanza.scheme_to_html(stanza_num=i) for i, stanza in enumerate(self.stanzas)]

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('language', help='Can be english.')
parser.add_argument('input_file', help='Path to the input file.')
parser.add_argument('output_path', help='Path to the output directory.')
parser.add_argument('format', help='Whether the output should be .txt or .html')
args = parser.parse_args()

if args.language == 'english':
Expand All @@ -241,18 +272,33 @@ def get_rhyme_scheme_text(self):
# Get the rhyme scheme
print('Getting rhyme scheme.')
poem = Poem(text_lines, corpus, model)
poem.get_rhyme_scheme_text()
for stanza in poem.rhyme_scheme:
print(stanza)
#rhyme_scheme = Stanza(text_lines, corpus, model)
#rhyme_scheme.scheme_to_text()

# Save to output file
input_filename = os.path.basename(args.input_file)
input_split = input_filename.split('.')
output_filename = input_split[0] + '_r.' + input_split[1]
output_file = os.path.join(args.output_path, output_filename)
with open(output_file, 'w') as fp:
if args.format == 'txt':
poem.get_rhyme_scheme_text()
for stanza in poem.rhyme_scheme:
fp.write(stanza)
fp.write('\n')
print(stanza)
input_filename = os.path.basename(args.input_file)
input_split = input_filename.split('.')
output_filename = input_split[0] + '_r.' + input_split[1]
output_file = os.path.join(args.output_path, output_filename)
with open(output_file, 'w') as fp:
for stanza in poem.rhyme_scheme:
fp.write(stanza)
fp.write('\n')


if args.format == 'html':
# Generate rhyme scheme in HTML
poem.get_rhyme_scheme_html()
for stanza in poem.rhyme_scheme_html:
print(stanza)
input_filename = os.path.basename(args.input_file)
input_split = input_filename.split('.')
output_html_filename = input_split[0] + '_r.html'
output_file = os.path.join(args.output_path, output_html_filename)
with open(output_file, 'w') as fp:
for stanza in poem.rhyme_scheme_html:
fp.write(stanza)
fp.write('\n')
40 changes: 40 additions & 0 deletions test_files/deck_thyself_r.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<span style="background-color:#1B0661; color: white">Deck thyself, </span>
<span style="background-color:#A87DCF; color: white">my </span>
<span style="background-color:#AAC315; color: white">soul, </span>
<span style="background-color:#1B0661; color: white">with </span>
<span style="background-color:#39DD2B; color: white">gladness, </span><br/>
<span style="background-color:#39DD2B; color: white">leave </span>
<span style="background-color:#1B0661; color: white">the </span>
<span style="background-color:#A87DCF; color: white">gloomy </span>
<span style="background-color:#39DD2B; color: white">haunts </span>
<span style="background-color:#1B0661; color: white">of </span>
<span style="background-color:#39DD2B; color: white">sadness; </span><br/>
<span style="background-color:#39DD2B; color: white">come into </span>
<span style="background-color:#1B0661; color: white">the </span>
<span style="background-color:#39DD2B; color: white">daylight's </span>
<span style="background-color:#736F2D; color: white">splendour, </span><br/>
<span style="background-color:#697D54; color: white">there </span>
<span style="background-color:#1B0661; color: white">with </span>
<span style="background-color:#A87DCF; color: white">joy thy </span>
<span style="background-color:#39DD2B; color: white">praises </span>
<span style="background-color:#736F2D; color: white">render </span><br/>
<span style="background-color:#1B0661; color: white">unto </span>
<span style="background-color:#A87DCF; color: white">him </span>
<span style="background-color:#1B0661; color: white">whose </span>
<span style="background-color:#39DD2B; color: white">grace unbounded </span><br/>
<span style="background-color:#1B0661; color: white">hath </span>
<span style="background-color:#39DD2B; color: white">this wondrous banquet founded: </span><br/>
<span style="background-color:#A87DCF; color: white">high </span>
<span style="background-color:#736F2D; color: white">o'er </span>
<span style="background-color:#AAC315; color: white">all </span>
<span style="background-color:#1B0661; color: white">the </span>
<span style="background-color:#A87DCF; color: white">heavens </span>
<span style="background-color:#1B0661; color: white">he </span>
<span style="background-color:#39DD2B; color: white">reigneth, </span><br/>
<span style="background-color:#39DD2B; color: white">yet </span>
<span style="background-color:#1B0661; color: white">to </span>
<span style="background-color:#AAC315; color: white">dwell </span>
<span style="background-color:#1B0661; color: white">with </span>
<span style="background-color:#A87DCF; color: white">thee </span>
<span style="background-color:#1B0661; color: white">he </span>
<span style="background-color:#39DD2B; color: white">deigneth. </span>
106 changes: 106 additions & 0 deletions test_files/love_unknown_r.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<span style="background-color:#736F2D; color: white">My </span>
<span style="background-color:#4CA999; color: white">song </span>
<span style="background-color:#39DD2B; color: white">is </span>
<span style="background-color:#1B0661; color: white">love </span>
<span style="background-color:#A87DCF; color: white">unknown, </span><br/>
<span style="background-color:#736F2D; color: white">my </span>
<span style="background-color:#A8030C; color: white">Savior's </span>
<span style="background-color:#1B0661; color: white">love to me; </span><br/>
<span style="background-color:#1B0661; color: white">love to the loveless </span>
<span style="background-color:#A87DCF; color: white">shown, </span><br/>
<span style="background-color:#AAC315; color: white">that </span>
<span style="background-color:#A87DCF; color: white">they </span>
<span style="background-color:#697D54; color: white">might </span>
<span style="background-color:#A87DCF; color: white">lovely </span>
<span style="background-color:#1B0661; color: white">be. </span><br/>
<span style="background-color:#1B0661; color: white">O who </span>
<span style="background-color:#697D54; color: white">am I, </span>
<span style="background-color:#AAC315; color: white">that </span>
<span style="background-color:#4CA999; color: white">for </span>
<span style="background-color:#736F2D; color: white">my </span>
<span style="background-color:#AAC315; color: white">sake, </span><br/>
<span style="background-color:#736F2D; color: white">my </span>
<span style="background-color:#4CA999; color: white">Lord </span>
<span style="background-color:#1B0661; color: white">should </span>
<span style="background-color:#AAC315; color: white">take </span>
<span style="background-color:#A8030C; color: white">frail flesh </span>
<span style="background-color:#736F2D; color: white">and die? </span><br/><br/>
<span style="background-color:#39DD2B; color: white">He </span>
<span style="background-color:#1B0661; color: white">came from His </span>
<span style="background-color:#A87DCF; color: white">blest </span>
<span style="background-color:#736F2D; color: white">throne </span><br/>
<span style="background-color:#4CA999; color: white">salvation </span>
<span style="background-color:#39DD2B; color: white">to </span>
<span style="background-color:#39DD2B; color: white">bestow; </span><br/>
<span style="background-color:#A87DCF; color: white">but </span>
<span style="background-color:#736F2D; color: white">man </span>
<span style="background-color:#1B0661; color: white">made </span>
<span style="background-color:#736F2D; color: white">strange, and none </span><br/>
<span style="background-color:#A87DCF; color: white">the </span>
<span style="background-color:#4CA999; color: white">longed-for </span>
<span style="background-color:#A87DCF; color: white">Christ would know. </span><br/>
<span style="background-color:#A87DCF; color: white">But oh, </span>
<span style="background-color:#736F2D; color: white">my Friend, my Friend </span>
<span style="background-color:#A87DCF; color: white">indeed, </span><br/>
<span style="background-color:#A87DCF; color: white">who at </span>
<span style="background-color:#736F2D; color: white">my </span>
<span style="background-color:#A87DCF; color: white">need </span>
<span style="background-color:#1B0661; color: white">his life did </span>
<span style="background-color:#736F2D; color: white">spend! </span><br/><br/>
<span style="background-color:#1B0661; color: white">Sometimes </span>
<span style="background-color:#39DD2B; color: white">they </span>
<span style="background-color:#736F2D; color: white">strew </span>
<span style="background-color:#1B0661; color: white">His </span>
<span style="background-color:#39DD2B; color: white">way </span><br/>
<span style="background-color:#39DD2B; color: white">and </span>
<span style="background-color:#1B0661; color: white">His sweet praises </span>
<span style="background-color:#A87DCF; color: white">sing; </span><br/>
<span style="background-color:#A87DCF; color: white">resounding </span>
<span style="background-color:#4CA999; color: white">all </span>
<span style="background-color:#697D54; color: white">the </span>
<span style="background-color:#39DD2B; color: white">day </span><br/>
<span style="background-color:#1B0661; color: white">hosannas </span>
<span style="background-color:#697D54; color: white">to </span>
<span style="background-color:#AAC315; color: white">their </span>
<span style="background-color:#A87DCF; color: white">King. Then </span><br/>
<span style="background-color:#39DD2B; color: white">"Crucify!" </span>
<span style="background-color:#697D54; color: white">is </span>
<span style="background-color:#4CA999; color: white">all </span>
<span style="background-color:#AAC315; color: white">their </span>
<span style="background-color:#697D54; color: white">breath, </span><br/>
<span style="background-color:#39DD2B; color: white">and </span>
<span style="background-color:#AAC315; color: white">for </span>
<span style="background-color:#1B0661; color: white">His </span>
<span style="background-color:#697D54; color: white">death </span>
<span style="background-color:#39DD2B; color: white">they </span>
<span style="background-color:#697D54; color: white">thirst </span>
<span style="background-color:#39DD2B; color: white">and cry. </span><br/><br/>
<span style="background-color:#1B0661; color: white">Why, </span>
<span style="background-color:#39DD2B; color: white">what </span>
<span style="background-color:#A87DCF; color: white">hath </span>
<span style="background-color:#1B0661; color: white">my </span>
<span style="background-color:#4CA999; color: white">Lord </span>
<span style="background-color:#697D54; color: white">done? </span><br/>
<span style="background-color:#39DD2B; color: white">What makes </span>
<span style="background-color:#736F2D; color: white">this </span>
<span style="background-color:#A87DCF; color: white">rage </span>
<span style="background-color:#1B0661; color: white">and </span>
<span style="background-color:#736F2D; color: white">spite? </span><br/>
<span style="background-color:#A87DCF; color: white">He </span>
<span style="background-color:#736F2D; color: white">made </span>
<span style="background-color:#A87DCF; color: white">the </span>
<span style="background-color:#736F2D; color: white">lame </span>
<span style="background-color:#A87DCF; color: white">to </span>
<span style="background-color:#697D54; color: white">run, </span><br/>
<span style="background-color:#A87DCF; color: white">He </span>
<span style="background-color:#736F2D; color: white">gave </span>
<span style="background-color:#A87DCF; color: white">the </span>
<span style="background-color:#736F2D; color: white">blind </span>
<span style="background-color:#4CA999; color: white">their </span>
<span style="background-color:#736F2D; color: white">sight. </span><br/>
<span style="background-color:#736F2D; color: white">Sweet injuries! Yet they at these </span><br/>
<span style="background-color:#A87DCF; color: white">themselves </span>
<span style="background-color:#736F2D; color: white">displease, </span>
<span style="background-color:#1B0661; color: white">and </span>
<span style="background-color:#736F2D; color: white">'gainst </span>
<span style="background-color:#1B0661; color: white">him rise. </span><br/>