Skip to content

Support for automated text translation. - #33

Closed
Stefan311 wants to merge 5 commits into
pmrowla:masterfrom
Stefan311:master
Closed

Stefan311 wants to merge 5 commits into
pmrowla:masterfrom
Stefan311:master

Conversation

@Stefan311

Copy link
Copy Markdown
Contributor

lmlsb: added extracttext and inserttext subcommands for line-wise text operations.
lmgoogle: tool for automated translation, using google translate. This tool needs your additional work for proper build system integration.

Stefan311 and others added 4 commits April 30, 2020 09:18
This is curently a standalone python file, and needs integration into pylivemaker build environment.
The export/import file format is similar/compatible to the menu translation format.
@pmrowla

pmrowla commented Apr 30, 2020

Copy link
Copy Markdown
Owner

Changes are failing lint

@Stefan311 can you run

pre-commit install
pre-commit run --all-files

(once the pre-commit hook is installed, it'll run automatically whenever you try to commit changes in the future)

@pmrowla

pmrowla commented Apr 30, 2020 •

Copy link
Copy Markdown
Owner

I'll have to think about this one a bit, I'm hesitant to merge changes that can potentially lead to abusing undocumented API endpoints. I know the translate_a api is used for chromium, and a bunch of other projects still use it to get around google's paid translation API, but I'm not sure I want to merge it in pylm.

Maybe some separated behavior would be better, where pylm can read/write to csv's, and then the user can then translate the csv however they want. But we would not actually do the machine translate step in a pylm CLI tool.

@Stefan311

Copy link
Copy Markdown
Contributor Author

Changes are failing lint

@Stefan311 can you run

pre-commit install
pre-commit run --all-files

(once the pre-commit hook is installed, it'll run automatically whenever you try to commit changes in the future)

''stefan@stefan-P55A-UD4:~/pylivemaker$ pre-commit install
pre-commit: Befehl nicht gefunden.
''
I am still a python noob. Sorry. I also think this will be the last commit. I just have no more ideas ;)

@pmrowla

pmrowla commented Apr 30, 2020

Copy link
Copy Markdown
Owner

you might need to do
pip install -U -r requirements_dev.txt
first, the pre-commit/lint stuff is new

@Stefan311

Copy link
Copy Markdown
Contributor Author

I'll have to think about this one a bit, I'm hesitant to merge changes that can potentially lead to abusing undocumented API endpoints. I know the translate_a api is used for chromium, and a bunch of other projects still use it to get around google's paid translation API, but I'm not sure I want to merge it in pylm.

Maybe some separated behavior would be better, where pylm can read/write to csv's, and then the user can then translate the csv however they want. But we would not actually do the machine translate step in a pylm CLI tool.

Sure. You are free to take or to drop this. That's why this is a separate command, and separate commit.

@Stefan311

Copy link
Copy Markdown
Contributor Author

you might need to do
pip install -U -r requirements_dev.txt
first, the pre-commit/lint stuff is new

Works, fixed the hard errors, but does a terrible re-formatting. Is this really necessary?

@pmrowla
pmrowla self-requested a review April 30, 2020 10:26
Comment thread livemaker/cli/lmlsb.py
sys.exit('Could not generate new LSB file: {}'.format(e))


def scan_texts(lsb, csv_data, file_name, extract=False, patch=False):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is way too big and should be broken up into smaller chunks. Stuff can go into it's own function even if that function is only called once. Basically you should not have python functions where you need 10+ levels of indentation, because it becomes really hard to read even after just a few levels of indentation.

the main reason the reformatter makes this code look particularly bad is because of the excessive levels of indentation

@Stefan311 Stefan311 Apr 30, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way too big? Really? This function perfectly fits my old small 1280x1024 screen.
Split this function into more small functions just to avoid intention levels would make the readability way more worse.

  • you have to permanently jump with your eyes between the main function and the sub functions to understand what the whole construction is doing.
  • you have to transport all variables between the functions, who are very many in this case!
  • i do not know many about the internal python operations, but i guess placing subroutine calls into the inner loops will have a big negative performance impact.

I will not split this function. Take it as it is, rewrite it by yourself or drop the whole pull request.

@pmrowla pmrowla Apr 30, 2020 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I certainly do appreciate the PR, and don't mind cleaning it up myself.

but mainly my issue was just that python projects generally have a standard coding style (pep8), and nested indentations this deep are for the most part considered bad practice . calling it "too big" was probably not the best way to put it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, too many intentions are bad style. But things are need to be done. Adding stuff just to avoid intentions levels will make the whole thing more complicated. Keep things as simple as possible.

@pmrowla

pmrowla commented May 1, 2020 •

Copy link
Copy Markdown
Owner

Been thinking about this some more, I think it would be beneficial to move the logic for being able to access individual lines into the core pylivemaker API. Having programmatic API access to lines would obviously be pretty beneficial for pylm.

Basically, the TpWord/ scenario body logic from scan_text() can go into some new lm.lsb.novel class. This hypothetical ScenarioLines (or something with a better name) class would be a container that iterates over the lines in a scenario body. So it works sort of like LNSDecompiler, except it gives access to text lines instead of outputting everything from the body into an LNS script.

Then, people can do whatever they want w/those lines via the actual API.

So, your CSV CLI command could be simplified to something like

    # open csv file, prepare for reading/writing csv
    for scenario in lsb.text_scenarios():
        lines = ScenarioLines(scenario)
        for line in ScenarioLines(scenario):
            if extract:
                csv.append(line)
            if patch:
                line.replace(csv_line)
        scenario.body = lines.make_body()
    # save lsb

This would also open up a bunch of other possibilities, like being able to call some machine translation API directly from a python script and not even need the intermediate CSV file. You'd just do the same kind of thing as the above example, except

    def translate(line):
        # do machine translate translate things, whether its google trans or whatever other api you want

    for line in ScenarioLines(scenario):
        translated = translate(line)
        line.replace(translated)
    # save lsb

although given that machine translations need to be cleaned up this may not be a super realistic example? more likely you would still want to output your translated scenario, whether it's dumping to CSV, or dumping the translated scenario as a full LNS script, etc.

It would also make lmlsb dump --mode=lines more useful, since we could dump lines from this standard API, instead of the using the text_only mode for LNSDecompiler (which basically just strips HTML tags).

@pmrowla

pmrowla commented May 1, 2020

Copy link
Copy Markdown
Owner

I guess for now you can just leave this PR alone, and I'll start cherry-picking stuff to move the necessary logic into the API this weekend.

then once that is done we can either update the CLI things in this PR to use the new API, or just do a new PR entirely.

Fixes "Stuck Game" bug. I still wonder, how this bug passed the function test.
@Stefan311

Copy link
Copy Markdown
Contributor Author

@LioMajor

  1. You using Windows, I presume? I must confess I have not tested this on Windows, only on Linux (Mint distribution, german locale, no explicit japanese localisation). I have no idea what of the new parts of lmlsb requires full japanese localisation.
  2. This is exactly how the csv format works. See https://en.wikipedia.org/wiki/Comma-separated_values
  3. Damn. How could this bug pass the function test? Please try out the last patch.

Seems pmrowla thinks about turning this pull request all over. So it makes little sense to do long bug fixing.

@pmrowla pmrowla mentioned this pull request May 2, 2020
@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner

The reason tests didn't catch it is that there are pretty much no tests. The only commands that are actually tested right now are lmlsb probe and lmlsb validate to just make sure we can actually read & write LSB files.

In this case, we'd need to add tests that run extracttext, inserttext on some sample LSB and then validate the resulting file.

Comment thread livemaker/cli/lmlsb.py
Comment on lines +1292 to +1299
new_ch = TWdChar(
ch = ch,
decorator = last_ch.decorator,
text_speed = last_ch.text_speed,
link_name = last_ch.link_name,
link = last_ch.link,
condition = last_ch.condition,
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command probably needs some documented warning that we ill break cases where a game uses styling tags (decorators) or links/conditions. (but links/conditions aren't as important here since I think they are really only used in history/scrollback).

Ex:
Say you have a text line that looks like

<STYLE ID=1>おはよう</STYLE><STYLE ID=2>ございます</STYLE>

where style 1 is the default, and style 2 is text colored red.

the string is extracted as

おはようございます

but when we translate and re-insert it, it would end up looking like

<STYLE ID=2>Good morning</STYLE>

where the entire line is now red, since the decorator condition is coming from the last character in the line.

It's not a huge deal, but basically any one using this command/API needs to aware that if their game uses tags for more advanced formatting, they will still need to clean things up themselves using the full script format.

and in the event a game is actually using links/conditions for embedding icons/images into regular message boxes, it may break their game (until they fix the translated script).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this. The script does a line break on everything else than TWdChar.
As long links/conds/icons/images are not embedded into TWdChar they remain an the right places.
If they are embedded you are right.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the issue is that links/conditionals/decorators are flags tied to a TWdChar. when a decorator (font styling) changes between characters, the only difference is that the next TWdChar will have a new .decorator value (corresponding to some font style ID). So there's no non-TWdChar object between the different characters.

if you look at what the script decompiler does, it has to watch all of those flags for whenever they change, and then close any previously opened style/link/etc tag, and then open a tag for the new style/link/etc.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not a serious issue for a majority of LM games out there, but it's still something people should be aware of.

Comment thread livemaker/cli/lmlsb.py
Comment on lines +1260 to +1268
for cmd_nr, cmd in enumerate(lsb.commands):
if cmd.type == CommandType.TextIns:
scenario = cmd.get('Text')
text_line = None
block_nr = 0
new_body = []
last_ch = None
cmd_patched = False
for w in scenario.body:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for future reference, there's an api function for iterating over scripts in an LSB, which could save a few levels of indentation here

https://pylivemaker.readthedocs.io/en/latest/livemaker.lsb.html#livemaker.lsb.lmscript.LMScript.text_scenarios

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for hint to the API documentation. Stupid me have used the source until now...
Python is such a terrible language for analysing foreign code. The more i work with it the more i dislike python.

@Stefan311

Copy link
Copy Markdown
Contributor Author

By "test" i mean translating an playing a game. I have done this, but played only to the first few lines, who where ok. Yesterday i played a bit further and the test game got stuck too.

scenario.body should not be writeable from outside the class, since scenario.replace_body(new_body) seems required for proper work.

@pmrowla

pmrowla commented May 2, 2020

Copy link
Copy Markdown
Owner

scenario.body should not be writeable from outside the class, since scenario.replace_body(new_body) seems required for proper work.

yeah, you should not be messing with scenario.body directly unless you are sure you know what you are doing, since actually compiling it into an lsb file requires updating character counts and some other references that go into the compiled lsb.

@pmrowla

pmrowla commented May 4, 2020

Copy link
Copy Markdown
Owner

closed in favor of #37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants