Skip to content

Instantly share code, notes, and snippets.

@phineas-pta
Last active June 18, 2023 19:55
Show Gist options
  • Save phineas-pta/457b9f546ec20d5d2019d5799847eb01 to your computer and use it in GitHub Desktop.
Save phineas-pta/457b9f546ec20d5d2019d5799847eb01 to your computer and use it in GitHub Desktop.
Sino-Vietnamese with thivien.net API

Chuyển đổi Hán-Việt với thivien.net API

bản gốc từ https://github.com/ds4v/NomNaSite/blob/main/handler/translator.py

giải thích API:

  • mode=trans chuyển đổi từ Hán tự
    • lang=1 → Hán-Việt
    • lang=2 → bính âm
    • lang=3 → chữ Nôm
  • mode=han chuyển đổi sang Hán tự
    • lang=1 từ Hán-Việt
    • lang=2 từ bính âm
    • lang=3 từ chữ Nôm
    • hanmode=trad dạng phồn thể
    • hanmode=trad dạng giản thể
  • mode=t2s chuyển đổi phồn thể → giản thể
  • mode=s2t chuyển đổi giản thể → phồn thể
import json, requests
payload = "mode=trans&lang=1&input=哀息之茧" # PUT WHAT YOU WANT
url = "https://hvdic.thivien.net/transcript-query.json.php"
headers = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
response = requests.request("POST", url, headers=headers, data=payload.encode())
result = json.loads(response.text)["result"]
" ".join([el["o"][0] for el in result])
# "ai tức chi kiển"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment