For this homework using Tracery I decided to work with an original text that has dreams I had (and wrote) during 2016.

The program extracts the verbs of it, the nouns, the adverbs and adjectives and creates new sentences.

This is the list that it takes from each group. I think there must be more nouns, because the text is long. I want to check if this is working good.

 

After loadinf JSON files from Corpora with Nouns, verbs, adverbs and adjectives.

I used Tracery like this:

#tracery rules
rules = {
‘noun’ : nounsInText,
‘verb’ : verbs_past_InText,
‘adverb’: adverbsInText,
‘adjective’: adjectivesInText,
‘origin’: [‘#noun# #verb# #adverb# #adjective#’]
}

 

I am still a bit frustated because the nouns extracted from the Original text are not all of them, I would like to use a bigger list than the corpora one or al least see whats happening.

I also want to explore more with Tracery and play more with the sentences.

But here is where I am now.  I will keep working on this.

 

Here the complete code:

 

import sys
import random
#import MyBasicsFunctions
import tracery
from tracery.modifiers import base_english
import json

dataVerbs= json.loads(open(“verbs_corpora.json”).read())
dataNouns= json.loads(open(“nouns_corpora.json”).read())
dataAdj= json.loads(open(“adj_corpora.json”).read())
dataAdverbs= json.loads(open(“adverbs_corpora.json”).read())
dataPrep= json.loads(open(“prepositions_corpora.json”).read())

#open list of verbs in past and present from Json file
verbs_past = list()
verbs_present = list()
for item in dataVerbs[“verbs”]:
verbs_past.append(item[“past”])
verbs_present.append(item[“present”])
#print verbs_past
#print verbs_present
nouns=dataNouns[“nouns”]
adjs=dataAdj[“adjs”]
adverbs=dataAdverbs [“adverbs”]
prepositions= dataPrep [“prepositions”]

#Create a list of the words of the input text
OriginalText= list()
for line in open(‘2016dreams2.txt’):
OriginalText.extend(line.split(” “))

#Check all the words of the input text and
#see if they match with any of the words of verbs_past, verbs_present, nouns, adj, adverbs, prepositions
#if they match: create a list of each.
nounsInText= list()
for noun_word in OriginalText:
if len(noun_word) > 0 and noun_word in nouns:
nounsInText.append(noun_word)

verbs_past_InText= list()
for verb_past_word in OriginalText:
if len(verb_past_word) > 0 and verb_past_word in verbs_past:
verbs_past_InText.append(verb_past_word)
#return verbsInText

adverbsInText= list()
for adverb_word in OriginalText:
if len(adverb_word) > 0 and adverb_word in adverbs:
adverbsInText.append(adverb_word)

adjectivesInText= list()
for adj_word in OriginalText:
if len(adj_word) > 0 and adj_word in adjs:
adjectivesInText.append(adj_word)

#create a variable to make random choice from the list of verbs of OriginalText
# I should create a for loop for this…
random_past_verbs = random.choice(verbs_past_InText)
random_nouns = random.choice(nounsInText)
random_adverbs = random.choice(adverbsInText)
random_adjectives = random.choice(adjectivesInText)

#print random_nouns+ ” ” +random_past_verbs +” “+ random_adverbs +” “+ random_adjectives
#print nounsInText +” “+ verbs_past_InText + adverbsInText

#create a function to make empty lines and create spaces between the words.
def multi_lines(n):
newString=””
for i in range(n):
newString +=”\n”
return newString

def multi_spaces(n):
newString=””
for i in range(n):
newString +=” ”
return newString

def new_line():
return “\n”

#tracery rules
rules = {
‘noun’ : nounsInText,
‘verb’ : verbs_past_InText,
‘adverb’: adverbsInText,
‘adjective’: adjectivesInText,
‘origin’: [‘#noun# #verb# #adverb# #adjective#’]
}

#printing the poem

print random_past_verbs + random_nouns + random_adverbs
print random_past_verbs + random_nouns + random_adverbs
print random_past_verbs + random_nouns + random_adverbs
print random_past_verbs + random_nouns + random_adverbs
print random_past_verbs + random_nouns + random_adverbs

grammar = tracery.Grammar(rules)
grammar.add_modifiers(base_english)
print nounsInText
print verbs_past_InText
print “adjetivos”
print adjectivesInText
print “adverbios”
print adverbsInText

print new_line()
print “blinded stories”
print new_line()
print grammar.flatten(“#origin#”) # prints, e.g., “Hello, world!”
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print multi_lines(1)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print new_line()
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print grammar.flatten(“#origin#”)
print multi_lines(2)