-2

I need to write a script that generates different sentences as Jean sleeps, Jean's mother sleeps, Jean's mother's cousin sleeps. Jean's sister's husband's brother sleeps and so on. For that I wrote a code of eight functions as seen below. Now I would like to write a second step, which would ask to input a sentence using family members like "my cousin's mother sleeps", and show which steps were taken to generate the sentence. Here is what I have so far:

# _*_ coding: utf-8 _*_
import random

def etat7(chaine):
    return chaine+"dort."

def etat6(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="Jean "
        return etat7(chaine+mot)
    elif choix==1:
        mot="la "
        return etat3(chaine+mot)
    elif choix==2:
        mot="l' "
        return etat4(chaine+mot)
def etat5(chaine):
    choix=random.choice(range(0,2))
    if choix==0:
        mot="de "
        return etat6(chaine+mot)
    if choix==1:
        mot="du "
        return etat2(chaine+mot)

def etat4(chaine):
    choix=random.choice(range(0,2))
    if choix==0:
        mot="ancêtre "
        return etat5(chaine+mot)
    elif choix==1:
        mot="oncle "
        return etat5(chaine+mot)

def etat3(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="mère "
        return etat5(chaine+mot)
    elif choix==1:
        mot="soeur "
        return etat5(chaine+mot)
    elif choix==2:
        mot="nièce "
        return etat5(chaine+mot)


def etat2(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="père "
        return etat5(chaine+mot)
    elif choix==1:
        mot="frère "
        return etat5(chaine+mot)
    elif choix==2:
        mot="neveu "
        return etat5(chaine+mot)

def etat1(chaine):
    choix=random.choice(range(0,3))
    if choix==0:
        mot="le "
        return etat2(chaine+mot)
    elif choix==1:
        mot="la "
        return etat3(chaine+mot)
    elif choix==2:
        mot="l' "
        return etat4(chaine+mot)

print etat1("")

At this step i have a program that generates different sentences, but I don't understand how to proceed.

Thank you

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
oezlem
  • 237
  • 2
  • 12
  • I don't speak french so I have a hard time reading this. Your code structure is also very unclean as you duplicate a lot of code. You should definitely try and simplify your code before you add anything else. – runDOSrun Feb 17 '15 at 19:00

1 Answers1

0

Take the output and parse it backwards

output = etat1("")
d = {"dort.": "etat7",
     "Jean": "etat6",
     "la": "etat6",
     "l'": "etat6",
     "de": "etat5",
     "du": "etat5",
     "ancêtre": "etat4",
     "oncle": "etat4",
     "mère": "etat3",
     "soeur": "etat3",
     "nièce": "etat3",
     "père": "etat2",
     "frère": "etat2",
     "neveu": "etat2"
}

words = output.split()[::-1]
calls = []

for i in words[:-1]:
    calls.append(d[i])
calls.append("etat1")
"->".join(calls[::-1])
Liblor
  • 480
  • 5
  • 13