<s> from sys import argv <EOL> import random <EOL> import re <EOL> import nltk <EOL> import roman <EOL> import operator <EOL> from nltk . corpus import cmudict <EOL> from nltk . corpus import wordnet as wn <EOL> from nltk . stem . snowball import SnowballStemmer <EOL> from nltk . probability import LidstoneProbDist <EOL> if len ( argv ) == <NUM_LIT:6> : <EOL> script , book , rhyme_scheme , poem_count , output_format , show_diagnostics = argv <EOL> elif len ( argv ) == <NUM_LIT:5> : <EOL> script , book , rhyme_scheme , poem_count , output_format = argv <EOL> show_diagnostics = '<STR_LIT:y>' <EOL> elif len ( argv ) == <NUM_LIT:4> : <EOL> script , book , rhyme_scheme , poem_count = argv <EOL> output_format = '<STR_LIT>' <EOL> show_diagnostics = '<STR_LIT:y>' <EOL> elif len ( argv ) == <NUM_LIT:3> : <EOL> script , book , rhyme_scheme = argv <EOL> poem_count = '<STR_LIT>' <EOL> output_format = '<STR_LIT>' <EOL> show_diagnostics = '<STR_LIT:y>' <EOL> else : <EOL> print "<STR_LIT>" <EOL> e = cmudict . entries ( ) <EOL> d = cmudict . dict ( ) <EOL> st = SnowballStemmer ( "<STR_LIT>" ) <EOL> banned_end_words = [ '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:o>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> banned_word_combos = [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:to>' ] , [ '<STR_LIT>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT:all>' , '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> f = open ( book ) <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> t = f . read ( ) <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> w = nltk . word_tokenize ( t ) <EOL> def remove_ml_words ( ) : <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> nv = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> signal = [ ] <EOL> for ( i , j ) in pos : <EOL> for x in nv : <EOL> if j == x and not i in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> signal . append ( i ) <EOL> else : <EOL> pass <EOL> return signal <EOL> def make_word_list ( ) : <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> word_list = [ ] <EOL> for i in w : <EOL> try : <EOL> d [ i . lower ( ) ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> if i . lower ( ) == "<STR_LIT>" : <EOL> pass <EOL> elif i [ - <NUM_LIT:1> ] == "<STR_LIT:.>" : <EOL> pass <EOL> else : <EOL> word_list . append ( ( i . lower ( ) , d [ i . lower ( ) ] [ <NUM_LIT:0> ] ) ) <EOL> return word_list <EOL> word_list = make_word_list ( ) <EOL> def valid_words ( ) : <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> vw = [ ] <EOL> for ( x , y ) in word_list : <EOL> vw . append ( x ) <EOL> return vw <EOL> vw = valid_words ( ) <EOL> vw_u = list ( set ( vw ) ) <EOL> def unique ( s ) : <EOL> u = [ ] <EOL> for x in s : <EOL> if x not in u : <EOL> u . append ( x ) <EOL> else : <EOL> pass <EOL> return u <EOL> word_list_u = unique ( word_list ) <EOL> def in_pdict ( word ) : <EOL> try : <EOL> d [ word ] <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> return True <EOL> def sylcount ( s ) : <EOL> try : <EOL> d [ s ] <EOL> except KeyError : <EOL> return None <EOL> else : <EOL> if len ( d [ s ] ) <= <NUM_LIT:1> : <EOL> sj = '<STR_LIT>' . join ( d [ s ] [ <NUM_LIT:0> ] ) <EOL> sl = re . split ( '<STR_LIT>' , sj ) <EOL> return len ( sl ) - <NUM_LIT:1> <EOL> else : <EOL> sj0 = '<STR_LIT>' . join ( d [ s ] [ <NUM_LIT:0> ] ) <EOL> sl0 = re . split ( '<STR_LIT>' , sj0 ) <EOL> sj1 = '<STR_LIT>' . join ( d [ s ] [ <NUM_LIT:1> ] ) <EOL> sl1 = re . split ( '<STR_LIT>' , sj1 ) <EOL> if len ( sl1 ) < len ( sl0 ) : <EOL> return len ( sl1 ) - <NUM_LIT:1> <EOL> else : <EOL> return len ( sl0 ) - <NUM_LIT:1> <EOL> def line_sylcount ( line ) : <EOL> count = <NUM_LIT:0> <EOL> for word in line : <EOL> count += sylcount ( word ) <EOL> return count <EOL> def meter ( word ) : <EOL> pron = d [ word ] <EOL> m1 = [ ] <EOL> m2 = [ ] <EOL> mx = [ ] <EOL> if len ( pron ) == <NUM_LIT:1> : <EOL> for i in pron [ <NUM_LIT:0> ] : <EOL> if '<STR_LIT:0>' in i : <EOL> m1 . append ( <NUM_LIT:0> ) <EOL> elif '<STR_LIT:1>' in i : <EOL> m1 . append ( <NUM_LIT:1> ) <EOL> elif '<STR_LIT:2>' in i : <EOL> m1 . append ( <NUM_LIT:2> ) <EOL> else : <EOL> pass <EOL> mx = [ m1 ] <EOL> elif len ( pron ) >= <NUM_LIT:2> : <EOL> for i in pron [ <NUM_LIT:0> ] : <EOL> if '<STR_LIT:0>' in i : <EOL> m1 . append ( <NUM_LIT:0> ) <EOL> elif '<STR_LIT:1>' in i : <EOL> m1 . append ( <NUM_LIT:1> ) <EOL> elif '<STR_LIT:2>' in i : <EOL> m1 . append ( <NUM_LIT:2> ) <EOL> else : <EOL> pass <EOL> for i in pron [ <NUM_LIT:1> ] : <EOL> if '<STR_LIT:0>' in i : <EOL> m2 . append ( <NUM_LIT:0> ) <EOL> elif '<STR_LIT:1>' in i : <EOL> m2 . append ( <NUM_LIT:1> ) <EOL> elif '<STR_LIT:2>' in i : <EOL> m2 . append ( <NUM_LIT:2> ) <EOL> else : <EOL> pass <EOL> mx = [ m1 , m2 ] <EOL> m = [ ] <EOL> if len ( mx ) == <NUM_LIT:1> : <EOL> w0 = reduce ( operator . mul , mx [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> if w0 >= <NUM_LIT:2> : <EOL> for i in mx [ <NUM_LIT:0> ] : <EOL> if i == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif i == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:1> : <EOL> for i in mx [ <NUM_LIT:0> ] : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:0> : <EOL> for i in mx [ <NUM_LIT:0> ] : <EOL> if i == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif i == <NUM_LIT:1> or i == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif len ( mx ) == <NUM_LIT:2> : <EOL> w0 = reduce ( operator . mul , mx [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> w1 = reduce ( operator . mul , mx [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> if w0 >= <NUM_LIT:2> and w1 >= <NUM_LIT:2> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i * j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif i * j == <NUM_LIT:4> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif i * j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif w0 == <NUM_LIT:1> and w1 == <NUM_LIT:1> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:0> and w1 == <NUM_LIT:0> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i == j and i * j >= <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif i != j and i * j == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == j and i * j == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif w0 >= <NUM_LIT:2> and w1 == <NUM_LIT:0> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i == <NUM_LIT:1> and j == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif i == <NUM_LIT:2> and j == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:1> and j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:1> and j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:2> and j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif i == <NUM_LIT:2> and j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:0> and w1 >= <NUM_LIT:2> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i == <NUM_LIT:0> and j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:u>' ) <EOL> elif i == <NUM_LIT:0> and j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:1> and j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:2> and j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:1> and j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif i == <NUM_LIT:2> and j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:1> and w1 >= <NUM_LIT:2> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 >= <NUM_LIT:2> and w1 == <NUM_LIT:1> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:1> and w1 == <NUM_LIT:0> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if j == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif j == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif j == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif w0 == <NUM_LIT:0> and w1 == <NUM_LIT:1> : <EOL> for ( i , j ) in zip ( mx [ <NUM_LIT:0> ] , mx [ <NUM_LIT:1> ] ) : <EOL> if i == <NUM_LIT:0> : <EOL> m . append ( '<STR_LIT:x>' ) <EOL> elif i == <NUM_LIT:1> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> elif i == <NUM_LIT:2> : <EOL> m . append ( '<STR_LIT:s>' ) <EOL> return m <EOL> def strip_numbers ( x ) : <EOL> xj = '<STR_LIT:.>' . join ( x ) <EOL> xl = re . split ( '<STR_LIT>' , xj ) <EOL> xjx = '<STR_LIT>' . join ( xl ) <EOL> xlx = xjx . split ( '<STR_LIT:.>' ) <EOL> return xlx <EOL> def get_pron ( word ) : <EOL> if not in_pdict ( word ) : <EOL> pron = None <EOL> else : <EOL> if len ( d [ word ] ) <= <NUM_LIT:1> : <EOL> pron = d [ word ] [ <NUM_LIT:0> ] <EOL> else : <EOL> p0 = d [ word ] [ <NUM_LIT:0> ] <EOL> p1 = d [ word ] [ <NUM_LIT:1> ] <EOL> sj0 = '<STR_LIT>' . join ( p0 ) <EOL> sl0 = re . split ( '<STR_LIT>' , sj0 ) <EOL> sj1 = '<STR_LIT>' . join ( p1 ) <EOL> sl1 = re . split ( '<STR_LIT>' , sj1 ) <EOL> if len ( sl1 ) < len ( sl0 ) : <EOL> pron = p1 <EOL> else : <EOL> pron = p0 <EOL> return pron <EOL> def last_stressed_vowel ( word ) : <EOL> pron = get_pron ( word ) <EOL> mtr = meter ( word ) <EOL> vowel_index = [ ] <EOL> if len ( mtr ) == <NUM_LIT:1> : <EOL> lsv = - <NUM_LIT:1> <EOL> elif mtr [ - <NUM_LIT:1> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:1> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:1> <EOL> elif mtr [ - <NUM_LIT:2> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:2> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:2> <EOL> elif mtr [ - <NUM_LIT:3> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:3> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:3> <EOL> elif mtr [ - <NUM_LIT:4> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:4> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:4> <EOL> elif mtr [ - <NUM_LIT:5> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:5> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:5> <EOL> elif mtr [ - <NUM_LIT:6> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:6> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:6> <EOL> elif mtr [ - <NUM_LIT:7> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:7> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:7> <EOL> elif mtr [ - <NUM_LIT:8> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:8> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:8> <EOL> elif mtr [ - <NUM_LIT:9> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:9> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:9> <EOL> elif mtr [ - <NUM_LIT:10> ] == '<STR_LIT:s>' or mtr [ - <NUM_LIT:10> ] == '<STR_LIT:x>' : <EOL> lsv = - <NUM_LIT:10> <EOL> else : <EOL> lsv = - <NUM_LIT:1> <EOL> for i in pron : <EOL> if '<STR_LIT:0>' in i or '<STR_LIT:1>' in i or '<STR_LIT:2>' in i : <EOL> vowel_index . append ( pron . index ( i ) ) <EOL> else : <EOL> continue <EOL> return vowel_index [ lsv ] <EOL> def rhyme_finder ( word ) : <EOL> rhyming_words = [ ] <EOL> pron = get_pron ( word ) <EOL> pron = strip_numbers ( pron ) <EOL> lsv = last_stressed_vowel ( word ) <EOL> rhyme_part = pron [ lsv : ] <EOL> lrp = len ( rhyme_part ) * - <NUM_LIT:1> <EOL> for ( x , y ) in word_list_u : <EOL> ps = strip_numbers ( y ) <EOL> if ps [ lrp : ] == rhyme_part and ps [ lrp - <NUM_LIT:1> : ] != pron [ lsv - <NUM_LIT:1> : ] : <EOL> rhyming_words . append ( x ) <EOL> else : <EOL> pass <EOL> rw = [ i for i in rhyming_words if not i == word ] <EOL> rw2 = [ j for j in rw if not j in banned_end_words ] <EOL> return rw2 <EOL> def contains_rhyme ( rs ) : <EOL> rs_list = [ i for i in rs ] <EOL> letters = [ ] <EOL> for x in rs_list : <EOL> try : <EOL> int ( x ) <EOL> except ValueError : <EOL> letters . append ( x ) <EOL> else : <EOL> pass <EOL> letters_u = list ( set ( letters ) ) <EOL> if len ( letters_u ) == len ( letters ) : <EOL> return False <EOL> else : <EOL> return True <EOL> if contains_rhyme ( rhyme_scheme ) or "<STR_LIT>" in rhyme_scheme . lower ( ) : <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> rhyme_dict = { } <EOL> rhyme_counts = [ ] <EOL> for word in vw_u : <EOL> rhymes = rhyme_finder ( word ) <EOL> rhyme_dict [ word ] = rhymes <EOL> rhyme_counts . append ( len ( rhymes ) ) <EOL> rhyme_freq = { } <EOL> for i in range ( max ( rhyme_counts ) + <NUM_LIT:1> ) : <EOL> rhyme_freq [ i ] = [ ] <EOL> for word in vw_u : <EOL> rhyme_freq [ len ( rhyme_dict [ word ] ) ] . append ( word ) <EOL> else : <EOL> pass <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> estimator = lambda fdist , bins : LidstoneProbDist ( fdist , <NUM_LIT> ) <EOL> content_model = nltk . NgramModel ( <NUM_LIT:3> , vw , estimator = estimator ) <EOL> def sw ( ) : <EOL> sw1 = random . randint ( <NUM_LIT:0> , len ( vw ) - <NUM_LIT:2> ) <EOL> return [ vw [ sw1 ] , vw [ sw1 + <NUM_LIT:1> ] ] <EOL> def generate_line ( prior_words ) : <EOL> line = [ ] <EOL> sc = <NUM_LIT:0> <EOL> word_y = content_model . generate ( <NUM_LIT:1> , prior_words ) <EOL> word_y = word_y [ - <NUM_LIT:1> ] <EOL> line += [ prior_words [ - <NUM_LIT:1> ] , word_y ] <EOL> sc += sylcount ( word_y ) <EOL> while sc < <NUM_LIT:10> : <EOL> word_z = content_model . generate ( <NUM_LIT:1> , line [ - <NUM_LIT:2> : ] ) <EOL> word_z = word_z [ - <NUM_LIT:1> ] <EOL> line . append ( word_z ) <EOL> sc += sylcount ( word_z ) <EOL> return line [ <NUM_LIT:1> : ] <EOL> def generate_sonnet ( starting_words ) : <EOL> sonnet = [ ] <EOL> lx = generate_line ( starting_words ) <EOL> sonnet . append ( lx ) <EOL> if "<STR_LIT>" in rhyme_scheme : <EOL> rs = rhyme_scheme . split ( '<STR_LIT:_>' ) <EOL> line_count = int ( rs [ <NUM_LIT:1> ] ) <EOL> else : <EOL> line_count = len ( rhyme_scheme ) <EOL> for i in range ( line_count - <NUM_LIT:1> ) : <EOL> ly = generate_line ( lx [ - <NUM_LIT:2> : ] ) <EOL> sonnet . append ( ly ) <EOL> lx = ly <EOL> return sonnet <EOL> def plagiarism_check ( sonnet ) : <EOL> for line in sonnet [ : ] : <EOL> if any ( line == vw [ i : i + len ( line ) ] for i in xrange ( len ( vw ) - len ( line ) + <NUM_LIT:1> ) ) : <EOL> sonnet . remove ( line ) <EOL> else : <EOL> pass <EOL> return sonnet <EOL> def line_syn_replace ( line ) : <EOL> pos = nltk . pos_tag ( line ) <EOL> nvaa = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> candidates = [ ] <EOL> for h in range ( len ( pos ) ) : <EOL> if pos [ h ] [ <NUM_LIT:1> ] in nvaa : <EOL> candidates . append ( h ) <EOL> else : <EOL> pass <EOL> synset_count_list = [ ] <EOL> lemma_list = [ ] <EOL> for n in candidates : <EOL> ss = wn . synsets ( line [ n ] ) <EOL> synset_count_list . append ( len ( ss ) ) <EOL> lemmas = [ ] <EOL> for i in ss : <EOL> lemmas += i . lemma_names <EOL> lemmas = list ( set ( lemmas ) ) <EOL> for lem in lemmas [ : ] : <EOL> if st . stem ( line [ n ] ) == st . stem ( lem ) : <EOL> lemmas . remove ( lem ) <EOL> else : <EOL> pass <EOL> lemma_list . append ( lemmas ) <EOL> pos_ss_count = [ synset_count_list [ j ] for j in range ( len ( candidates ) ) if synset_count_list [ j ] > <NUM_LIT:0> and lemma_list [ j ] != [ ] ] <EOL> if pos_ss_count == [ ] : <EOL> synonym = "<STR_LIT>" <EOL> else : <EOL> min_pos_ss_count = min ( pos_ss_count ) <EOL> for k in range ( len ( candidates ) ) : <EOL> if synset_count_list [ k ] == min_pos_ss_count and lemma_list [ k ] != [ ] : <EOL> word_index = candidates [ k ] <EOL> syn_candidates = lemma_list [ k ] <EOL> break <EOL> else : <EOL> pass <EOL> for l in range ( len ( syn_candidates ) ) : <EOL> syn_candidates [ l ] = syn_candidates [ l ] . lower ( ) <EOL> if '<STR_LIT:_>' in syn_candidates [ l ] : <EOL> tp = syn_candidates [ l ] . split ( '<STR_LIT:_>' ) <EOL> tj = '<STR_LIT:U+0020>' . join ( tp ) <EOL> syn_candidates [ l ] = tj <EOL> else : <EOL> pass <EOL> synonym = random . choice ( syn_candidates ) <EOL> line [ word_index ] = synonym <EOL> if "<STR_LIT>" in rhyme_scheme . lower ( ) : <EOL> if '<STR_LIT:U+0020>' in synonym : <EOL> synonym = synonym . split ( '<STR_LIT:U+0020>' ) <EOL> else : <EOL> synonym = [ synonym ] <EOL> for w in synonym : <EOL> if in_pdict ( w ) : <EOL> rhymes = rhyme_finder ( w ) <EOL> rhyme_dict [ w ] = rhymes <EOL> if len ( rhymes ) > max ( rhyme_counts ) : <EOL> rhyme_counts . append ( len ( rhymes ) ) <EOL> rhyme_freq [ len ( rhymes ) ] = [ w ] <EOL> else : <EOL> rhyme_counts . append ( len ( rhymes ) ) <EOL> rhyme_freq [ len ( rhymes ) ] . append ( w ) <EOL> else : <EOL> rhyme_dict [ w ] = [ ] <EOL> if <NUM_LIT:0> in rhyme_counts : <EOL> rhyme_counts . append ( <NUM_LIT:0> ) <EOL> rhyme_freq [ <NUM_LIT:0> ] . append ( w ) <EOL> else : <EOL> rhyme_counts . append ( <NUM_LIT:0> ) <EOL> rhyme_freq [ <NUM_LIT:0> ] = [ w ] <EOL> return line <EOL> def plagiarism_syn_replace ( sonnet ) : <EOL> for i in range ( len ( sonnet ) ) : <EOL> if any ( sonnet [ i ] == vw [ j : j + len ( sonnet [ i ] ) ] for j in xrange ( len ( vw ) - len ( sonnet [ i ] ) + <NUM_LIT:1> ) ) : <EOL> sonnet [ i ] = line_syn_replace ( sonnet [ i ] ) <EOL> else : <EOL> pass <EOL> return sonnet <EOL> def end_words ( sonnet ) : <EOL> for i in range ( len ( sonnet ) - <NUM_LIT:1> ) : <EOL> while sonnet [ i ] [ - <NUM_LIT:1> ] in banned_end_words : <EOL> if any ( [ sonnet [ i ] [ - <NUM_LIT:1> ] , sonnet [ i + <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] == vw [ j : j + <NUM_LIT:2> ] for j in xrange ( len ( vw ) - <NUM_LIT:1> ) ) : <EOL> break <EOL> else : <EOL> sonnet [ i ] = sonnet [ i ] [ : - <NUM_LIT:1> ] <EOL> else : <EOL> pass <EOL> return sonnet <EOL> def line_rhymer ( sonnet , line_numbers ) : <EOL> origin_word = sonnet [ line_numbers [ <NUM_LIT:0> ] ] [ - <NUM_LIT:1> ] <EOL> candidate_count = len ( line_numbers ) - <NUM_LIT:1> <EOL> if len ( rhyme_dict [ origin_word ] ) >= candidate_count : <EOL> pass <EOL> else : <EOL> new_owc = [ ] <EOL> for i in range ( candidate_count , max ( rhyme_counts ) + <NUM_LIT:1> ) : <EOL> new_owc += rhyme_freq [ i ] <EOL> origin_word = random . choice ( new_owc ) <EOL> sonnet [ line_numbers [ <NUM_LIT:0> ] ] [ - <NUM_LIT:1> ] = origin_word <EOL> rhyming_words = random . sample ( rhyme_dict [ origin_word ] , candidate_count ) <EOL> for i in range ( candidate_count ) : <EOL> sonnet [ line_numbers [ i + <NUM_LIT:1> ] ] [ - <NUM_LIT:1> ] = rhyming_words [ i ] <EOL> return sonnet <EOL> def line_repeater ( sonnet , line_numbers ) : <EOL> origin_line = sonnet [ line_numbers [ <NUM_LIT:0> ] ] <EOL> for i in line_numbers [ <NUM_LIT:1> : ] : <EOL> sonnet [ i ] = origin_line <EOL> return sonnet <EOL> def rhymer ( sonnet ) : <EOL> rs_list = [ i for i in rhyme_scheme ] <EOL> rs_u = unique ( rs_list ) <EOL> rs_u_n = [ ] <EOL> rs_u_l = [ ] <EOL> for des in rs_u : <EOL> try : <EOL> int ( des ) <EOL> except ValueError : <EOL> rs_u_l . append ( des ) <EOL> else : <EOL> rs_u_n . append ( des ) <EOL> rs_n_loc = [ [ ] for i in rs_u_n ] <EOL> locations = [ [ ] for i in rs_u_l ] <EOL> for i in range ( len ( rs_u_l ) ) : <EOL> for j in range ( len ( rs_list ) ) : <EOL> if rs_u_l [ i ] == rs_list [ j ] : <EOL> locations [ i ] . append ( j ) <EOL> else : <EOL> pass <EOL> for n in range ( len ( rs_u_n ) ) : <EOL> for m in range ( len ( rs_list ) ) : <EOL> if rs_u_n [ n ] == rs_list [ m ] : <EOL> rs_n_loc [ n ] . append ( m ) <EOL> else : <EOL> pass <EOL> for x in locations : <EOL> if len ( x ) <= <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> sonnet = line_rhymer ( sonnet , x ) <EOL> for y in rs_n_loc : <EOL> if len ( y ) <= <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> sonnet = line_repeater ( sonnet , y ) <EOL> return sonnet <EOL> def slant_rhyme_loc ( word , word_set ) : <EOL> loc = [ ] <EOL> pron = get_pron ( word ) <EOL> if pron == None : <EOL> pass <EOL> else : <EOL> pron = strip_numbers ( pron ) <EOL> for i in range ( len ( word_set ) ) : <EOL> ipron = get_pron ( word_set [ i ] ) <EOL> if ipron == None : <EOL> pass <EOL> else : <EOL> ipron = strip_numbers ( ipron ) <EOL> if pron == ipron : <EOL> pass <EOL> else : <EOL> if len ( pron ) == <NUM_LIT:1> : <EOL> if any ( pron [ <NUM_LIT:0> ] == ipron [ j ] for j in xrange ( len ( ipron ) ) ) : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> elif len ( ipron ) == <NUM_LIT:1> : <EOL> if any ( ipron [ <NUM_LIT:0> ] == pron [ j ] for j in xrange ( len ( pron ) ) ) : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> else : <EOL> for x in range ( len ( pron ) - <NUM_LIT:1> ) : <EOL> for y in range ( len ( ipron ) - <NUM_LIT:1> ) : <EOL> if pron [ x ] == ipron [ y ] and pron [ x + <NUM_LIT:1> ] == ipron [ y + <NUM_LIT:1> ] : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> loc = list ( set ( loc ) ) <EOL> return loc <EOL> def rhyme_loc ( word , word_set ) : <EOL> loc = [ ] <EOL> rhyming_words = rhyme_dict [ word ] <EOL> for i in range ( len ( word_set ) ) : <EOL> if word_set [ i ] in rhyming_words : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> return loc <EOL> def rep_loc ( word , word_set ) : <EOL> loc = [ ] <EOL> for i in range ( len ( word_set ) ) : <EOL> if word == word_set [ i ] : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> return loc <EOL> def noun_loc ( word_set ) : <EOL> loc = [ ] <EOL> pos = nltk . pos_tag ( word_set ) <EOL> rs = rhyme_scheme . split ( '<STR_LIT:_>' ) <EOL> if len ( rs ) > <NUM_LIT:2> : <EOL> if rs [ <NUM_LIT:2> ] == '<STR_LIT:n>' : <EOL> for i in range ( len ( word_set ) ) : <EOL> if pos [ i ] [ <NUM_LIT:1> ] in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> elif rs [ <NUM_LIT:2> ] == '<STR_LIT:v>' : <EOL> for i in range ( len ( word_set ) ) : <EOL> if pos [ i ] [ <NUM_LIT:1> ] in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> else : <EOL> for i in range ( len ( word_set ) ) : <EOL> if pos [ i ] [ <NUM_LIT:1> ] in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> loc . append ( i ) <EOL> else : <EOL> pass <EOL> return loc <EOL> def line_length_counter ( b , word_list ) : <EOL> lls = [ b [ <NUM_LIT:0> ] ] <EOL> for i in range ( <NUM_LIT:1> , len ( b ) ) : <EOL> lls . append ( b [ i ] - b [ i - <NUM_LIT:1> ] ) <EOL> lls . append ( len ( word_list ) - b [ - <NUM_LIT:1> ] ) <EOL> return lls <EOL> def natural_breaks ( sonnet ) : <EOL> all_words = [ ] <EOL> for line in sonnet : <EOL> all_words += line <EOL> for i in range ( len ( all_words ) ) : <EOL> if '<STR_LIT:U+0020>' in all_words [ i ] : <EOL> spl = all_words [ i ] . split ( '<STR_LIT:U+0020>' ) <EOL> all_words [ i ] = spl [ <NUM_LIT:0> ] <EOL> for j in range ( <NUM_LIT:1> , len ( spl ) ) : <EOL> all_words . insert ( i + j , spl [ j ] ) <EOL> else : <EOL> pass <EOL> rhyme_locs = [ rhyme_loc ( w , all_words ) for w in all_words ] <EOL> noun_locs = noun_loc ( all_words ) <EOL> breaks = [ ] <EOL> line_lengths = [ len ( all_words ) ] <EOL> if any ( bool ( i ) for i in rhyme_locs ) : <EOL> for j in rhyme_locs : <EOL> if bool ( j ) : <EOL> breaks += j <EOL> else : <EOL> pass <EOL> breaks = sorted ( list ( set ( breaks ) ) ) <EOL> line_lengths = line_length_counter ( breaks , all_words ) <EOL> else : <EOL> pass <EOL> if len ( breaks ) <= ( len ( sonnet ) / <NUM_LIT:2> ) : <EOL> breaks += noun_locs <EOL> else : <EOL> pass <EOL> breaks = sorted ( list ( set ( breaks ) ) ) <EOL> s = [ ] <EOL> s . append ( all_words [ : ( breaks [ <NUM_LIT:0> ] + <NUM_LIT:1> ) ] ) <EOL> for i in range ( len ( breaks ) - <NUM_LIT:1> ) : <EOL> s . append ( all_words [ breaks [ i ] + <NUM_LIT:1> : breaks [ i + <NUM_LIT:1> ] + <NUM_LIT:1> ] ) <EOL> s . append ( all_words [ breaks [ - <NUM_LIT:1> ] + <NUM_LIT:1> : len ( all_words ) + <NUM_LIT:1> ] ) <EOL> while len ( s [ - <NUM_LIT:1> ] ) > <NUM_LIT:0> and s [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] in banned_end_words : <EOL> s [ - <NUM_LIT:1> ] = s [ - <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] <EOL> if s [ - <NUM_LIT:1> ] == [ ] : <EOL> del s [ - <NUM_LIT:1> ] <EOL> else : <EOL> pass <EOL> return s <EOL> def plagiarized ( sonnet ) : <EOL> for i in range ( len ( sonnet ) - <NUM_LIT:1> ) : <EOL> if any ( sonnet [ i ] + sonnet [ i + <NUM_LIT:1> ] == vw [ j : j + len ( sonnet [ i ] ) + len ( sonnet [ i + <NUM_LIT:1> ] ) ] for j in xrange ( len ( vw ) - len ( sonnet [ i ] ) - len ( sonnet [ i + <NUM_LIT:1> ] ) + <NUM_LIT:1> ) ) : <EOL> plag = True <EOL> break <EOL> else : <EOL> plag = False <EOL> return plag <EOL> def banned_word_combo_fixer ( l ) : <EOL> l = '<STR_LIT:U+0020>' . join ( l ) <EOL> for combo in banned_word_combos : <EOL> if '<STR_LIT:U+0020>' . join ( combo ) in l : <EOL> l = l . split ( '<STR_LIT:U+0020>' ) <EOL> for i in range ( len ( l ) - len ( combo ) ) : <EOL> if l [ i : i + len ( combo ) ] == combo : <EOL> l [ i : i + len ( combo ) ] = [ random . choice ( combo ) ] <EOL> l = '<STR_LIT:U+0020>' . join ( l ) <EOL> break <EOL> else : <EOL> pass <EOL> else : <EOL> pass <EOL> return l <EOL> if output_format . lower ( ) == "<STR_LIT>" : <EOL> latex_lb = "<STR_LIT>" <EOL> elif output_format . lower ( ) == "<STR_LIT>" : <EOL> latex_lb = "<STR_LIT>" <EOL> elif output_format . lower ( ) == "<STR_LIT>" : <EOL> latex_lb = "<STR_LIT:.>" <EOL> def sonnetizer ( ) : <EOL> title = sw ( ) <EOL> s = generate_sonnet ( title ) <EOL> while plagiarized ( s ) : <EOL> title = sw ( ) <EOL> s = generate_sonnet ( title ) <EOL> s = plagiarism_syn_replace ( s ) <EOL> s = end_words ( s ) <EOL> if "<STR_LIT>" in rhyme_scheme : <EOL> s = natural_breaks ( s ) <EOL> else : <EOL> s = rhymer ( s ) <EOL> while s [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] in banned_end_words : <EOL> s [ - <NUM_LIT:1> ] = s [ - <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] <EOL> sonnet = [ ] <EOL> for i in range ( len ( s ) ) : <EOL> line = s [ i ] <EOL> line = banned_word_combo_fixer ( line ) <EOL> line = line + latex_lb <EOL> sonnet . append ( line ) <EOL> return [ '<STR_LIT:U+0020>' . join ( title ) . upper ( ) , '<STR_LIT:\n>' . join ( sonnet ) + '<STR_LIT:\n>' + latex_lb + '<STR_LIT:\n>' + latex_lb ] <EOL> if show_diagnostics . lower ( ) == '<STR_LIT:y>' : <EOL> print "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:0> , int ( poem_count ) , <NUM_LIT:2> ) : <EOL> sonnet1 = sonnetizer ( ) <EOL> sonnet2 = sonnetizer ( ) <EOL> if output_format == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> else : <EOL> pass <EOL> print str ( i + <NUM_LIT:1> ) + '<STR_LIT>' + sonnet1 [ <NUM_LIT:0> ] + latex_lb <EOL> print sonnet1 [ <NUM_LIT:1> ] <EOL> print str ( i + <NUM_LIT:2> ) + '<STR_LIT>' + sonnet2 [ <NUM_LIT:0> ] + latex_lb <EOL> print sonnet2 [ <NUM_LIT:1> ] <EOL> if output_format == "<STR_LIT>" : <EOL> print '<STR_LIT>' <EOL> else : <EOL> pass </s>
