Skip to content

Conversation

FAReTek1
Copy link
Collaborator

@FAReTek1 FAReTek1 commented Nov 12, 2024

Used the scratch translation api to make a function in other_apis.py

Also added a supported languages list/dict in the utils folder
This uses the api at https://translate-service.scratch.mit.edu/translate?language={language}&text={text}


Also added a tts api in the same file using:
https://synthesis-service.scratch.mit.edu/synth?locale={language}&gender={gender[0]}&text={text}
Added relevant languages for tts to the languages file in the utils folder asw

(cherry picked from commit 6cb631f)
@TheCommCraft
Copy link
Collaborator

I recommend using an Enum for the languages

@FAReTek1
Copy link
Collaborator Author

true actually. but it will be little annoying to make a list of keys and values

(cherry picked from commit f3ce6a2)
@FAReTek1 FAReTek1 changed the title Added (smart) translation api Added (smart) translation + TTS api Nov 13, 2024
@FAReTek1
Copy link
Collaborator Author

I recommend using an Enum for the languages

I'll try to make an enum and then make functions/methods to get the data from those enums

@TheCommCraft
Copy link
Collaborator

TheCommCraft commented Nov 13, 2024 via email

@TheCommCraft
Copy link
Collaborator

TheCommCraft commented Nov 13, 2024 via email

@FAReTek1
Copy link
Collaborator Author

FAReTek1 commented Nov 15, 2024

yesnt. i kind of added one but its not very helpful for this use case since i have to search lists

having to use .value is also a bit annoying

@TheCommCraft
Copy link
Collaborator

You should make the function take an instance of the Enum and take the value out of it in the function

@TheCommCraft
Copy link
Collaborator

TheCommCraft commented Nov 15, 2024

That's what Enums are used for (for instance)

@FAReTek1
Copy link
Collaborator Author

FAReTek1 commented Nov 16, 2024

i added an enum (afaik its an enum) but it's not helpful for searching the list of languages. i might abstract the code a bit (making a language class) to clean it up a bit

@TheCommCraft
Copy link
Collaborator

TheCommCraft commented Nov 16, 2024 via email

@FAReTek1
Copy link
Collaborator Author

image
does this count as an enum??

@FAReTek1
Copy link
Collaborator Author

the problem is is what i really want is a list of keys and values, and those are easier to do with dictionaries

@FAReTek1
Copy link
Collaborator Author

ill make a languages class to abstract teh stuff a bit actually

@FAReTek1
Copy link
Collaborator Author

there:
langs = [Language('Albanian', 'sq'),
Language('Amharic', 'am'),
Language('Arabic', 'ar', ['ar'], 'arb', True),
Language('Armenian', 'hy'),
Language('Azerbaijani', 'az'),
Language('Basque', 'eu'),
Language('Belarusian', 'be'),
Language('Bulgarian', 'bg'),
Language('Catalan', 'ca'),
Language('Chinese (Traditional)', 'zh-tw'),
Language('Croatian', 'hr'),
Language('Czech', 'cs'),
Language('Danish', 'da', ['da'], 'da-DK', False),
Language('Dutch', 'nl', ['nl'], 'nl-NL', False),
Language('English', 'en', ['en'], 'en-US', False),
Language('Esperanto', 'eo'),
Language('Estonian', 'et'),
Language('Finnish', 'fi'),
Language('French', 'fr', ['fr'], 'fr-FR', False),
Language('Galician', 'gl'),
Language('German', 'de', ['de'], 'de-DE', False),
Language('Greek', 'el'),
Language('Haitian Creole', 'ht'),
Language('Hindi', 'hi', ['hi'], 'hi-IN', True),
Language('Hungarian', 'hu'),
Language('Icelandic', 'is', ['is'], 'is-IS', False),
Language('Indonesian', 'id'),
Language('Irish', 'ga'),
Language('Italian', 'it', ['it'], 'it-IT', False),
Language('Japanese', 'ja', ['ja', 'ja-hira'], 'ja-JP', False),
Language('Kannada', 'kn'),
Language('Korean', 'ko', ['ko'], 'ko-KR', True),
Language('Kurdish (Kurmanji)', 'ku'),
Language('Latin', 'la'),
Language('Latvian', 'lv'),
Language('Lithuanian', 'lt'),
Language('Macedonian', 'mk'),
Language('Malay', 'ms'),
Language('Malayalam', 'ml'),
Language('Maltese', 'mt'),
Language('Maori', 'mi'),
Language('Marathi', 'mr'),
Language('Mongolian', 'mn'),
Language('Myanmar (Burmese)', 'my'),
Language('Persian', 'fa'),
Language('Polish', 'pl', ['pl'], 'pl-PL', False),
Language('Portuguese', 'pt'),
Language('Romanian', 'ro', ['ro'], 'ro-RO', True),
Language('Russian', 'ru', ['ru'], 'ru-RU', False),
Language('Scots Gaelic', 'gd'),
Language('Serbian', 'sr'),
Language('Slovak', 'sk'),
Language('Slovenian', 'sl'),
Language('Spanish', 'es'),
Language('Swedish', 'sv', ['sv'], 'sv-SE', True),
Language('Telugu', 'te'),
Language('Thai', 'th'),
Language('Turkish', 'tr', ['tr'], 'tr-TR', True),
Language('Ukrainian', 'uk'),
Language('Uzbek', 'uz'),
Language('Vietnamese', 'vi'),
Language('Welsh', 'cy', ['cy'], 'cy-GB', True),
Language('Zulu', 'zu'),
Language('Hebrew', 'he'),
Language('Chinese (Simplified)', 'zh-cn'),
Language(None, None, ['zh-cn', 'zh-tw'], 'cmn-CN', True),
Language(None, None, ['nb', 'nn'], 'nb-NO', True),
Language(None, None, ['pt-br'], 'pt-BR', False),
Language(None, None, ['pt'], 'pt-PT', False),
Language(None, None, ['es'], 'es-ES', False),
Language(None, None, ['es-419'], 'es-US', False)]

@FAReTek1
Copy link
Collaborator Author

hmm. an enum would be nice tho for code completion

@FAReTek1
Copy link
Collaborator Author

easy to add

@FAReTek1
Copy link
Collaborator Author

image
is this good then

@FAReTek1
Copy link
Collaborator Author

do i have to inherit from enum? It feels like it would work nicer without it since then you don't need to do .value all the time

@FAReTek1
Copy link
Collaborator Author

oh, i just don't know enums well enough. there is actually an implementation for using for loops with it already

@FAReTek1
Copy link
Collaborator Author

image
yeah this find function should do the job

@FAReTek1
Copy link
Collaborator Author

now i will use these for tts and translate

@FAReTek1
Copy link
Collaborator Author

ok hopefully the new commit means its all good

@FAReTek1
Copy link
Collaborator Author

if this gets merged, I'll have to update the scratchblocks part of scralenium since that also has language data

@TheCommCraft
Copy link
Collaborator

You just need to do .value in the functions that use the enum instances

Copy link
Collaborator

@TheCommCraft TheCommCraft left a comment

Choose a reason for hiding this comment

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

Possibly change lines 152-156 to

if isinstance(language, str):
        if language.lower() in Languages.all_of("code", str.lower):
            lang = Languages.find(language.lower(), "code")
        elif language.lower() in Languages.all_of("name", str.lower):
            lang = Languages.find(language.lower(), apply_func=str.lower)
    elif isinstance(language, Languages):
        lang = language

@FAReTek1
Copy link
Collaborator Author

ill take a look tmrw

@FAReTek1
Copy link
Collaborator Author

ok. i will take a look now

@FAReTek1
Copy link
Collaborator Author

yes, i'm making a few changes to your addition but it works now with any of these settings:

import scratchattach as sa

print(sa.translate("hungarian", "radish"))
print(sa.translate(sa.Languages.Hungarian, "radish"))
print(sa.translate(sa.Languages.Hungarian.value, "radish"))
print(sa.translate("hu", "radish"))

@FAReTek1
Copy link
Collaborator Author

i will take a look at the dataclass stuff now

@FAReTek1
Copy link
Collaborator Author

dang, wow dataclasses makes the code a lot more compact, thanks for telling me about that

@FAReTek1
Copy link
Collaborator Author

its basically just a python struct

@FAReTek1
Copy link
Collaborator Author

Possibly change lines 152-156 to

if isinstance(language, str):
        if language.lower() in Languages.all_of("code", str.lower):
            lang = Languages.find(language.lower(), "code")
        elif language.lower() in Languages.all_of("name", str.lower):
            lang = Languages.find(language.lower(), apply_func=str.lower)
    elif isinstance(language, Languages):
        lang = language

i added that now but im not sure how i tell github that i did

Copy link
Collaborator

@TheCommCraft TheCommCraft left a comment

Choose a reason for hiding this comment

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

Seems good now

@FAReTek1
Copy link
Collaborator Author

the cmn_CN's attributes should be added to chinese_simplified and chinese_traditional since it is just the tts version of Chinese which applies to both simplified and traditional
likewise, pt_pt could be merged with Portuguese and es_es with Spanish

@FAReTek1
Copy link
Collaborator Author

added now

@FAReTek1
Copy link
Collaborator Author

also should probably add better error handling - maybe even add more to exceptions.py

@FAReTek1
Copy link
Collaborator Author

maybe i should make an enum for supported tts genders

@FAReTek1
Copy link
Collaborator Author

maybe change supportedlangs file into utils.enums or something

@FAReTek1
Copy link
Collaborator Author

so instead of making a new file for each enum, just put those kinds of things there

@FAReTek1
Copy link
Collaborator Author

ill turn those extra functions (find and all_of) into something that comes under an enumwrapper class

@TheCommCraft
Copy link
Collaborator

Are you done with the pull request?

@FAReTek1
Copy link
Collaborator Author

FAReTek1 commented Dec 2, 2024

yeah i think so

@FAReTek1
Copy link
Collaborator Author

(yes, pls merge 🥺)

@TimMcCool TimMcCool merged commit 1aa7bca into TimMcCool:main Dec 20, 2024
@TheCommCraft
Copy link
Collaborator

yeah i think so

Sorry I didn't see the message

@FAReTek1 FAReTek1 deleted the translate branch December 21, 2024 11:42
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.

3 participants