Skip to content

Commit

Permalink
Fix translation after papago was discontinued
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed May 30, 2024
1 parent ff2fee0 commit 1924034
Showing 1 changed file with 44 additions and 84 deletions.
128 changes: 44 additions & 84 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,6 @@
from modules.shazam import Shazam
from modules.ui import BaseButtonPaginator, Compliance

papago_pairs = [
"ko/en",
"ko/ja",
"ko/zh-cn",
"ko/zh-tw",
"ko/vi",
"ko/id",
"ko/de",
"ko/ru",
"ko/es",
"ko/it",
"ko/fr",
"en/ja",
"ja/zh-cn",
"ja/zh-tw",
"zh-cn/zh-tw",
"en/ko",
"ja/ko",
"zh-cn/ko",
"zh-tw/ko",
"vi/ko",
"id/ko",
"th/ko",
"de/ko",
"ru/ko",
"es/ko",
"it/ko",
"fr/ko",
"ja/en",
"zh-cn/ja",
"zh-tw/ja",
"zh-tw/zh-tw",
]


class GifOptions(util.KeywordArguments):
def __init__(self, start: float | None = None, end: float | None = None):
Expand Down Expand Up @@ -729,6 +695,8 @@ async def translate(self, ctx: commands.Context, *, text):
You can specify language pairs or let them be automatically detected.
Default target language is english.
You can also use '->' in place of '/' for language specification.
Usage:
>translate <sentence>
>translate xx/yy <sentence>
Expand All @@ -737,66 +705,58 @@ async def translate(self, ctx: commands.Context, *, text):
"""
if len(text) > 1000:
raise exceptions.CommandWarning(
"Sorry, the maximum length of text i can translate is 1000 characters!"
"The maximum length of text i can translate is 1000 characters!"
)

source = ""
target = ""
languages = text.partition(" ")[0]
if "/" in languages or "->" in languages:
source, target = (
languages.split("/") if "/" in languages else languages.split("->")
)
text = text.partition(" ")[2]
if source == "":
source = await detect_language(self.bot, text)
if target == "":
target = "en"
languages, text = text.split(" ", 1)

for separator in ["/", "->"]:
if separator in languages:
source, target = languages.split(separator)
if source or target:
break
else:
source = await detect_language(self.bot, text)
target = "ko" if source == "en" else "en"
language_pair = f"{source}/{target}"

# we have language and query, now choose the appropriate translator

if language_pair in papago_pairs:
# use papago
url = "https://openapi.naver.com/v1/papago/n2mt"
params = {"source": source, "target": target, "text": text}
headers = {
"X-Naver-Client-Id": self.bot.keychain.NAVER_APPID,
"X-Naver-Client-Secret": self.bot.keychain.NAVER_TOKEN,
}
# nothing was found, reconstruct the full text
text = languages + " " + text

async with self.bot.session.post(
url, headers=headers, data=params
) as response:
translation = (await response.json(loads=orjson.loads))["message"][
"result"
]["translatedText"]
# default target to english
if not target:
target = "en"

else:
# use google
url = "https://translation.googleapis.com/language/translate/v2"
params = {
"key": self.bot.keychain.GCS_DEVELOPER_KEY,
"model": "nmt",
"target": target,
"source": source,
"q": text,
}
if source == target:
raise exceptions.CommandInfo(
f"Nothing to translate! Source and target languages match ({source})"
)

url = "https://translation.googleapis.com/language/translate/v2"
params = {
"key": self.bot.keychain.GCS_DEVELOPER_KEY,
"target": target,
"q": text,
}
if source:
params["source"] = source

async with self.bot.session.get(url, params=params) as response:
data = await response.json(loads=orjson.loads)
async with self.bot.session.get(url, params=params) as response:
data = await response.json(loads=orjson.loads)

try:
translation = html.unescape(
data["data"]["translations"][0]["translatedText"]
)
except KeyError:
return await ctx.send("Sorry, I could not translate this :(")
# check for errors and raise if any are present
error = data.get("error")
if error:
logger.error(error)
raise exceptions.CommandError("Error: " + error["message"])

result = data["data"]["translations"][0]

# get the detected language if one was not supplied
if not source:
source = result["detectedSourceLanguage"]

translation = html.unescape(result["translatedText"])

await ctx.send(f"`{source}->{target}` {translation}")
await ctx.send(f"`{source}->{target}`\n>>> {translation}")

@commands.command(aliases=["wolf", "w"])
async def wolfram(self, ctx: commands.Context, *, query):
Expand Down

0 comments on commit 1924034

Please sign in to comment.