diff --git a/README.md b/README.md index 6a814ec..634f09d 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ 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: @@ -49,6 +49,7 @@ 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`. diff --git a/deep_rhyme_detection/rhyme.py b/deep_rhyme_detection/rhyme.py index 81eaeca..f046366 100644 --- a/deep_rhyme_detection/rhyme.py +++ b/deep_rhyme_detection/rhyme.py @@ -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'{block[0]} ' + # Push
after at the end of each line + if '\n' in delimited: + delimited = delimited.replace('\n', '') + '
' + delimited_html.append(delimited) + html = '\n'.join(delimited_html) + html += '
' + return html + class Poem: ''' Poems have multiple stanzas. This class gives functions for handling text with many @@ -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': @@ -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') \ No newline at end of file + 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') \ No newline at end of file diff --git a/test_files/deck_thyself_r.html b/test_files/deck_thyself_r.html new file mode 100644 index 0000000..b0ec202 --- /dev/null +++ b/test_files/deck_thyself_r.html @@ -0,0 +1,40 @@ +Deck thyself, +my +soul, +with +gladness,
+leave +the +gloomy +haunts +of +sadness;
+come into +the +daylight's +splendour,
+there +with +joy thy +praises +render
+unto +him +whose +grace unbounded
+hath +this wondrous banquet founded:
+high +o'er +all +the +heavens +he +reigneth,
+yet +to +dwell +with +thee +he +deigneth. diff --git a/test_files/love_unknown_r.html b/test_files/love_unknown_r.html new file mode 100644 index 0000000..654cde6 --- /dev/null +++ b/test_files/love_unknown_r.html @@ -0,0 +1,106 @@ +My +song +is +love +unknown,
+my +Savior's +love to me;
+love to the loveless +shown,
+that +they +might +lovely +be.
+O who +am I, +that +for +my +sake,
+my +Lord +should +take +frail flesh +and die?

+He +came from His +blest +throne
+salvation +to +bestow;
+but +man +made +strange, and none
+the +longed-for +Christ would know.
+But oh, +my Friend, my Friend +indeed,
+who at +my +need +his life did +spend!

+Sometimes +they +strew +His +way
+and +His sweet praises +sing;
+resounding +all +the +day
+hosannas +to +their +King. Then
+"Crucify!" +is +all +their +breath,
+and +for +His +death +they +thirst +and cry.

+Why, +what +hath +my +Lord +done?
+What makes +this +rage +and +spite?
+He +made +the +lame +to +run,
+He +gave +the +blind +their +sight.
+Sweet injuries! Yet they at these
+themselves +displease, +and +'gainst +him rise.