Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Write a poem based on themes and keywords """
#from pattern import sentiment, parsetree #from pattern import search
""" Get vocabulary from themes, themes being words Actually get lines not individual words """ '"', cached=False): plaintext(tweet.text).encode('ascii', 'ignore'))
vocabulary.append(m.group(2).string)
# http://www.clips.ua.ac.be/pages/pattern-search #get related vocabulary from theme : Pattern
#word-sense disembiguation in Python # https://github.com/alvations/pywsd disambiguated = lesk(context_sentence=phrase, ambiguous_word=word) return disambiguated.definition()
""" Find synonyms or lemma from WordNet """ synonyms = [] for ss in wn.synsets(word): synonyms.append(ss.name().split(".")[0].replace('_', ' ')) for sim in ss.similar_tos(): synonyms.append(sim.name().split(".")[0].replace('_', ' ')) return list(set(synonyms)) # http://stackoverflow.com/questions/5534926/to-find-synonyms-defintions-and-example-sentences-using-wordnet
""" Count syllables in a piece of text Not very reliable """ #could syllables : Pyphen #from nltk_contrib.readability.textanalyzer import syllables_en #print syllables_en.count("potatoes ") # http://image.slidesharecdn.com/nltk-110105180423-phpapp02/95/nltk-natural-language-processing-in-python-22-728.jpg?cb=1309726267 # using PyPhen instead
#detect rhymes : Metaphone word1_a, word1_b = doublemetaphone(word1) word2_a, word2_b = doublemetaphone(word2) return word1_a[-1] == word2_a[-1] # does not actually use the 2nd value... could use metaphone simple instead?
""" Find words from a list with the same number of syllables than a given word """ result = [] for candidate_word in list_of_words: if count_syllables(candidate_word) == count_syllables(word): result.append(candidate_word) return result
""" Find words from a list rhyming with a given word """ result = [] for candidate_word in list_of_words: if words_rhymes(candidate_word, word): result.append(candidate_word) return result
#spell check : Hunspell potential_mistakes = {} hobj = hunspell.HunSpell('/usr/share/hunspell/en_US.dic', '/usr/share/hunspell/en_US.aff') for word in re.findall(r"[\w']+", text): if not hobj.spell(word): potential_mistakes[word] = hobj.suggest(word) return potential_mistakes
#sentiment analysis : Pattern sentiment() return sentiment(text) #useless so far, just a personal warpper # for meaning of values see http://www.clips.ua.ac.be/pages/pattern-en#sentiment
#replace the first found tag #print replace_tag("I want to eat an apple.", "NN", "orange") # print t
# text is not a string but a list of verses if not len(text) == len(structure): print 'failed number of lines' return False for i, line in enumerate(text): if not count_syllables(line) == structure[i][0]: # should be able to handle None too when syllable count does not matter print 'failed syllables' print i return False if not structure[i][1] == None: if not words_rhymes(line, text[structure[i][1]]): # redundant test at corresponding later line print 'failed rhyming' print i return False return True # poor return value, unable to tell what failed
#should become a grammar respecting function #for grammatical rules tokenized_sent = word_tokenize(text) pos_tagged = pos_tag(tokenized_sent) testinggrammar = [] for t in pos_tagged: testinggrammar.append(t[-1]) return testinggrammar == grammar # http://image.slidesharecdn.com/nltk-110105180423-phpapp02/95/nltk-natural-language-processing-in-python-22-728.jpg?cb=1309726267
testwords.append(arg)
# include nicknames as potential synonym for each name
#structures['rhymetest'] = [(2,1),(2,0)] # redundant test # structures have : # metres or syllables # stanza or lines or verses # rhyme_form (e.g. current line rhymes with line 3) # Python indexing, line 1 is in fact index 0 # WARNING : better overdefine multiples versions of one structure # than handle strange open cases! # no free poetry but N different lines with N different rhymes # kaiku3 haiku5 haiku7 for the different number of verses # etc
#print 'vocabulary_from_theme' + str(vocabulary_from_theme)
# looping to adjust until # change to synonyms, names and nicknames # rhymes work # syllable count work # splitting in appropriate lines else: print "should get a synonym for a name or adjective (JJ*)" print "but not implemented yet" exit()
# cut in 5 7 5 #print picked_text #print sentence #print allsyllables #exit()
#print allsyllables picked_text) #print picked_text #print poem_cut #not working well so far
for line_number in range(0, len(structures[structure])): #should pop themes to insure diversity picked_text = random.choice(vocabulary_from_theme[random.choice(themes)]) if line_number > 0: if not structures[structure][line_number][-1] == None: print "should check if it rhymes" #should pop names and nicknames to insure diversity picked_text = replace_tag(picked_text, "NNS", random.choice(names)) if not structures[structure][line_number][0] == None: print "syllables ok: " + str(count_syllables(picked_text) == structures[structure][line_number][0]) poem.append(picked_text) for line in poem: print line
#https://github.com/lekhakpadmanabh/Summarizer #summarize a text that is too long
#Named Entity Recognition (NEs) in http://www.nltk.org/book/ch07.html |