Home > other >  Content spinning - Python
Content spinning - Python

Time:05-17

I have created an input that allows me to make a list of sentences (list) and an input that includes a dictionary with multiple values (dict). The elements of the list and the dictionary vary according to the elements entered by the user

I try to get the script to produce a list of sentences from List, by randomly picking from the data between {}

Do you have a solution using either random, itertool or re? I am completely blocked.

My informations :

List = ["Je {suis|m'appelle} kevin rab et je suis {grand|petit} et {beau|moche}.", "Je {suis|m'appelle} sam slap et je suis {grand|petit} et {beau|moche}.", "Je {suis|m'appelle} bob clob et je suis {grand|petit} et {beau|moche}.", "Je {suis|m'appelle} lydie bal et je suis {grand|petit} et {beau|moche}."] 


Dict =  {"{suis|m'appelle}": ['suis', 'mpo%appelle'], '{grand|petit}': ['grand', 'petit'], '{beau|moche}': ['beau', 'moche']}

I'm searching for a result like that (randomly) :

['Je mpo%appelle kevin rab et je suis grand et moche.', 'Je suis sam slap et je suis grand et beau.', 'Je mpo%appelle bob clob et je suis petit et moche.', 'Je suis lydie bal et je suis grand et moche.']

CodePudding user response:

You don't need the dict, you can just use re.sub with a callback function to look for those {...} regions and replace those with one of the alternatives:

>>> text = "Je {suis|m'appelle} kevin rab et je suis {grand|petit} et {beau|moche}."   
>>> re.sub(r"\{(. ?)\}", lambda m: random.choice(m.group(1).split("|")), text)         
'Je suis kevin rab et je suis petit et moche.'
>>> re.sub(r"\{(. ?)\}", lambda m: random.choice(m.group(1).split("|")), text)         
"Je m'appelle kevin rab et je suis petit et beau."

Relevant excerpt from the docs:

If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.

  • Related