The code and execution results are available on GitHub.
Parsing Character Lines
Since Java is my daily bread and butter, even though Python is great, I chose Java. After all, preparing the fine-tuning data JSON is a one-off task that isn’t part of the model training itself, so it’s fine to just use the tool I’m most comfortable with to parse and build it.
First, define two core data classes. The first is for character lines, where each object stores the character’s name and their corresponding speech or thought.
The first step of parsing is naturally reading the file, parsing line by line, trimming leading and trailing whitespaces, and trying to print the output. Thanks to the Galgame script format, lines are mostly on a single line and use “【XXX】” to indicate who is speaking.
Character list, all old friends, so nostalgic
Since some characters in the text are not enclosed in “【】” but appear at the front of a sentence, we first extract a list of character names and then perform matching to improve accuracy.
Because character text enclosed in “【】” appears near the top of the file, we can simply use a Set without needing to read through the file again.
Results of parsing characters not enclosed in brackets
Next, use inQuote to identify whether the current line belongs to a character’s speech, plus check whether the end of the line is a closing symbol to determine if there are multi-line speeches.
Capable of identifying multi-line speech
At this point, Parser.java is complete—reading the script, parsing character lines, and placing them into an object list.
public List<CharacterQuote> parseLines()throws IOException { List<CharacterQuote> parseResult = new ArrayList<>(); Set<String> characters = new HashSet<>();