How to use the zdict.dictionary.DictBase function in zdict

To help you get started, we’ve selected a few zdict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zdict / zdict / zdict / dictionaries / yahoo.py View on Github external
from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError
from zdict.models import Record


def text(x):
    return x.text


# typing.Callable is new in Python 3.5,
# use type(lambda: None) for function type hints
def foreach(f: type(lambda: None), i: iter) -> None:
    deque(map(f, i), 0)


class YahooDict(DictBase):

    API = 'https://tw.dictionary.yahoo.com/dictionary?p={word}'

    @property
    def provider(self):
        return 'yahoo'

    @property
    def title(self):
        return 'Yahoo Dictionary'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)

    def show(self, record: Record):
        content = json.loads(record.content)
github zdict / zdict / zdict / dictionaries / oxford.py View on Github external
import json
import os
import re

from zdict.constants import BASE_DIR
from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError, QueryError, APIKeyError
from zdict.models import Record


class OxfordDictionary(DictBase):
    """
    Docs:

    * https://developer.oxforddictionaries.com/documentation/
    """

    KEY_FILE = os.path.join(BASE_DIR, 'oxford.key')

    API = 'https://od-api.oxforddictionaries.com/api/v1/entries/en/{word}'

    # https://developer.oxforddictionaries.com/documentation/response-codes
    status_code = {
        200: 'Success!',
        400: 'The request was invalid or cannot be otherwise served.',
        403: 'The request failed due to invalid credentials.',
        404: 'No entry is found.',
github zdict / zdict / zdict / loader.py View on Github external
def _is_dict(cls):
    try:
        return issubclass(cls, DictBase) and not (cls is DictBase)
    except Exception:
        return False
github zdict / zdict / zdict / dictionaries / urban.py View on Github external
import json

from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError
from zdict.models import Record


class UrbanDict(DictBase):

    API = 'http://api.urbandictionary.com/v0/define?term={word}'

    @property
    def provider(self):
        return 'urban'

    @property
    def title(self):
        return 'Urban Dictionary'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)

    def show(self, record: Record):
        content = json.loads(record.content)
github zdict / zdict / zdict / dictionaries / spanish.py View on Github external
from bs4 import BeautifulSoup

from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError
from zdict.models import Record


# [TODO]
#
# * let user choose en <-> spanish
# * some word's webpage use different CSS class ... (e.g. yo)
# * make code much more readable


class SpanishDict(DictBase):
    '''
    Tested words : ('soy', 'manzana', 'python', 'perdón')
    '''

    API = 'http://www.spanishdict.com/translate/{word}'

    @property
    def provider(self):
        return 'spanish'

    @property
    def title(self):
        return 'SpanishDict'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)
github zdict / zdict / zdict / dictionaries / wiktionary.py View on Github external
import json

from bs4 import BeautifulSoup

from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError, QueryError
from zdict.models import Record


class WiktionaryDict(DictBase):

    API = 'https://en.wiktionary.org/api/rest_v1/page/definition/{word}'

    @property
    def provider(self):
        return 'wiktionary'

    @property
    def title(self):
        return 'Wiktionary'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)

    def show(self, record: Record):
        content = json.loads(record.content)
github zdict / zdict / zdict / dictionaries / moe.py View on Github external
def clean(data, clean_cf=False):
    '''
    Clean the word segmentation

    remove "`~" and things in Unicode 'Cf' category
    '''
    data = data.translate(str.maketrans('', '', '`~'))
    if clean_cf:
        return remove_cf(data)
    else:
        return data


class MoeDictTaiwanese(DictBase):

    API = 'https://www.moedict.tw/t/{word}.json'

    @property
    def provider(self):
        return 'moe-taiwanese'

    @property
    def title(self):
        return '萌典(臺)'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)

    def show(self, record: Record):
        content = json.loads(record.content)
github zdict / zdict / zdict / dictionaries / jisho.py View on Github external
import json

from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError
from zdict.models import Record


class JishoDict(DictBase):

    # Change the url below to the API url of the new dictionary.
    # Need to keep the `{word}` for `_get_url()` usage.
    API = 'http://jisho.org/api/v1/search/words?keyword={word}'

    @property
    def provider(self):
        # Change `template` to the short name of the new dictionary.
        return 'jisho'

    @property
    def title(self):
        return 'Jisho'

    def _get_url(self, word) -> str:
        return self.API.format(word=word)
github zdict / zdict / zdict / dictionaries / yandex.py View on Github external
import json

from zdict.dictionary import DictBase
from zdict.exceptions import NotFoundError, QueryError
from zdict.models import Record


# The daily request limit is 1,000,000 characters
# The monthly limit is 10,000,000 characters
API_KEY = 'trnsl.1.1.20170826T075621Z.' \
          '6dfcaff242c6caa8.e2b9cf136d451d9d6eb69516ec97b827e8c8229b'


class YandexDict(DictBase):
    """
    Docs:

    * https://tech.yandex.com/translate/
    * https://tech.yandex.com/dictionary/

    """

    # Change the url below to the API url of the new dictionary.
    # Need to keep the `{word}` for `_get_url()` usage.
    # TODO: support different translate direction
    # TODO: use Dictionary API
    API = 'https://translate.yandex.net/api/v1.5/tr.json/translate?' \
          'key={api_key}&text={word}&lang=ru-en'

    status_code = {