Tool: Converting normal story text into renpy text.

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
ippherita
Regular
Posts: 26
Joined: Fri May 09, 2014 1:46 pm
Contact:

Tool: Converting normal story text into renpy text.

#1 Post by ippherita » Tue Jul 02, 2019 10:19 am

I posted at the creator's corner, and verysunshine suggested me to put it here. so here goes.

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 (Chen Wei Neng) 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.
her phone screen now showing video "Ah, found it." Jesse said excitedly
'''


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]

#BUG!
#inserting the {color = [tcw]} thing into the sentence according to the location just found
#for i,j in var:
#  sep_para2[i].insert(j, '{color = [tcw]}')
#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
#for i,j in var2_2:
#  sep_para2[i].insert(j, '{/color}')
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

#deleting extra stuff in front and behind
sep_para5 = sep_para4[6:-3]

#joining the list and print out
sep_para6 = ''.join(sep_para5)
print(sep_para6)

INSTRUCTION:
just paste this code in any 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.(maybe it is my mistake.)

IMPROVEMENT:
I was thinking to put this code onto a website and make it easier for everybody to use it. but i have no idea how to run this code on a website or where i create a website for free AND run this code. Let me know if anybody have any idea.

User avatar
ComputerArt.Club
Veteran
Posts: 406
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Tool: Converting normal story text into renpy text.

#2 Post by ComputerArt.Club » Tue Jul 02, 2019 11:15 am

Wow! Great work! This looks really useful! I have plenty of domains, but I just got rid of my hosting plans as I tend to use Blogger and Google Sites recently. I am sure someone here must have their own server or hosting and perhaps some idea of how to run your script on a website. Maybe someday I should try to make a little server at home too.

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

Re: Tool: Converting normal story text into renpy text.

#3 Post by ippherita » Wed Jul 03, 2019 3:53 pm

ComputerArt.Club wrote:
Tue Jul 02, 2019 11:15 am
Wow! Great work! This looks really useful! I have plenty of domains, but I just got rid of my hosting plans as I tend to use Blogger and Google Sites recently. I am sure someone here must have their own server or hosting and perhaps some idea of how to run your script on a website. Maybe someday I should try to make a little server at home too.
Haha, thank you, did some reading on hosting and stuff and realised it is a whole new beast. Here i thought it was a simple copy paste.

Well, it is easy to download python and run the code.

Post Reply

Who is online

Users browsing this forum: No registered users