[SOLVED] Mirror punchline problem

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Mardhol
Newbie
Posts: 6
Joined: Tue Jul 30, 2019 7:59 am
Contact:

[SOLVED] Mirror punchline problem

#1 Post by Mardhol » Fri Mar 12, 2021 2:20 pm

Hi,
first of all, sorry for my english ; I speak french...

In order to make a "Parrot function" that mirrors what the main character (player) is saying, I use a function that get the chain string of the last player punchline and then I use the .replace function.
But I encounter a problem and don't know how to solve it.

Here's my function:
#######################################
def MirrorPunchline(self):
P.Punchline = P.Punchline.replace("I am", "You are") #Punchline is a parameter in wich I save successfully the last chain-string said by the Player.
P.Punchline = P.Punchline.replace("I'm", "you're")
P.Punchline = P.Punchline.replace("me ", "you ")
Parrot.Punchline = P.Punchline
renpy.say(Parrot, "{}".format(Parrot.Punchline))
########################################
When I call this function: $ P.MirrorPunchline()
It works perfectly and the parrot repeats what the player says like this :
"""
Player : "I am very dumb."
Parrot : "You are very dumb."
""""

But, here comes the problem : how to improve this function when the player use sentences like this :

"I am proud of me as you are proud of you" <---- in this case, my function will replace "I am" by "You are" and "me" by "you" but the rest is tricky.

If I complete the code of the function to replace "You are" by "I am" and "you" by "me", it will return:

"I am proud of me as I am proud of me".

How to get around this ?

Thanks a lot :)...
Last edited by Mardhol on Mon Mar 22, 2021 5:48 am, edited 1 time in total.

User avatar
_ticlock_
Veteran
Posts: 393
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Mirror punchline problem

#2 Post by _ticlock_ » Fri Mar 12, 2021 5:21 pm

Hi, Mardhol,

I would split the sentence into parts that you can then substitute. Here is a raw example:

Code: Select all

import re

subs = {
    'I': 'You',
    'am': 'are',
    'me': 'you',
    'you': 'I',
    'are': 'am'
    }
delimeters = subs.keys()

#pattern for re.split
pattern = '|'.join(delimeters)
pattern = r"({})".format(pattern)

sentence = 'I am proud of me as you are proud of you'

split_s = re.split(pattern, sentence)

new_sentence = []
for i in split_s:
    try:
        new_sentence.append(subs[i])
    except KeyError:
        new_sentence.append(i)
new_sentence = ''.join(new_sentence)
You will get "You are proud of you as I am proud of I"

You also need to take care of uppercase-lowercase changes. I guess the main problem is to distinguish how to replace pronouns with the right ones, since personal pronouns can be either the subject of a sentence or an object within a sentence For example, "you" should be correctly substituted to either "I" or "me".

Mardhol
Newbie
Posts: 6
Joined: Tue Jul 30, 2019 7:59 am
Contact:

Re: Mirror punchline problem

#3 Post by Mardhol » Sun Mar 14, 2021 11:05 am

Thank you for you reply. I think I can manage this with few exceptions.
I'll try your code asap.

Hopefully, I code a game in french and the problem is easier because the pronoun "you" is different when used as a subject or object. In french "you" as personnal pronoun = "Tu" and "you" as reflexive pronoun = "toi". So I don't need to make the code to recognise subject or object for now.
But, if I translate the project in English one day, the problem will come back :)...

Do you think this sample below will work ?

"you[rp]" : "me", <----- maybe I can define a empty string variable [rp] = " " for the "you" used as reflexive pronoun ? Does a dict key recognise variable?
"you" : "I",

And in every sentence I got a "you" as reflexive pronoun I will write it "you[rp]" but it the variable will be never screened because its empty.

User avatar
_ticlock_
Veteran
Posts: 393
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Mirror punchline problem

#4 Post by _ticlock_ » Sun Mar 14, 2021 12:44 pm

No, you can't use "you[rp]" : "me" in the dictionary. You probably could replace english "o" to a different language "o", that looks identical but have a different code, although I am not sure how it can help with subject-object recognition of the pronouns.

To do things right you likely need to analyze the sentence structure but that is totally more complex job.

Note: I just realized that the example I provided doesn't check if the key is part of a bigger word (for example "me" can be part of "come" ) or the whole word. To address this you need to wrap each key with "\w":

Code: Select all

pattern = "\w" + '\w|\w'.join(delimeters) + "\w"

Mardhol
Newbie
Posts: 6
Joined: Tue Jul 30, 2019 7:59 am
Contact:

Re: Mirror punchline problem

#5 Post by Mardhol » Mon Mar 22, 2021 5:46 am

_ticlock_ wrote:
Sun Mar 14, 2021 12:44 pm
No, you can't use "you[rp]" : "me" in the dictionary. You probably could replace english "o" to a different language "o", that looks identical but have a different code, although I am not sure how it can help with subject-object recognition of the pronouns.

To do things right you likely need to analyze the sentence structure but that is totally more complex job.

Note: I just realized that the example I provided doesn't check if the key is part of a bigger word (for example "me" can be part of "come" ) or the whole word. To address this you need to wrap each key with "\w":

Code: Select all

pattern = "\w" + '\w|\w'.join(delimeters) + "\w"
Many Thanks. I mark the post as solved !

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]