GithubHelp home page GithubHelp logo

mhwgoo / cambridge Goto Github PK

View Code? Open in Web Editor NEW
50.0 3.0 6.0 20.46 MB

Terminal version of Cambridge Dictionary by default. Also supports Merrian-Webster Dictionary.

License: GNU General Public License v3.0

Python 99.09% Makefile 0.91%
python cambridge-dictionary dictionary merrian-webster web-scraping

cambridge's Introduction

Cambridge

cambridge is a terminal version of Cambridge Dictionary, with its data from https://dictionary.cambridge.org

By default, it is English to English translation. For English to Chinese, add '-c' or '--chinese' option.

Also supports the Merriam-Webster Dictionary. Webster has no foreign language translation in itself.

Screenshots

Look up Cambridge Dictionary (screenshot on DARK terminal background)

look up a word in Cambridge Dictionary

Look up Merriam-Webster Dictionary (screenshot on LIGHT terminal background)

look up a word in Merriam-Webster Dictionary

Why This

I'm a terminal person tired of pulling out a GUI app or browser, inputting words in the search bar, hitting the search button and then waiting for the result to render with a bunch of unnecessary static files coming along. Not only is the time taken long, but also switching apps back and forth can be annoying. So I wrote this console application with features to my satisfaction.

Highlights

  1. camb <word/phrase> to initiate the lookup in Cambridge Dictionary by default
  2. -w option to enable looking up in Merriam-Webster Dictionary
  3. less than 2s taken to do all the work for the word, including fetching, parsing, printing, and writing cache
  4. less than 0.1s for the same word's later search by retrieving cache
  5. only the first dictionary from Cambridge (assuming the optimal) to avoid being confused by multiple dictionaries
  6. a list of suggestions will be given, if not found
  7. camb l to list cached words and phrases
  8. support displaying spellcheck suggestion list, cache list, Webster's word of the day list by fzf
  9. if fzf not installed, no worries, printing of the aforementioned lists on terminal has been beautified
  10. well tuned to dark, light, blueish, grayish, gruvbox terminal colorschemes

Install & Uninstall

pip install cambridge
pip uninstall -r requirements.txt -y
rm -rf $HOME/.cache/cambridge
rm -rf $HOME/.cache/fakeua

# within the project
make install
make uninstall
make clean_cache

Usages

Command s (hidden)

For looking up a dictionary. Options can be put before or after <word/phrase>.

camb <word/phrase>      # look up a word/phrase in Cambridge Dictionary
camb <word/phrase> -w   # look up a word/phrase in Merriam-Webster Dictionary
camb <word/phrase> -c   # look up a word/phrase in Cambridge with Chinese translation

camb <phrase with "'" > # camb "a stone's throw" | camb a stone\'s throw
camb <phrase with "/" > # camb "have your/its moments" | camb have your\/its moments

camb <word/phrase> -d   # look up a word/phrase in debug mode
camb <word/phrase> -f   # look up a word/phrase afresh without using cache
camb <word/phrase> -n   # look up a word/phrase without showing suggestions if not found

Command l

For listing and deleting items in the cache.

camb l                  # list alphabetically ordered words/phrases you've found before
camb l -t               # list words/phrases in reverse chronological order
camb l -r               # list 20 words/phrases from the word list randomly
camb l -d               # delete one or more words/phrases(separated by ", ") from the list

Command wod

For displaying 'Word of the Day' in the Merriam Webster Dictionary

camb wod                # list today's Word of the Day from Merriam-Webster Dictionary
camb wod -l             # list all words of the day

General options

camb -h, --help         # show this help message and exit
camb -v, --version      # print the current version of the program

TO-DOS

  • input a new word/phrase when spelling suggestions are not satisfactory without restarting a new command line
  • check a particular expression against all cached sentence examples, if found, we can confidently use it like that in our speaking or writing
  • split and accumulate all sentences from a whole bulk of cache as independent Corpus, then we can use this Corpus outside this program
  • strengthen 'Word of the Day' functionality from Webster

cambridge's People

Contributors

mhwgoo avatar migcosta 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

Watchers

 avatar  avatar  avatar

cambridge's Issues

Webster doesn't work

camb -wf hello
2022-11-14 10:29:27 ERROR webster.py[152] The fetched result is not what we intended for the word due to the network or website reasons, please try again.

Crash

➜  camb bolster

bolster verb T
UK |ˈbəʊl.stər| US |ˈboʊl.stɚ|
: to support or improve something or make it stronger
|More money is needed to bolster the industry.
|She tried to bolster my confidence/morale (= encourage me and make me feel stronger) by telling me that I had a special talent.
|They need to do something to bolster their image.

bolster noun C
UK |ˈbəʊl.stər| US |ˈboʊl.stɚ|
: a long firm cylinder-shaped pillow

FOUND "bolster" from CAMBRIDGE in cache. You can add "-f -w" to fetch the MERRIAM_WEBSTER dictionary
Traceback (most recent call last):
  File "/home/USERNAME/.pyenv/versions/cambridge/bin/camb", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/USERNAME/.pyenv/versions/3.12.2/envs/cambridge/lib/python3.12/site-packages/cambridge/main.py", line 5, in main
    args.func(args)
  File "/home/USERNAME/.pyenv/versions/3.12.2/envs/cambridge/lib/python3.12/site-packages/cambridge/args.py", line 281, in search_word
    cambridge.search_cambridge(input_word, is_fresh, is_ch, no_suggestions, None)
  File "/home/USERNAME/.pyenv/versions/3.12.2/envs/cambridge/lib/python3.12/site-packages/cambridge/dicts/cambridge.py", line 38, in search_cambridge
    cached = dicts.cache_run(input_word, req_url)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/USERNAME/.pyenv/versions/3.12.2/envs/cambridge/lib/python3.12/site-packages/cambridge/dicts/dicts.py", line 58, in cache_run
    data = get_cache(input_word, req_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/USERNAME/.pyenv/versions/3.12.2/envs/cambridge/lib/python3.12/site-packages/cambridge/cache.py", line 45, in get_cache
    with con:
sqlite3.ProgrammingError: Cannot operate on a closed database.

Doesn't work

I have latest pypi version 3.5.9:

❯ camb remit
2022-12-12 22:45:49 ERROR user_agent.py[70] Nothing parsed out
2022-12-12 22:45:49 ERROR base_events.py[1747] Task exception was never retrieved
future: <Task finished name='Task-1' coro=<main() done, defined at /home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py:186> exception=SystemExit()>
Traceback (most recent call last):
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/main.py", line 17, in main
    args.func(args, con, cur)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/args.py", line 243, in search_word
    cambridge.search_cambridge(con, cur, input_word, is_fresh, is_ch)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/dicts/cambridge.py", line 40, in search_cambridge
    fresh_run(con, cur, req_url, input_word, is_ch)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/dicts/cambridge.py", line 75, in fresh_run
    result = fetch_cambridge(req_url, input_word, is_ch)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/dicts/cambridge.py", line 50, in fetch_cambridge
    res = dict.fetch(req_url, session)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/cambridge/dicts/dict.py", line 20, in fetch
    ua = user_agent()
  File "/home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py", line 250, in user_agent
    return asyncio.run(main(browser, use_cache))
  File "/usr/lib/python3.10/asyncio/runners.py", line 47, in run
    _cancel_all_tasks(loop)
  File "/usr/lib/python3.10/asyncio/runners.py", line 63, in _cancel_all_tasks
    loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True))
  File "/usr/lib/python3.10/asyncio/base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
    self._run_once()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1899, in _run_once
    handle._run()
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py", line 211, in main
    await asyncio.gather(*tasks)
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
    self._run_once()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1899, in _run_once
    handle._run()
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py", line 103, in write_to_dict
    versions = await parse(browser, session)
  File "/home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py", line 92, in parse
    attempt = call_on_error(ValueError("Nothing parsed out"), url, attempt, OP[1])
  File "/home/USERNAME/.local/lib/python3.10/site-packages/fake_user_agent/user_agent.py", line 71, in call_on_error
    sys.exit()
SystemExit

Cannot import fake user agent

[XXX@XXX Downloads]$ camb hello
Traceback (most recent call last):
  File "/home/XXX/.local/bin/camb", line 5, in <module>
    from cambridge.main import main
  File "/home/XXX/.local/lib/python3.10/site-packages/cambridge/main.py", line 16, in <module>
    from .args import parse_args
  File "/home/XXX/.local/lib/python3.10/site-packages/cambridge/args.py", line 15, in <module>
    from .dicts import webster, cambridge
  File "/home/XXX/.local/lib/python3.10/site-packages/cambridge/dicts/webster.py", line 12, in <module>
    from ..dicts import dict
  File "/home/XXX/.local/lib/python3.10/site-packages/cambridge/dicts/dict.py", line 6, in <module>
    from fake_user_agent import user_agent
ImportError: cannot import name 'user_agent' from 'fake_user_agent' (/home/XXX/.local/lib/python3.10/site-packages/fake_user_agent/__init__.py)

key error?

C:\Users\abc>camb commit
Traceback (most recent call last):
File "c:\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\python39\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "C:\Python39\Scripts\camb.exe_main
.py", line 4, in
File "c:\python39\lib\site-packages\cambridge\cambridge.py", line 512, in
from .log import logger
File "c:\python39\lib\site-packages\cambridge\log.py", line 11, in
data = Path(os.environ["HOME"]).absolute() / ".local" / "share" / "cambridge"
File "c:\python39\lib\os.py", line 679, in getitem
raise KeyError(key) from None
KeyError: 'HOME'

Fail in case of word not found

Current in case the word is not found, cambridge asks the user to select a similar word that is present in the dictionary.

I'm basic building a GUI around cambridge, and I need it to fail and return exit code != 0 in case the word is not found, without asking the user.

Otherwise my GUI will hang waiting for cambridge to exit.

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.