ipython-contrib / jupyter_contrib_nbextensions

A collection of various notebook extensions for Jupyter
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest
Other
5.22k stars 806 forks source link

spellchecker - personal dictionary #1428

Open b-trav opened 5 years ago

b-trav commented 5 years ago

The spellchecker does not seem to have any easy way of incorporating personal dictionaries. Perhaps it is possible to use more than one dictionary, but I have not discovered how. If this is possible, please let me know.

Ideally, there would be a way of specifying a personal dictionary in the nbextension configuration page. As a work-around, I am currently using the following code:

import os.path
from jupyter_core.paths import jupyter_data_dir
from notebook.services.config import ConfigManager

def AddToDictionary(word):

    local_base_url = os.path.join(
        jupyter_data_dir(),
        'nbextensions', 'spellchecker', 'typo', 'dictionaries')

    lang_code = 'en_AU'
    personal_dic = 'personal.dic'
    mod_dic = lang_code + '_mod' + '.dic'

    if not os.path.exists(local_base_url):
        print('creating directory {!r}'.format(local_base_url))
        os.makedirs(os.path.realpath(local_base_url))

    #Add the word to the personal dictionary
    with open(local_base_url + '/' + personal_dic, 'a') as file:
        file.write(word + '\n')

    #Combine the personal and language dictionary
    with open(local_base_url + '/' + lang_code + ".dic", 'r') as file:
        old_lines = file.read()
    with open(local_base_url + '/' + personal_dic, 'r') as file:
        new_lines = file.read()
    with open(local_base_url + '/' + mod_dic, 'w') as file:
        file.write(old_lines)
        file.write(new_lines)

    #Make sure the spellchecker is using the modified dictionary
    cm = ConfigManager()
    rel_path = './typo/dictionaries/' + mod_dic
    cm.update('notebook', {'spellchecker': {'dic_url': rel_path}})