Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
agmmnn committed Sep 28, 2021
0 parents commit 3844238
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__/
.vscode
_other
build
dist
*.spec
*.manifest
*.egg-info
22 changes: 22 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# tdk-cli

<div align="center">
<a href="https://github.com/agmmnn/tdk-cli">
<img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/agmmnn/tdk-cli"></a>
<a href="https://pypi.org/project/tdk-cli/">
<img alt="PyPI" src="https://img.shields.io/pypi/v/tdk-cli"></a>

Command-line tool for [TDK](https://en.wikipedia.org/wiki/Turkish_Language_Association) Dictionary, [sozluk.gov.tr](https://sozluk.gov.tr/) with rich output.
</div>


```
pip install tdk-cli
```

```
tdk <word>
tdk <word> -p
# plain text output
```
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rich
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from setuptools import setup
import tdk_cli.__main__ as m

with open("README.md", "r") as fh:
long_description = fh.read()

with open("requirements.txt", "r") as file:
requires = [line.strip() for line in file.readlines()]

VERSION = m.__version__
DESCRIPTION = "Command-line tool for TDK Dictionary (sozluk.gov.tr) with rich output."

setup(
name="tdk-cli",
version=VERSION,
url="https://github.com/agmmnn/turengcli",
description=DESCRIPTION,
long_description=long_description,
long_description_content_type="text/markdown",
author="agmmnn",
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"Environment :: Console",
"Topic :: Utilities",
],
packages=["tdk_cli"],
install_requires=requires,
include_package_data=True,
package_data={"tdk_cli": ["tdk_cli/*"]},
python_requires=">=3.5",
entry_points={"console_scripts": ["tdk = tdk_cli.__main__:cli"]},
)
Empty file added tdk_cli/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions tdk_cli/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from .cli import TDKDict
import argparse

__version__ = "0.0.1"

# parse arguments
ap = argparse.ArgumentParser()
ap.add_argument(
"word",
type=str,
nargs="*",
help="<word>",
)
ap.add_argument(
"-p",
"--plain",
action="store_true",
default=False,
help="returns plain text output",
)
ap.add_argument(
"-f",
"--fuzzy",
action="store_true",
default=False,
help="returns fuzzy search results",
)
ap.add_argument("-v", "--version", action="version", version="%(prog)s v" + __version__)
args = ap.parse_args()


def cli():
word = " ".join(args.word)
if word == "":
print("Please enter a word.")
else:
if args.plain:
TDKDict(word).plain()
else:
TDKDict(word).rich()


if __name__ == "__main__":
cli()
80 changes: 80 additions & 0 deletions tdk_cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
from urllib.parse import quote
from urllib.request import urlopen
from json import loads

from rich import box
from rich.table import Table
from rich.console import Console
from rich import print


class TDKDict:
def __init__(self, word):
self.word = word
# gts, bati, tarama, derleme, atasozu, kilavuz, lehceler
url = "https://sozluk.gov.tr/gts?ara=" + quote(word)
self.data = loads(urlopen(url).read())
if "error" in self.data:
print("Error")
exit()

def rich(self):
data = self.data
word = self.word
table = Table(title=word + " - TDK", show_header=False, box=box.SQUARE)
for i in range(len(data)):
# lang
if data[i]["lisan"] != "":
table.add_row(data[i]["lisan"] + ":")
else:
# suffix
if data[i]["taki"] != None:
table.add_row(word + ", " + data[i]["taki"] + ":")
else:
table.add_row(word + ":")
for j in range(len(data[i]["anlamlarListe"])):
# meaning
table.add_row(
str(j + 1) + ". [cyan]" + data[i]["anlamlarListe"][j]["anlam"] + "."
)
# sample sentences
if "orneklerListe" in data[i]["anlamlarListe"][j] != "":
table.add_row(
" [grey62]“"
+ data[i]["anlamlarListe"][j]["orneklerListe"][0]["ornek"]
+ "."
+ "”"
)
# space after each item except last one
if len(data) > 1 and i != range(len(data))[1]:
table.add_row("")
print(table)

def plain(self):
data = self.data
word = self.word
for i in range(len(data)):
# lang
if data[i]["lisan"] != "":
print(data[i]["lisan"] + ":")
else:
# suffix
if data[i]["taki"] != None:
print(word + ", " + data[i]["taki"] + ":")
else:
print(word + ":")
for j in range(len(data[i]["anlamlarListe"])):
# meaning
print(str(j + 1) + ". " + data[i]["anlamlarListe"][j]["anlam"] + ".")
# sample sentences
if "orneklerListe" in data[i]["anlamlarListe"][j] != "":
print(
" “"
+ data[i]["anlamlarListe"][j]["orneklerListe"][0]["ornek"]
+ "."
+ "”"
)
# space after each item except last one
if len(data) > 1 and i != range(len(data))[1]:
print("")

0 comments on commit 3844238

Please sign in to comment.