Page 1 of 1

[SOLVED] Mirror punchline problem

Posted: Fri Mar 12, 2021 2:20 pm
by Mardhol
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 :)...

Re: Mirror punchline problem

Posted: Fri Mar 12, 2021 5:21 pm
by _ticlock_
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".

Re: Mirror punchline problem

Posted: Sun Mar 14, 2021 11:05 am
by Mardhol
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.

Re: Mirror punchline problem

Posted: Sun Mar 14, 2021 12:44 pm
by _ticlock_
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"

Re: Mirror punchline problem

Posted: Mon Mar 22, 2021 5:46 am
by Mardhol
_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 !