Resource: Translation from normal text into renpy text.

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
ippherita
Regular
Posts: 26
Joined: Fri May 09, 2014 1:46 pm
Contact:

Resource: Translation from normal text into renpy text.

#1 Post by ippherita » Sun Jun 30, 2019 11:24 am

Basically, this is a code to change this:

Code: Select all

label start:
	Jack walked into the office.
	"Morning." Jack greeted.
	Jesse looked up, "Oh Hi!"
	"Did you saw the news?" Jacked asked. "The one about the game?"
	Jesse shook her head. "Nope. let me google." She started typing on her phone.
	her phone screen now showing video "Ah, found it." Jesse said excitedly
into this:

Code: Select all

"Jack walked into the office."
"\"Morning.\"{color = [tcw]} Jack greeted.{/color}"
"{color = [tcw]}Jesse looked up, {/color}\"Oh Hi!\""
"\"Did you saw the news?\"{color = [tcw]} Jacked asked. {/color}\"The one about the game?\""
"{color = [tcw]}Jesse shook her head. {/color}\"Nope. let me google.\"{color = [tcw]} She started typing on her phone.{/color}"
"{color = [tcw]}her phone screen now showing video {/color}\"Ah, found it.\"{color = [tcw]} Jesse said excitedly{/color}"
Basically I have made a simple function/program to translate normal text into renpy text.
I am the type of person who don't like to write story and dialogs in renpy, i like to write stories in words or google words. It felt more natural.
But it was a hassle to copy and paste it into renpy,
need to change every " into \" in the dialogs,
then add a pair of "" at the start and end of every sentences.
to make matter worse. i wanted the part of the sentences that are not dialogs to appear WHITE.
Therefore i need to add a {color = [tcw]} {/color} in sentences that have dialogs and non-dialogs
(i have written tcw = #ffffff somewhere else in renpy, tcw meanign 'text color white', remember to do this in your renpy)
It was such a hassle and boring work that i decided to just write a code to help me automate.
after many googles and copy paste from stackoverflow, i think i got it. I am quite proud of this baby.
Just want to share it so other story writers like me don't need to go through the same hassle as I have.

Feel free to use, Just acknowledge me (ippherita) if you use this.

Code: Select all

paragraph = '''
Jack walked into the office.
"Morning." Jack greeted.
Jesse looked up, "Oh Hi!"
"Did you saw the news?" Jacked asked. "The one about the game?"
Jesse shook her head. "Nope. let me google." She started typing on her phone.
'''


import re


#splitting the paragraph into listed and nested list, basically separating the paragraph into sentences, and separate sentences into parts that with "" or without
sep_para2 = [re.split('(".*?")', x) for x in re.split('\n', paragraph)]

#delete some empty '' in the front or end of nested list that sometimes shows up
acc7 = 0
for x in sep_para2:
  for y in sep_para2[acc7]:
    if sep_para2[acc7][0] == '':
      sep_para2[acc7].pop(0)
  acc7 += 1

acc8 = 0
for x in sep_para2:
  for y in sep_para2[acc8]:
    if sep_para2[acc8][-1] == '':
      sep_para2[acc8].pop(-1)
  acc8 += 1

#find out the location of the part of sentence that don't have "" quotation marks (You can see that i basically cope paste this code from someone's stackoverflow)
var = [(index1,index2) for index1,value1 in enumerate(sep_para2) for index2,value2 in enumerate(value1) if '"' not in value2]

#inserting the {color = [tcw]} thing into the sentence according to the location just found
#added the extra now_i stuff is to recognize is we jump to next sentence, reset the acc to zero to move the {color} down the line
acc11 = 0
pre_i = 0
now_i = 0
for i,j in var:
  now_i = i
  if now_i > pre_i:
    acc11 = 0
  sep_para2[i].insert(j+acc11, '{color = [tcw]}')
  acc11 += 1
  pre_i = i

#again, find out the location of the part of sentence that don't have "" quotation marks and also don't have the {color = [tcw] i have inserted}
var2 = [(index1,index2) for index1,value1 in enumerate(sep_para2) for index2,value2 in enumerate(value1) if '"' not in value2 and 'tcw' not in value2]
var2_2 = [(x,y+1) for x,y in var2]

#inserting the {/color} thing into the sentence
acc12 = 0
pre_i2 = 0
now_i2 = 0
for i,j in var2_2:
  now_i2 = i
  if now_i2 > pre_i2:
    acc12 = 0
  sep_para2[i].insert(j+acc12, '{/color}')
  acc12 += 1
  pre_i2 = i

#joining the nested list, so now we only have a list of sentences
sep_para3 = [''.join(sentences) for sentences in sep_para2]

#deleting the extra {color} thing from sentences that don't have ""
a, b, c = '{color = [tcw]}', '', '{/color}'
for i, v in enumerate(sep_para3):
    if '"' not in v:
      sep_para3[i] = v.replace(a, b)
for i, v in enumerate(sep_para3):
    if '"' not in v:
      sep_para3[i] = v.replace(c, b)

#replacing " to \" so renpy can recognize
sep_para4 = [x.replace('"','\\"') for x in sep_para3]

#adding "" at both ends of the sentence, and adding the line break in the sentences
acc6 = 0
for x in range(len(sep_para4)):
  sep_para4[x+acc6:x+acc6] = ['"','\n','"']
  acc6+=3

#joining the list and print out, while adding the last " at the end
print(''.join(sep_para4)+'"')

INSTRUCTION:
just paste this code in python interpreter.
copy your normal story text into the space between ''' and ''' at the 'paragraph'
remember to delete any tabs if you have it. as it will add make the results weird.
run this code
and you will get the result as above.

remember to put below somewhere in your renpy, or any color you like in the non-dialog part of the sentences

Code: Select all

tcw = #ffffff
Or you can just delete the part of the code that insert the {color} thing if you dont use it.

after you pasted the result in renpy, remember to add the name of your character in front of the sentences that contain dialogs. so renpy can recognize it is a speech from your character

BUGS;
This code is not perfect. Review the results!
as you can see, at the last line of the story shows weird location of the {color} tags
this only happen to sentece structure that is:
[character do stuff "I am speaking." character do stuff.]
i think it has something to do when we added the {color} tag, the location changed and thus it inserted a bit earlier.
let me know if there are any fix to this problem.
(fixed)
I found sometimes the sentece structure ["i am speaking" character do stuff "I speak again"] also not translate properly (no {color} tag). dunno why.

verysunshine
Veteran
Posts: 280
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Resource: Translation from normal text into renpy text.

#2 Post by verysunshine » Mon Jul 01, 2019 6:47 pm

Hi, there! It seems you put this in the wrong section. This should be in the Ren'Py Cookbook area of the forum.

This will be very useful.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

ippherita
Regular
Posts: 26
Joined: Fri May 09, 2014 1:46 pm
Contact:

Re: Resource: Translation from normal text into renpy text.

#3 Post by ippherita » Tue Jul 02, 2019 9:33 am

verysunshine wrote:
Mon Jul 01, 2019 6:47 pm
Hi, there! It seems you put this in the wrong section. This should be in the Ren'Py Cookbook area of the forum.

This will be very useful.
Oh, verysunshine, thank you so much!

I DID not realised there is such an area on the forum.

copying this article there...

Post Reply

Who is online

Users browsing this forum: No registered users