mirror of
https://gitlab.com/gnh1201/pylingva.git
synced 2024-11-26 15:32:06 +00:00
pylingva
This commit is contained in:
commit
22b9c7f804
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/pylingva/__pycache__/*
|
||||
/build/*
|
||||
/dist/*
|
||||
/pylingva.egg-info/*
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2022 Nestero
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
53
README.md
Normal file
53
README.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Pylingva
|
||||
Simple translator using [Lingva Translate](https://lingva.ml/) API
|
||||
|
||||
# Install
|
||||
```
|
||||
pip install pylingva
|
||||
```
|
||||
|
||||
# Usage
|
||||
## CLI
|
||||
```shell
|
||||
$ translate -h
|
||||
usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll]
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
-s SOURCE, --source SOURCE
|
||||
Source Language to translate
|
||||
-t TARGET, --target TARGET
|
||||
Target Language to translate
|
||||
-txt TEXT, --text TEXT
|
||||
Text to translate
|
||||
-ll, --list-languages
|
||||
List Languages support
|
||||
|
||||
$ translate -s auto -t id -txt "How are You ?"
|
||||
Apa kabar ?
|
||||
```
|
||||
## Python
|
||||
### List languages
|
||||
```python
|
||||
from pylingva import pylingva
|
||||
trans = pylingva()
|
||||
list_languages = trans.languages()
|
||||
print(list_languages)
|
||||
```
|
||||
**Output:**
|
||||
```
|
||||
{'Detect': 'auto', 'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu', 'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chichewa': 'ny', 'Chinese': 'zh', 'Chinese (Traditional)': 'zh_HANT', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en', 'Esperanto': 'eo', 'Estonian': 'et', 'Filipino': 'tl', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl', 'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw', 'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id', 'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km', 'Kinyarwanda': 'rw', 'Korean': 'ko', 'Kurdish (Kurmanji)': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt', 'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi', 'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Odia (Oriya)': 'or', 'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru', 'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala': 'si', 'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv', 'Tajik': 'tg', 'Tamil': 'ta', 'Tatar': 'tt', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Turkmen': 'tk', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo', 'Zulu': 'zu'}
|
||||
```
|
||||
|
||||
### Translate
|
||||
```python
|
||||
from pylingva import pylingva
|
||||
trans = pylingva()
|
||||
txt = "Halo apa kabar ?"
|
||||
translate = trans.translate("auto", "en", txt)
|
||||
print(translate)
|
||||
```
|
||||
**Output**:
|
||||
```
|
||||
Hello, how are you ?
|
||||
```
|
2
pylingva/__init__.py
Normal file
2
pylingva/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
#! /usr/bin/env python
|
||||
from .pylingva import *
|
24
pylingva/cli.py
Normal file
24
pylingva/cli.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
#! /usr/bin/env python
|
||||
|
||||
from pylingva import pylingva
|
||||
import argparse
|
||||
|
||||
def translate():
|
||||
translate = pylingva()
|
||||
arg = argparse.ArgumentParser()
|
||||
arg.add_argument("-s", "--source", type=str, help="Source Language to translate")
|
||||
arg.add_argument("-t", "--target", type=str, help="Target Language to translate")
|
||||
arg.add_argument("-txt", "--text", type=str, help="Text to translate")
|
||||
arg.add_argument("-ll", "--list-languages", help="List Languages support", action="store_true")
|
||||
args = arg.parse_args()
|
||||
|
||||
if args.list_languages:
|
||||
lang = translate.languages()
|
||||
print("{:<25} {:<25}".format('Name', 'Code'))
|
||||
for key, value in lang.items():
|
||||
x = key
|
||||
y = value
|
||||
print("{:<25} {:<25}".format(x, y))
|
||||
else:
|
||||
result = translate.translate(args.source, args.target, args.text)
|
||||
print(result)
|
42
pylingva/pylingva.py
Normal file
42
pylingva/pylingva.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
#! /usr/bin/env python
|
||||
|
||||
import requests as req
|
||||
|
||||
class pylingva:
|
||||
|
||||
def __init__(self):
|
||||
self.url_list = ("https://lingva.ml", "https://translate.alefvanoon.xyz", "https://translate.igna.rocks", "https://lingva.pussthecat.org")
|
||||
for url in self.url_list:
|
||||
check_url = req.get(url)
|
||||
status = check_url.status_code
|
||||
if status == 200:
|
||||
self.url = url + "/api/v1/"
|
||||
break
|
||||
else:
|
||||
self.url = 0
|
||||
|
||||
def languages(self):
|
||||
if self.url == 0:
|
||||
print("Server Down !")
|
||||
else:
|
||||
url = self.url + "languages"
|
||||
all_languages = req.get(url)
|
||||
all_languages = all_languages.json()
|
||||
lang = {}
|
||||
for x in range(0, len(all_languages['languages'])):
|
||||
list_code = all_languages['languages'][x]['code']
|
||||
list_name = all_languages['languages'][x]['name']
|
||||
lang.update({list_name: list_code})
|
||||
return lang
|
||||
|
||||
def translate(self, source, target, text):
|
||||
if self.url == 0:
|
||||
print("Server Down !")
|
||||
else:
|
||||
url = f"{self.url}/{source}/{target}/{text}"
|
||||
url = url.replace("?", "%3F")
|
||||
r = req.get(url)
|
||||
r = r.json()
|
||||
result = r['translation']
|
||||
return result
|
||||
|
19
setup.py
Normal file
19
setup.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
#!/usr/bin/env python
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
with open("README.md", "r") as des:
|
||||
l_desc = des.read()
|
||||
|
||||
setup(
|
||||
name='pylingva',
|
||||
packages=find_packages(),
|
||||
version='0.1.0',
|
||||
entry_points={'console_scripts': ['translate = pylingva.cli:translate']},
|
||||
description='Simple translator using Lingva Translate API',
|
||||
url='https://gitlab.com/nesstero/pylingva',
|
||||
license='MIT',
|
||||
author='nestero',
|
||||
author_email='nestero@mail.com',
|
||||
long_description=l_desc,
|
||||
long_description_content_type='text/markdown',
|
||||
)
|
Loading…
Reference in New Issue
Block a user