From 8e7823150383c54fc5cfc0b65aa81ee507454364 Mon Sep 17 00:00:00 2001 From: CrazyPython Date: Sat, 8 Feb 2014 11:15:17 -0500 Subject: [PATCH 1/3] Funny sentence generator maker Adds classes for words and a class that makes generators. Also a class used as a namespace for some generators. (Might wanna move `SentenceGenerators` to a different file.) --- Sentence.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Sentence.py diff --git a/Sentence.py b/Sentence.py new file mode 100644 index 0000000..113aa72 --- /dev/null +++ b/Sentence.py @@ -0,0 +1,83 @@ +import random +class Word(object): + def __init__(self,s): + self.mtext=s #the word itself + self.etext=s #the word w/ adjectives and adverbs +class Adjective(Word): + pass +class Noun(Word): + def __init__(self,s,pronoun=False): + Word.__init__(self,s) + self.pronoun=pronoun + def extend(self,adjective): + self.etext = adjective.mtext+self.etext + def get_pronoun(self,prefix="the"): + if self.pronoun: + return self.mtext + else: + return self.etext.strip(self.mtext)+prefix+" "+self.mtext #add the +class Pronoun(Noun): + #s is the noun, not the pronoun + def __init__(self,s): + Noun.__init__(self,self.get_pronoun(s),pronoun=True) +class Verb(Word): + def extend(self,adjective): + self.etext = adjective.mtext+self.etext +class PastVerb(Verb): + def __init__(self,Initializer): + "Initializer is either a string, or now verb. ("killed"->"killed")(Verbobj:awkwardly kill->"awkwardly killed")" + typ = type(Initializer) + if typ==str: + self.mtext=Initializer + self.etext=Initializer + elif typ==Verb: + self.mtext=Initializer.etext + self.etext=Initializer.etext +class SentenceGen(object): + def __init__(self,l): + self.instruct=l + def __call__(self): + global nouns,verbs,pastverbs,adjectives,adverbs + adjectives_to_apply=[] + text=[] + def apply_adjectives(p,adjectives): + for adj in adjectives_to_apply:t + p.extend(adj) + return p,[] + for wtype in instruct: + if wtype == "pronoun": + p=Pronoun(random.choice(nouns)) + p,adjectives_to_apply=apply_adjectives(p,adjectives_to_apply) + elif wtype == "a_noun": + #pronoun! + p=Pronoun(random.choice(nouns),prefix="a") + p,adjectives_to_apply=apply_adjectives(p,adjectives_to_apply) + elif wtype=="noun": + p=Noun(random.choice(nouns)) + p,adjectives_to_apply=apply_adjectives(p,adjectives_to_apply) + elif wtype=="adjective": + adjectives_to_apply.append(Adjective(random.choice(adjectives))) + elif wtype=="adverb": + adjectives_to_apply.append(Adjective(random.choice(adverbs))) + elif wtype=="verb": + p=Verb(random.choice(verbs)) + p,adjectives_to_apply=apply_adjectives(p,adjectives_to_apply) + elif wtype=="pastverb": + p=PastVerb(random.choice(pastverbs)) + p,adjectives_to_apply=apply_adjectives(p,adjectives_to_apply) + else: + raise ValueError("Invalid word type:"+wtype) + text.append(p.mtext) + return text +class SentenceGenerators: + "A virtual namespace of sentence generators" + #Words to use for examples: + #Pronoun->the orange + #A_pronoun->a man + #Pastverb->hugged + #Noun->speaker + #Verb->washed + #Adjective->quickly + #Adverb->tightly + did = SentenceGen(["pronoun","pastverb","a_pronoun"]) #The orange hugged a man + did_disc = SentenceGen(["pronoun","adverb","pastverb","a_pronoun"]) #The orange tightly hugged a man From 807263eaba085da32aecbcd4c590eb262d7b019f Mon Sep 17 00:00:00 2001 From: CrazyPython Date: Sat, 8 Feb 2014 11:17:29 -0500 Subject: [PATCH 2/3] Adjective example wrong --- Sentence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sentence.py b/Sentence.py index 113aa72..3ddfd11 100644 --- a/Sentence.py +++ b/Sentence.py @@ -77,7 +77,7 @@ class SentenceGenerators: #Pastverb->hugged #Noun->speaker #Verb->washed - #Adjective->quickly + #Adjective->awesome #Adverb->tightly did = SentenceGen(["pronoun","pastverb","a_pronoun"]) #The orange hugged a man did_disc = SentenceGen(["pronoun","adverb","pastverb","a_pronoun"]) #The orange tightly hugged a man From 25a7209c279be3fc286c740dd265ec3b3cb1095c Mon Sep 17 00:00:00 2001 From: CrazyPython Date: Sat, 8 Feb 2014 11:31:53 -0500 Subject: [PATCH 3/3] Add ComplexVerb Note, this has not been integrated into SentenceGen yet. Now with support for "hugged the orange tightly" pastverb,noun,adverb --- Sentence.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sentence.py b/Sentence.py index 3ddfd11..b9d3d13 100644 --- a/Sentence.py +++ b/Sentence.py @@ -23,6 +23,12 @@ def __init__(self,s): class Verb(Word): def extend(self,adjective): self.etext = adjective.mtext+self.etext +class ComplexVerb(Verb): + def __init__(self,pastverb,noun,adverb): + "all args are strings, ex:'hugged the orange tightly'" + self.innerwords=[PastVerb(pastverb),Noun(noun),Adjective(adverb)] + s = ' '.join([word.etext for word in self.innerwords]) + Word.__init__(self,s) class PastVerb(Verb): def __init__(self,Initializer): "Initializer is either a string, or now verb. ("killed"->"killed")(Verbobj:awkwardly kill->"awkwardly killed")"