Code Monkey home page Code Monkey logo

dict.cc.py's Introduction

dict.cc.py

dict.cc.py usage

Simple unofficial command line interface for dict.cc written in Python. It supports translations between the most common languages available on the website.

Installation

dict.cc.py works with Python 2 and Python 3 and is available on PyPi. All you have to do to install it is:

pip install dict.cc.py

Usage

It's super easy! Here's a quick example of using it to translate the word beer between english (en) and swedish (sv):

$ dict.cc.py en sv beer
Showing 2 of 2 result(s)

English                                                     Swedish
========                                                    =======
beer ...................................................... öl
beer glass ................................................ ölglas

You can also search for phrases by using quotation marks:

% dict.cc.py en de body
Showing 10 of 49 result(s)

Englisch                                                    Deutsch
=========                                                   =======
body ...................................................... Körper-
a'body [Scot.] [allbody] .................................. jedermann
body [also wine] .......................................... Körper [auch bei Wein]
body ...................................................... Leib
body [dead body ] ......................................... Leiche
body ...................................................... Karosserie
body [trunk] .............................................. Rumpf
body [main part] .......................................... Hauptteil
body ...................................................... Gehäuse
body ...................................................... Körperschaft
body [society, organisation for sth.] ..................... Gesellschaft [Organisation]

Available languages include: en, de, sv, pt, it, fr, ro, nl, no.

Usage as Code

>>> from dictcc import Dict
>>> translator = Dict()
>>> result = translator.translate("hello", from_language="en", to_language="de")
>>> result.translation_tuples[:2]
[('Hello !', 'Hallo!'), ('Hello !', 'Servus! [bayer.] [österr.]')]

License

Public domain.

dict.cc.py's People

Contributors

aaschmid avatar chw90 avatar h10r avatar leonklingele avatar mh21 avatar omargfh avatar pavlolysytsya avatar ploth avatar rbaron avatar thomas-mc-work avatar vlow avatar zaraken avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dict.cc.py's Issues

Documentation for usage as code

Would be great if the documentation would also include the usage as code not just command line. Is it intended to be used apart from command line?

Non-ascii characters don't work

See below.

% dict.cc.py en de verfügbar 
Traceback (most recent call last):
  File "/usr/bin/dict.cc.py", line 73, in <module>
    run()
  File "/usr/bin/dict.cc.py", line 54, in run
    args.output_language)
  File "/usr/lib/python3.5/site-packages/dictcc/dictcc.py", line 57, in translate
    response_body = cls._get_response(word, from_language, to_language)
  File "/usr/lib/python3.5/site-packages/dictcc/dictcc.py", line 73, in _get_response
    res = urllib2.urlopen(req).read()
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1141, in _send_request
    self.putrequest(method, url, **skips)
  File "/usr/lib/python3.5/http/client.py", line 983, in putrequest
    self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 12: ordinal not in range(128)

SSLError

Installed from pip and it is up to date.
dict.cc.py de en test results in

Traceback (most recent call last):
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 594, in urlopen
    self._prepare_proxy(conn)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy
    conn.connect()
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/connection.py", line 344, in connect
    ssl_context=context)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/util/ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 817, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='deen.dict.cc', port=443): Max retries exceeded with url: /?s=test (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tlothpa/venv/dictcc/bin/dict.cc.py", line 82, in <module>
    run()
  File "/home/tlothpa/venv/dictcc/bin/dict.cc.py", line 63, in run
    args.output_language)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/dictcc/dictcc.py", line 51, in translate
    response_body = cls._get_response(word, from_language, to_language)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/dictcc/dictcc.py", line 61, in _get_response
    headers={'User-agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0'}
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='deen.dict.cc', port=443): Max retries exceeded with url: /?s=test (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)'),))

Unicode error on umlauts since colorized output

This is the command line and exception:

dict.cc.py de en körper
Showing 20 of 49 result(s)

Englisch                                                    Deutsch 
========                                                    ========
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/dict.cc/dict.cc.py", line 129, in <module>
    run()
  File "/usr/lib/python3.6/site-packages/dict.cc/dict.cc.py", line 123, in run
    print_translation(input_word, output_word, args.color, args.word)
  File "/usr/lib/python3.6/site-packages/dict.cc/dict.cc.py", line 99, in print_translation
    print(u"{} {} = {}".format(apply_color(input_word),
  File "/usr/lib/python3.6/site-packages/dict.cc/dict.cc.py", line 90, in apply_color
    string = re.sub(r"\b({})\b".format(re.escape(wrd)),
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 2: ordinal not in range(128)

Commenting out loop beginning in scripts/dict.cc.py:89 solves the issue ...

Env: Python 2.7.17 and Python 3.8.0

More robust HTML parsing

The way the HTML is parsed is very hacky and very fragile to changes in the results page. I think we would benefit from a more robust strategy for looking for the relevant tags.

The parsing is currently done here.

Can you explain in more detail, how to install and use this on OS X?

Hi @rbaron
I tried to get this to run on OS X, but without any success ("pip" not available). Can you explain in more detail, what is required and how to setup the application to run this on OS X (I think it's most likely very similiar to Linux anyway)?

Additionally, I feel like one needs to obtain some file(s) from dict.cc - like the translation database(s) - additionally to the application itself. Can this be? If yes, where and which files do I need?

Thanks & best regards,
Oliver

Invalid syntax on python 3.5 when installed via pip

That is a nice script - thanks for that!

I installed it via pip and wanted to test it on my windows machine by just call "dict.cc.py" in console window.
It throws the following error:

C:\Whatever>dict.cc.py
  File "C:\...\python_3.5.0\Scripts\dict.cc.py", line 31
    print "{}{}{}\n{}{}{}".format(
                         ^
SyntaxError: invalid syntax

So I checked line 31 of the dict.cc.py-file in my Scripts folder and got confused because of the difference between this line and the one which is online here on github:

Installed via pip: print "{}{}{}\n{}{}{}".format(

Github (line 32): print(u"{}{}{}\n{}{}{}".format(

Is the pip package not up2date?

HTTP Error 403: Forbidden

Am I the only one? Since today it raises the following error:

Traceback (most recent call last):
  File "/home/tlothpa/venv/dictcc/bin/dict.cc.py", line 81, in <module>
    run()
  File "/home/tlothpa/venv/dictcc/bin/dict.cc.py", line 62, in run
    args.output_language)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/dictcc/dictcc.py", line 59, in translate
    response_body = cls._get_response(word, from_language, to_language)
  File "/home/tlothpa/venv/dictcc/lib/python3.6/site-packages/dictcc/dictcc.py", line 76, in _get_response
    res = urllib2.urlopen(req).read()
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

unable to store the result in a bash variable

I would like to store the result in a variable before doing fourther procseeing like this:

TRANSLATION=$(dict.cc.py en de hello)

When I execute this then I get a python error:

 Traceback (most recent call last):
   File "/usr/local/bin/dict.cc.py", line 73, in <module>
     run()
   File "/usr/local/bin/dict.cc.py", line 67, in run
     print_translation(input_word, output_word)
   File "/usr/local/bin/dict.cc.py", line 45, in print_translation
     output_word))
 UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 62: ordinal not in range(128)

Configurable default languages

It would be great if we could avoid having to type the source and target language all the time. Instead of doing:

$ dict.cc.py en de body

We could let users configure which languages they would like to be the default, so they could avoid having to type en de all the time (although they can type it if they want non-default options):

$ dict.cc.py body

Two ideas for handling this config:

  • Config file in the user's home dir (e.g.: ~/.dict.cc.py.yaml)
  • Environment variables (e.g.: DICT_CC_PY_LANGS="en de")

I tend to favor the first approach, but at this complexity level it's a matter of personal taste 🙂

python warning

Hi,

I really like your work! I get the following warning on my environment, I have python 2.7 and python 3.5 installed but using python 2.7 for the translations scripts:

% /usr/lib/python2.7/site-packages/dict.cc/dict.cc.py en de test
/usr/lib/python2.7/site-packages/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")

  markup_type=markup_type))
Showing 10 of 49 result(s)

Englisch                                                    Deutsch
========                                                    =======
to test.....................................................testen
to test sth.................................................etw. prüfen
to test.....................................................ausprobieren
to test.....................................................untersuchen
to test.....................................................austesten
to test.....................................................erproben
to test sth.................................................etw. probieren
to test.....................................................kontrollieren
to test sb..................................................jdn. auf die Probe stellen
to test sth.................................................etw. überprüfen
to trial....................................................einem Test unterziehen

Show suggestions

There's one quite useful feature of the web interface that would be nice to have in this tool: Suggestions. For example, when I search for apathic, it suggests pathic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.