Replace sections of text What is dialogue text

I am trying to rework a manuscript (160 pages) and replace every line that is a dialogue (starts with “-: Dialog/Text”) with “Dialog/Text”. With PHP I could easily replace standard lines without any additional description (verb and action). The challenge I face is when the dialogue line has additional descriptions that should not be quoted.

As I only code in PHP and Tinderbox I was thinking of giving the Tinderbox help forum a try. Maybe someone of you had a similar problem or there is a solution (loop sentence by sentence) in Tinderbox.

I would be grateful for any advice on how to approach this - to avoid having to do it by hand.

Here are some lines of code from the manuscript. Text in brackets should not be quoted.

Many thanks in advance for any help.

[As-is]
-: Liegt vor dem Haus, sagte er und zeigte auf die Straße.

[To-be]
»Liegt vor dem Haus«, sagte er und zeigte auf die Straße.

More Example, Text in brackets should not be quoted.
-: Liegt vor dem Haus(, sagte er und zeigte auf die Straße.)
-: Ich bin oben auf dem Gipfel(, sagte er.)
-: Du spinnst(, sagte Rico.) Wir sind immer noch in der Schule.
-: Es ist kalt(, sagte der Hausmeister.) Ich kann verstehen, dass du lieber im Bett liegen geblieben bist. Ich schließe dir auf, Junge.
-: Mirko, bleibst du bitte stehen(, sagte Frau Köhler, )bis wir einen Platz für euren neuen Mitschüler gefunden haben.
-: Es ist gut Mirko(, sagte die Lehrerin, )wir haben dich gehört.
-: Der Unterricht beginnt um halb acht(, sagte sie und stand auf.)
-: Sie heisst Berkehan(, sagte Frau Köhler.)
-: Ich wusste nicht(, sagte er, )dass wir in der ersten Stunde Sport haben.

Is this what you want?

[As-is]
 Dialog/Text: Liegt vor dem Haus, sagte er und zeigte auf die Straße.
[To-be]
 »Liegt vor dem Haus«, sagte er und zeigte auf die Straße.
More Example, Text in brackets should not be quoted.
 Dialog/Text: Liegt vor dem Haus(, sagte er und zeigte auf die Straße.)
 Dialog/Text: Ich bin oben auf dem Gipfel(, sagte er.)
 Dialog/Text: Du spinnst(, sagte Rico.) Wir sind immer noch in der Schule.
 Dialog/Text: Es ist kalt(, sagte der Hausmeister.) Ich kann verstehen, dass du lieber im Bett liegen geblieben bist. Ich schließe dir auf, Junge.
 Dialog/Text: Mirko, bleibst du bitte stehen(, sagte Frau Köhler, )bis wir einen Platz für euren neuen Mitschüler gefunden haben.
 Dialog/Text: Es ist gut Mirko(, sagte die Lehrerin, )wir haben dich gehört.
 Dialog/Text: Der Unterricht beginnt um halb acht(, sagte sie und stand auf.)
 Dialog/Text: Sie heisst Berkehan(, sagte Frau Köhler.)
 Dialog/Text: Ich wusste nicht(, sagte er, )dass wir in der ersten Stunde Sport haben.

Is so, 1) copy your text into a new note so that you have a back up, the 2) apply this stamp $Text=$Text.replace("-:","Dialog/Text:");.

Thanks Michel for your prompt reply. I Need to replace the first two chars and then insert - here is the thing - in between … depends on the dialog.

I have tried couple of regex and even uploaded the whole file to ChatGPT, but it dient work.

Here is a translation of a line, maybe this helps. What is Not spoken should not be quoted

[As-is]
-: Lies in front of the house, he said, pointing to the street.

[To-be]

“Lies in front of the house,” he said, pointing to the street.

Here we can consider ‘lines’ as paragraphs (as that’s how they get processed in most action code). But, how many discrete ( )-enclosed sections are there in a given line? Your examples suggest a maximum of one. If so, the task is quite simple. If not, then it’s a bit complex.

Also are the( ) the only use of these characters in the text, i.e. there are no literal parentheses used?

But the basic method might be:

  • read a line of text
  • remove the opening -: segment
  • capture the text to the first (. Enclosed the saved string in German-style quotes and save to a string variable.
  • Capture the text from the ( to the next ) and add it to the existing string variable.
  • If no more text, add a line break to the stored variable and read the next line, or capture the text to the first (., etc.
  • loop through until done.

Assumption: parentheses are only ever used to indicate text that is not to be placed in quotes. Otherwise the above method will fail.

I see; I did not correctly read your original post. I’m still not clear on the pattern. As @mwra suggests, line parsing would work really well in this case, but I’d/we’d need to get a better sense fo the pattern to help. DM me tomorrow. If I’m around we’ can hop on a Zoom call. That may be faster than the back and forth. We can then post the answer to the community.

is there a set pattern of “, he said”, “she said”, or some other delimiter we can key off of? Before you write a bunch of regex, you can use TBX highlighting to try to match the pattern and visually see how much of the text gets recognized.

AIUI the text to be changed does not have the brackets – is that right?

Your first example doesn’t have brackets, but your others do – it reads to me that you’ve added those to explain the problem to us, and they’re not in the original text.

If so, then is every single dialogue/text element followed by , sagt?

Then if so the following regex expression seems to do what your example suggests you want:

^-: (.*)(, sagte .*\.)(.*)

EDIT: forgot to put the replacement string in…

»\1«\2\3

The third capture group is there to deal with circumstances where the paragraph continues on after the end to the ‘sagt’ sentences. It may not actually be needed…

I tested this in BBEdit (because it has a pattern playground which makes this sort of thing a lot easier and got this result on the text you provided:

-: Liegt vor dem Haus, sagte er und zeigte auf die Straße.
-: Ich bin oben auf dem Gipfel, sagte er.
-: Du spinnst, sagte Rico. Wir sind immer noch in der Schule.
-: Es ist kalt, sagte der Hausmeister. Ich kann verstehen, dass du lieber im Bett liegen geblieben bist. Ich schließe dir auf, Junge.
-: Mirko, bleibst du bitte stehen, sagte Frau Köhler, bis wir einen Platz für euren neuen Mitschüler gefunden haben.
-: Es ist gut Mirko, sagte die Lehrerin, wir haben dich gehört.
-: Der Unterricht beginnt um halb acht, sagte sie und stand auf.
-: Sie heisst Berkehan, sagte Frau Köhler.
-: Ich wusste nicht, sagte er, dass wir in der ersten Stunde Sport haben.

»Liegt vor dem Haus«, sagte er und zeigte auf die Straße.
»Ich bin oben auf dem Gipfel«, sagte er.
»Du spinnst«, sagte Rico. Wir sind immer noch in der Schule.
»Es ist kalt«, sagte der Hausmeister. Ich kann verstehen, dass du lieber im Bett liegen geblieben bist. Ich schließe dir auf, Junge.
»Mirko, bleibst du bitte stehen«, sagte Frau Köhler, bis wir einen Platz für euren neuen Mitschüler gefunden haben.
»Es ist gut Mirko«, sagte die Lehrerin, wir haben dich gehört.
»Der Unterricht beginnt um halb acht«, sagte sie und stand auf.
»Sie heisst Berkehan«, sagte Frau Köhler.
»Ich wusste nicht«, sagte er, dass wir in der ersten Stunde Sport haben.

Is that what you’re after?

NB, naturally this won’t pick up paragraphs where you use a different speech marker from , sagte, so you’ll have to amend the regex accordingly to do that. (You could put the alternatives into the regex, but personally I think that’s getting complicated and I’d just run the regex a few times for different speech markers.

You’ll need to turn the regex(es) into Tinderbox’s version, of course, but that should be fairly easy – \1 becomes $1 and so on.

Have I understand what you want correctly? If not, please ignore my wibblings…

1 Like

I agree with others that this is fundamentally a regex challenge, and with @brookter that therefore you might want to do this in an app such as BBEdit with powerful regex preview and try-out support rather than TBX per se (though the latter would be entirely feasible once you’re confident you have the right regex “recipe”). As with any regex challenge you’re leveraging predictability and consistency so a lot is going to depend on the consistency of the punctuation in your source material.

A two-step solution might also hurt your brain less! @brookter has provided the first step. Now as I understand it you also want to “enquote” second clauses of direct speech, where present, between the first comma after “sagte” (and potentially also analogous verbs such as “flüsterte” or “schreie”) and the period at the end of the sentence, except (if I remember my German correctly) where the first word after the comma is “dass”, as I think in your final example (»Ich wusste nicht«, sagte er, dass wir in der ersten Stunde Sport haben) you don’t want wir…haben in quotes.

No doubt there will be a few edge cases that your regex didn’t fix: I suspect some careful proofreading at the end will still be required

2 Likes

Thank you all very much for all your response and hints. This is very helpful!!!

@brookter
That’s right, I was using parentheses to indicate the part of the line that is description. Your regex helped me to think in a different direction. The purpose of it is to change the dialogue style with brackets.

For documentation purpose. I am using the following approach:

Step 1: Change each line that is dialogue (-: )(.*)()
»$2«

Step 2: Insert for every keyword using a list of keywords
(, (sagte|fragte|überlegte)[^,.]*[,.])
« $& »

in an app such as BBEdit with powerful regex preview and try-out support rather than TBX per se

Good point about using BBedit. I was thinking of TBX because that’s what I’m used to and the text is 160 pages and has about 50k words.

No doubt there will be a few edge cases that your regex didn’t fix: I suspect some careful proofreading at the end will still be required

@Rob your are right, I want to fix all cases, careful proofreading at the end is crucial.

2 Likes