Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Dwarf Fortress Translator (not ready yet) - Python  (Read 1210 times)

Grinso

  • Escaped Lunatic
    • View Profile
Dwarf Fortress Translator (not ready yet) - Python
« on: August 05, 2017, 04:09:55 pm »

So im trying to translate Dwarf Fortress but was not able to translate full sentences instead it translates all words written in small letters, word by word.
Maybe someone of you can help me.

//pip install yandex.translate
API Key https://tech.yandex.com/translate/

Quote
import re
import sys
from path import Path
from yandex_translate import *

yTranslate = YandexTranslate('API KEY')


def main(args):
    current_dir = Path(__file__).parent
    translation_dir = current_dir / "translation"
    if not translation_dir.exists():
        translation_dir.makedirs()

    for file in current_dir.files("*.txt"):
        with (translation_dir / file.name).open('w') as target_file:
            with file.open('r') as source_file:
                for i, line in enumerate(source_file):
                    line = line[:-1]
                    if i > 0:
                        for matcher in re.finditer(r"([a-zA-Z][a-z\- ,]+)", line):
                            if matcher:
                                if matcher.group(1) == 'kph': continue
                                transstring = matcher.group(1)
                                translation = yTranslate.translate(transstring, "en-de")
                                translation = translation["text"][0]
                                line = line.replace(transstring, translation)
                                print(transstring + " = " + translation)
                    line += '\n'
                    target_file.write(line)

if __name__ == '__main__':
    main(sys.argv)
« Last Edit: August 06, 2017, 07:42:00 am by Grinso »
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #1 on: August 05, 2017, 04:31:59 pm »

My proficiency with Python isn't great (gimme Perl, any day, much easier), but gut instinct suggests you haven't set case-insensitivity.

Unless I've missed it, you probably need to make sure that the re.I is set.  Or do whatever else you do with "re.finditer(r…)" to do what "m/…/ig"does in Perl.

(ETA: Or maybe I misunderstand what you're trying to do.  Second thoughts, actually putting my mind to the code, it looks more like it is grabbing each word, asking for a translate (to German), repeating with the next word.  Whatever that Yandex is, do you really mean to grab (translatable) words, grouped as sentences, then send the whole sentence to Yandex?  I think you need to explain your aim better. I may be the only one confused, of course.)
« Last Edit: August 05, 2017, 04:45:37 pm by Starver »
Logged

Grinso

  • Escaped Lunatic
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #2 on: August 05, 2017, 04:45:06 pm »

Yandex is the russian version of google (the google api is not for free), and yes i want to send the whole sentence to yandex and let it translate
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #3 on: August 05, 2017, 05:04:35 pm »

The following is Pseudocode, because the literal Perl I would write might not go so easily into your Python. Some work needed by you, anyway, but less than it could be. Assumes sentences end with "." and then whitespace (or EOF of some kind).
Code: [Select]
// Set up all you need to set up

Sentence="" // Blank starter sentence.
While (there.is.still.file)
  Grab = Find("<non-whitespace>+") // Greedy grab of one or more non-space character
  Sentence=Sentence+" "+Grab // (spot the inefficiency!)
  If Right(Grab,1) eq "." Then // Make that a *literal* . and not an "any character" .
              // (if you're doing this with regexp where it matters)
    Translate(Sentence) // Send, get, display and/or store; whatever you wish to put here.
    Sentence="" // Reblank sentence, ready to start fresh 'Grab'bing.
  EndIf
  If not(there.is.still.file) Then // Can be ORed onto the above If, just separating for convenience
    SentenceSentence+" "+Grab // (...)
    // Call the Translate as per the above If segment. If separate, can ignore the reblanking. But you'll be dropping out of the While loop anyway.
  EndIf
  Find("<whitespace>+") // Just to skip spaces, tabs, newlines, etc, ready for the next Grab=...
EndWhile
« Last Edit: August 05, 2017, 05:08:48 pm by Starver »
Logged

Grinso

  • Escaped Lunatic
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #4 on: August 06, 2017, 07:37:56 am »

ok i have done it buttttttt....

Its not as good as i hoped and its mostly because the of Yandex translator.

If some of you wants to try it i will edit the first post with the new sourcecode... u need to have a yandex account for the API Key https://tech.yandex.com/translate/
Logged

Grinso

  • Escaped Lunatic
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #6 on: August 07, 2017, 09:18:11 am »

The translator is not designed to translate fictional language into englisch it is for translating englisch into german (or another language that is supported by the yandex api)
Logged

Bearskie

  • Bay Watcher
  • Nyam nyam
    • View Profile
Re: Dwarf Fortress Translator (not ready yet) - Python
« Reply #7 on: August 07, 2017, 10:56:00 am »

Oh, sorry. My bad.