def setDictionary(self):        
     import dictLangDialog
     dialog = dictLangDialog.DictLangDialog()
     try:
         dialog.show()
         lang = dialog.selectedLang
         if lang == "United States" and enchant.dict_exists("en_US"):
             self.wordlist = enchant.request_dict("en_US")
         elif lang == "Chinese" and enchant.dict_exists("zh"):
             self.wordlist = enchant.request_dict("zh")
         elif lang == "Russian" and enchant.dict_exists("ru"):
             self.wordlist = enchant.request_dict("ru")
         elif lang == "German" and enchant.dict_exists("de_DE"):
             self.wordlist = enchant.request_dict("de_DE")
         elif lang == "French" and enchant.dict_exists("fr_FR"):
             self.wordlist = enchant.request_dict("fr_FR")
         elif lang == "Norwegian" and enchant.dict_exists("no"):
             self.wordlist = enchant.request_dict("no")
         elif lang == "Zulu" and enchant.dict_exists("zu"):
             self.wordlist = enchant.request_dict("zu")
         elif lang == "Arabic" and enchant.dict_exists("ar"):
             self.wordlist = enchant.request_dict("ar")
         elif lang == "Hindi" and enchant.dict_exists("hi"):
             self.wordlist = enchant.request_dict("hi")
         elif lang == "British" and enchant.dict_exists("en_GB"):
             self.wordlist = enchant.request_dict("en_GB")
     except:
         self.wordlist = enchant.request_dict("en_GB")
Exemplo n.º 2
0
    def toggleSpellcheck(self, v):
        self.spellcheck = v
        if enchant and self.spellcheck and not self._dict:
            if self.currentDict and enchant.dict_exists(self.currentDict):
                self._dict = enchant.Dict(self.currentDict)
            elif enchant.get_default_language() and enchant.dict_exists(enchant.get_default_language()):
                self._dict = enchant.Dict(enchant.get_default_language())
            else:
                self.spellcheck = False

        if self.highlighter:
            self.highlighter.rehighlight()
        else:
            self.spellcheck = False
Exemplo n.º 3
0
	def _initialize(self):
		"""
		@raise ImportError: if pyenchant is not installed
		@raise enchant.Error: if no default dictionary exists
		"""

		# lazy import for catching import exceptions on missing Enchant
		import enchant
		import enchant.checker
		import enchant.tokenize

		# select a dictionary
		language = str(self._preferences.get("SpellCheckDictionary", ""))
		if len(language) > 0 and enchant.dict_exists(language):
			self._dictionary = enchant.Dict(language)
		else:
			# try to use default language (may raise enchant.Error)
			self._dictionary = enchant.Dict()
			language = self._dictionary.tag
		self._log.debug("Using dictionary '%s'" % language)

		# create a spellchecker that is aware of email addresses and URLs
		self._checker = enchant.checker.SpellChecker(language, filters=[enchant.tokenize.EmailFilter, enchant.tokenize.URLFilter])

		self._initialized = True
Exemplo n.º 4
0
    def _initialize(self):
        """
		@raise ImportError: if pyenchant is not installed
		@raise enchant.Error: if no default dictionary exists
		"""

        # lazy import for catching import exceptions on missing Enchant
        import enchant
        import enchant.checker
        import enchant.tokenize

        # select a dictionary
        language = str(self._preferences.get("SpellCheckDictionary", ""))
        if len(language) > 0 and enchant.dict_exists(language):
            self._dictionary = enchant.Dict(language)
        else:
            # try to use default language (may raise enchant.Error)
            self._dictionary = enchant.Dict()
            language = self._dictionary.tag
        self._log.debug("Using dictionary '%s'" % language)

        # create a spellchecker that is aware of email addresses and URLs
        self._checker = enchant.checker.SpellChecker(
            language,
            filters=[enchant.tokenize.EmailFilter, enchant.tokenize.URLFilter])

        self._initialized = True
Exemplo n.º 5
0
    def initSpellchecker(self):
        # TODO: disable spellchecker icon in case of not working enchant
        try:
            import enchant
            spellDictDir = settings.get('spellchecker:directory')
            if spellDictDir:
                if enchant.__ver_major__ >= 1 and enchant.__ver_minor__ >= 6:
                    enchant.set_param("enchant.myspell.dictionary.path",
                                      spellDictDir)
                else:
                    print "Your pyenchant version is to old. Please " \
                          "upgrade to version 1.6.0 or higher, if you want " \
                          "to use spellchecker."
                    return None

            spellLang = settings.get('spellchecker:lang')
            if enchant.dict_exists(spellLang):
                self.dict = enchant.Dict(spellLang)
            else:
                # try dictionary based on the current locale
                try:
                    self.dict = enchant.Dict()
                    settings.set('spellchecker:lang', self.dict.tag)
                except:  # we don not have working dictionary...
                    return None
            if self.dict:
                self.usePWL(self.dict)

        except:
            print "can not start spellchecker!!!"
            import traceback
            traceback.print_exc()
            return None
Exemplo n.º 6
0
    def __init__(self, name):
        self._lang = name
        if not (self._lang and enchant.dict_exists(self._lang)):
            self._lang = self.getDefaultDictionary()

        self._dict = enchant.DictWithPWL(self._lang,
                                         self.getCustomDictionaryPath())
Exemplo n.º 7
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        import enchant

        raw = request.raw_post_data
        input = json.loads(raw)
        id = input["id"]
        method = input["method"]
        params = input["params"]
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError("dictionary not found for language '%s'" % lang)

        checker = enchant.Dict(str(lang))

        if method == "checkWords":
            result = [word for word in arg if not checker.check(word)]
        elif method == "getSuggestions":
            result = checker.suggest(arg)
        else:
            raise RuntimeError("Unkown spellcheck method: '%s'" % method)
        output = {"id": id, "result": result, "error": None}
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(json.dumps(output), content_type="application/json")
Exemplo n.º 8
0
def aspell(string):
    '''
    если ok:    False, []
    если ошибка: True, aspell_sug

    '''
    enchant.set_param("enchant.aspell.dictionary.path",
                      "./aspell6-ru-0.99f7-1")
    if not enchant.dict_exists('ru_RU'):
        copyfile(
            './resources/aspell/ru_RU.dic',
            os.path.dirname(enchant.__file__).replace('\\', '/') +
            '/share/enchant/myspell/ru_RU.dic')
        copyfile(
            './resources/aspell/ru_RU.aff',
            os.path.dirname(enchant.__file__).replace('\\', '/') +
            '/share/enchant/myspell/ru_RU.aff')
    d = enchant.Dict("ru_RU")
    if d.check(string):  # aspell says OK
        # print('aspell says OK')
        return False, []
    else:
        # print('aspell says mistake and suggests:')
        aspell_sug = d.suggest(string)
        return True, aspell_sug
Exemplo n.º 9
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        import enchant

        raw = request.raw_post_data
        input = simplejson.loads(raw)
        id = input['id']
        method = input['method']
        params = input['params']
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError("dictionary not found for language '%s'" % lang)
        checker = enchant.Dict(lang)

        if method == 'checkWords':
            result = [word for word in arg if not checker.check(word)]
        elif method == 'getSuggestions':
            result = checker.suggest(arg)
        else:
            raise RuntimeError("Unkown spellcheck method: '%s'" % method)
        output = {
            'id': id,
            'result': result,
            'error': None,
        }
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(simplejson.dumps(output),
                        content_type='application/json')
Exemplo n.º 10
0
def spellcheck(request):
    """
Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
"""
    try:
        raw = request.raw_post_data
        input = json.loads(raw)
        id = input['id']
        method = input['method']
        params = input['params']
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError("dictionary not found for language '%s'" % lang)

        checker = enchant.Dict(str(lang))

        if method == 'checkWords':
            result = [word for word in arg if word and not checker.check(word)]
        elif method == 'getSuggestions':
            result = checker.suggest(arg)
        else:
            raise RuntimeError("Unkown spellcheck method: '%s'" % method)
        output = {
            'id': id,
            'result': result,
            'error': None,
        }
    except Exception:
        return HttpResponse("Error running spellchecker")
    return HttpResponse(json.dumps(output),
            content_type='application/json')
Exemplo n.º 11
0
    def initSpellchecker(self):
        # TODO: disable spellchecker icon in case of not working enchant
        try:
            import enchant
            spellDictDir = settings.get('spellchecker:directory')
            if spellDictDir:
                if enchant.__ver_major__ >= 1 and enchant.__ver_minor__ >= 6:
                    enchant.set_param("enchant.myspell.dictionary.path",
                                      spellDictDir)
                else:
                    print "Your pyenchant version is to old. Please " \
                          "upgrade to version 1.6.0 or higher, if you want " \
                          "to use spellchecker."
                    return None

            spellLang = settings.get('spellchecker:lang')
            if enchant.dict_exists(spellLang):
                self.dict = enchant.Dict(spellLang)
            else:
                # try dictionary based on the current locale
                try:
                    self.dict = enchant.Dict()
                    settings.set('spellchecker:lang', self.dict.tag)
                except:  # we don not have working dictionary...
                    return None
            if self.dict:
                self.usePWL(self.dict)

        except:
            print "can not start spellchecker!!!"
            import traceback
            traceback.print_exc()
            return None
    def __init__(self,
                 languages=('en_US', ),
                 unicode_data=True,
                 cldr_data=True,
                 quick=True):
        '''
        Initialize the emoji matcher

        :param languages: A list of languages to use for matching emoji
        :type languages: List or tuple of strings
        :param unicode_data: Whether to load the UnicodeData.txt file as well
        :type unicode_data: Boolean
        :param cldr_data: Whether to load data from CLDR as well
        :type cldr_data: Boolean
        :param quick: Whether to do a quicker but slighly less precise match.
                      Quick matching is about 4 times faster and usually
                      good enough.
        :type quick: Boolean
        '''
        self._languages = languages
        self._quick = quick
        self._enchant_dicts = []
        if IMPORT_ENCHANT_SUCCESSFUL:
            for language in self._languages:
                if enchant.dict_exists(language):
                    self._enchant_dicts.append(enchant.Dict(language))
        # From the documentation
        # (https://docs.python.org/3.6/library/difflib.html):
        # “SequenceMatcher computes and caches detailed information
        # about the second sequence, so if you want to compare one
        # sequence against many sequences, use set_seq2() to set the
        # commonly used sequence once and call set_seq1() repeatedly,
        # once for each of the other sequences.”
        self._matcher = SequenceMatcher(isjunk=None,
                                        a='',
                                        b='',
                                        autojunk=False)
        self._match_cache = {}
        self._string1 = ''
        self._seq1 = ''
        self._len1 = 0
        self._string2 = ''
        self._string2_number_of_words = 0
        self._string2_word_list = []
        self._seq2 = ''
        self._len2 = 0
        self._emoji_dict = {}
        self._candidate_cache = {}
        # The three data sources are loaded in this order on purpose.
        # The data from Unicode is loaded first to put the official
        # names first into the list of names to display the official
        # names in the candidates, if possible.  The second best names
        # are the long names of emojione.
        if unicode_data:
            self._load_unicode_data()
        self._load_emojione_data()
        if cldr_data:
            for language in _expand_languages(self._languages):
                self._load_cldr_annotation_data(language)
Exemplo n.º 13
0
 def __init__(self, language="en_US"):
     if not enchant.dict_exists(language):
         logging.warning("Spelling_Corrector: Don't have {} , Please check it!!!", language)
         logging.warning("Recommend same language for you: {}", enchant.list_languages())
         language = "en_US"
     self.dict = enchant.Dict(language)
     self.check = SpellChecker(language)
     self.tokenizer = get_tokenizer(language)
Exemplo n.º 14
0
def addspell(self, channel, sender, line):
    words = line.split()
    if enchant.dict_exists(words[0]):
        d = enchant.Dict(words.pop(0))
    else:
        d = enchant.Dict('en')
    for i in words:
        d.add(i)
Exemplo n.º 15
0
def clean_ques(ques):
	ques = ques.lower()
	ques = tokenizer.tokenize(ques)
	for i in range(len(ques)):
		if not enchant.dict_exists(ques[i]):
 			ques[i] = dictionary.suggest(ques[i])[0]
	#ques = [q for q in ques if q not in stop]
	ques = [stemmer.stem(q) for q in ques if q not in stop]
	return ques
Exemplo n.º 16
0
 def __init__(self,language):
     '''
     Constructor
     '''
     if enchant.dict_exists(language):
         self.dictionnary = enchant.Dict(language)
     else:
         availableDicos = '[%s]' % ', '.join(map(str, enchant.list_languages()))                              
         sys.exit("No dictionary installed for {}, the available dictionaries are: {}".format(language,availableDicos))
Exemplo n.º 17
0
def clean_ques(ques):
    ques = ques.lower()
    ques = tokenizer.tokenize(ques)
    for i in range(len(ques)):
        if not enchant.dict_exists(ques[i]):
            ques[i] = dictionary.suggest(ques[i])[0]
    #ques = [q for q in ques if q not in stop]
    ques = [stemmer.stem(q) for q in ques if q not in stop]
    return ques
Exemplo n.º 18
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        if not enchant:
            raise RuntimeError("install pyenchant for spellchecker functionality")

        raw = force_text(request.body)
        raw = force_text(request.body)
        try:
            input = json.loads(raw)
            id = input['id']
            method = input['method']
            params = input['params']
            lang = params[0]
            arg = params[1]
        except ValueError:
            data = QueryDict(raw.encode('utf8'))
            method = data.get('method')
            text = data.get('text')
            lang = data.get('lang')


        if not enchant.dict_exists(str(lang)):
            raise RuntimeError("dictionary not found for language {!r}".format(lang))

        checker = enchant.Dict(str(lang))

        if method == 'spellcheck':
            res = {}
            for word in text.split():
                tmp = word.encode('utf8').strip()
                if tmp == '':
                    continue
                if not checker.check(tmp):
                    res[tmp] = checker.suggest(tmp)
            output = dict(words=res)
        else:
            if method == 'checkWords':
                result = [word for word in arg if word and not checker.check(word)]
            elif method == 'getSuggestions':
                result = checker.suggest(arg)
            else:
                raise RuntimeError("Unknown spellcheck method: {!r}".format(method))
            output = {
                'id': id,
                'result': result,
                'error': None,
            }
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(json.dumps(output),
                        content_type='application/json')
Exemplo n.º 19
0
 def set_dict(self, tag):
     if tag:
         logger.info('Setting dictionary %s', tag)
     if tag and enchant and enchant.dict_exists(tag):
         self.dict = enchant.Dict(tag)
     else:
         if tag:
             logger.warning('Failed to set dictionary %s', tag)
         self.dict = None
     self.config_store.set('spelling', 'language', self.current_language())
     self.new_dict.emit()
Exemplo n.º 20
0
 def set_dict(self, tag):
     if tag:
         logger.info('Setting dictionary %s', tag)
     if tag and enchant and enchant.dict_exists(tag):
         self.dict = enchant.Dict(tag)
     else:
         if tag:
             logger.warning('Failed to set dictionary %s', tag)
         self.dict = None
     self.config_store.set('spelling', 'language', self.current_language())
     self.new_dict.emit()
Exemplo n.º 21
0
def loadDicts():
    global text_dic, en_dic
    en_dic = en.Dict("ru_RU")
    if  en.dict_exists("ru_RU"):
        if os.path.exists(os.path.expanduser('~/.ExPy/dict.txt')):
            buf_dic = en.DictWithPWL("ru_RU",os.path.expanduser('~/.ExPy/dict.txt'))
            text_dic = SpellChecker(buf_dic, filters=[EmailFilter,URLFilter])
        else:
            text_dic = SpellChecker("ru_RU", filters=[EmailFilter,URLFilter])
    else:
        QtGui.QMessageBox.information(None,
                "Не обнаружен словарь", "Не обнаружен русский словарь Hunspell, проверка орфографии невозможна")
Exemplo n.º 22
0
 def set_dict(self, tag):
     if not bool(enchant):
         self.dict = None
     elif tag and enchant.dict_exists(tag):
         self.dict = enchant.Dict(tag)
     else:
         self.dict = None
     if self.dict:
         self.tag = self.dict.tag
     else:
         self.tag = ''
     self.new_dict.emit()
Exemplo n.º 23
0
 def init_language(self):
     '''Init self.language.'''
     c = self.c
     language = g.toUnicode(c.config.getString('enchant_language'))
     if language:
         try:
             ok = enchant.dict_exists(language)
         except Exception:
             ok = False
         if not ok:
             g.warning('Invalid language code for Enchant', repr(language))
             g.es_print('Using "en_US" instead')
             language = 'en_US'
     self.language = language
Exemplo n.º 24
0
    def getDefaultDictionary():
        if not EnchantDictionary.isInstalled():
            return None

        default_locale = enchant.get_default_language()
        if default_locale and not enchant.dict_exists(default_locale):
            default_locale = None

        if default_locale is None:
            default_locale = QLocale.system().name()
        if default_locale is None:
            default_locale = self.availableDictionaries()[0]

        return default_locale
Exemplo n.º 25
0
 def set_language(self, code):
     if code:
         logger.debug('Setting dictionary %s', code)
     self.dict = None
     if Gspell:
         if code:
             self.dict = Gspell.Checker.new(Gspell.Language.lookup(code))
     elif enchant:
         if code and enchant.dict_exists(code):
             self.dict = enchant.Dict(code)
     if code and not self.dict:
         logger.warning('Failed to set dictionary %s', code)
     self.config_store.set('spelling', 'language', self.current_language())
     self.new_dict.emit()
Exemplo n.º 26
0
 def init_language(self):
     """Init self.language."""
     c = self.c
     language = g.checkUnicode(c.config.getString('enchant-language'))
     if language:
         try:
             ok = enchant.dict_exists(language)
         except Exception:
             ok = False
         if not ok:
             g.warning('Invalid language code for Enchant', repr(language))
             g.es_print('Using "en_US" instead')
             g.es_print('Use @string enchant_language to specify your language')
             language = 'en_US'
     self.language = language
Exemplo n.º 27
0
 def init_language(self):
     '''Init self.language.'''
     c = self.c
     language = g.toUnicode(c.config.getString('enchant_language'))
     if language:
         try:
             ok = enchant.dict_exists(language)
         except Exception:
             ok = False
         if not ok:
             g.warning('Invalid language code for Enchant', repr(language))
             g.es_print('Using "en_US" instead')
             g.es_print('Use @string enchant_language to specify your language')
             language = 'en_US'
     self.language = language
Exemplo n.º 28
0
def loadDicts():
    global text_dic, en_dic
    en_dic = en.Dict("ru_RU")
    if en.dict_exists("ru_RU"):
        if os.path.exists(os.path.expanduser('~/.ExPy/dict.txt')):
            buf_dic = en.DictWithPWL("ru_RU",
                                     os.path.expanduser('~/.ExPy/dict.txt'))
            text_dic = SpellChecker(buf_dic, filters=[EmailFilter, URLFilter])
        else:
            text_dic = SpellChecker("ru_RU", filters=[EmailFilter, URLFilter])
    else:
        QtGui.QMessageBox.information(
            None, "Не обнаружен словарь",
            "Не обнаружен русский словарь Hunspell, проверка орфографии невозможна"
        )
Exemplo n.º 29
0
    def spell_check(self, post, lang):
        """ Check spellings for the given post and given language. """

        if enchant.dict_exists(lang):
            checker = SpellChecker(lang, filters=[EmailFilter, URLFilter])
            checker.set_text(post.text(lang=lang, strip_html=True))
            words = [error.word for error in checker]
            words = [
                word for word in words
                if self._not_in_other_dictionaries(word, lang)
            ]
            LOGGER.notice('Mis-spelt words in %s: %s' %
                          (post.fragment_deps(lang), ', '.join(words)))

        else:
            LOGGER.notice('No dictionary found for %s' % lang)
Exemplo n.º 30
0
 def __init__(self, c):
     """Ctor for EnchantClass class."""
     self.c = c
     language = g.toUnicode(c.config.getString("enchant_language"))
     # Set the base language
     if language and not enchant.dict_exists(language):
         g.warning("Invalid language code for Enchant", repr(language))
         g.es_print('Using "en_US" instead')
         language = "en_US"
     # Compute fn, the full path to the local dictionary.
     fn = c.config.getString("enchant_local_dictionary")
     if not fn:
         fn = g.os_path_finalize_join(g.app.loadDir, "..", "plugins", "spellpyx.txt")
     # Fix bug https://github.com/leo-editor/leo-editor/issues/108
     if not g.os_path_exists(fn):
         fn = g.os_path_finalize_join(g.app.homeDir, ".leo", "spellpyx.txt")
     self.open_dict(fn, language)
Exemplo n.º 31
0
 def __init__(self, c):
     """Ctor for EnchantClass class."""
     self.c = c
     language = g.toUnicode(c.config.getString('enchant_language'))
     # Set the base language
     if language and not enchant.dict_exists(language):
         g.warning('Invalid language code for Enchant', repr(language))
         g.es_print('Using "en_US" instead')
         language = 'en_US'
     # Compute fn, the full path to the local dictionary.
     fn = c.config.getString('enchant_local_dictionary')
     if not fn:
         fn = g.os_path_finalize_join(g.app.loadDir, "..", "plugins", 'spellpyx.txt')
     # Fix bug https://github.com/leo-editor/leo-editor/issues/108
     if not g.os_path_exists(fn):
         fn = g.os_path_finalize_join(g.app.homeDir, '.leo', 'spellpyx.txt')
     self.open_dict(fn, language)
Exemplo n.º 32
0
 def __init__(self, c):
     """Ctor for EnchantClass class."""
     # pylint: disable=super-init-not-called
     self.c = c
     language = g.toUnicode(c.config.getString('enchant_language'))
     # Set the base language
     if language and not enchant.dict_exists(language):
         g.warning('Invalid language code for Enchant', repr(language))
         g.es_print('Using "en_US" instead')
         language = 'en_US'
     # Compute fn, the full path to the local dictionary.
     fn = c.config.getString('enchant_local_dictionary')
     if not fn:
         fn = g.os_path_finalize_join(g.app.loadDir, "..", "plugins", 'spellpyx.txt')
     # Fix bug https://github.com/leo-editor/leo-editor/issues/108
     if not g.os_path_exists(fn):
         fn = g.os_path_finalize_join(g.app.homeDir, '.leo', 'spellpyx.txt')
     self.open_dict(fn, language)
Exemplo n.º 33
0
    def spell_check(self, post, lang):
        """ Check spellings for the given post and given language. """

        if enchant.dict_exists(lang):
            checker = SpellChecker(lang, filters=[EmailFilter, URLFilter])
            checker.set_text(post.text(lang=lang, strip_html=True))
            words = [error.word for error in checker]
            words = [
                word for word in words if
                self._not_in_other_dictionaries(word, lang)
            ]
            LOGGER.notice(
                'Mis-spelt words in %s: %s' % (
                    post.fragment_deps(lang), ', '.join(words)
                )
            )

        else:
            LOGGER.notice('No dictionary found for %s' % lang)
Exemplo n.º 34
0
def spell_check(request):
    """
    Returns a response that implements the TinyMCE spellchecker protocol.
    """
    try:
        if not enchant:
            raise RuntimeError("install pyenchant for spellchecker functionality")

        method = request.POST.get("method")
        text = request.POST.get("text")
        lang = request.POST.get("lang")

        if not enchant.dict_exists(lang):
            e_msg = f"Dictionary not found for language '{lang}', check pyenchant."
            raise RuntimeError(e_msg)

        checker = enchant.Dict(lang)

        def sanitize_words(text):
            """
            Sanitize words in text and recommend suggestions for wrong words.
            """
            suggested_words = {}
            words = text.split()
            for word in words:
                word.strip()
                word.strip(".,:;'\"")
                if not checker.check(word):
                    suggested_words[word] = checker.suggest(word)
            return suggested_words

        if method == "spellcheck":
            output = {"words": sanitize_words(text)}
        else:
            e_msg = f"Got an unexpected method '{method}'"
            raise RuntimeError(e_msg)

    except Exception as err:
        logging.exception("Error running spellchecker")
        output = {"error": str(err)}

    return JsonResponse(output)
Exemplo n.º 35
0
def spell(inp):
    "spell <word> -- Check spelling of <word>."

    if ' ' in inp:
        return "This command only supports one word at a time."

    if not enchant.dict_exists(locale):
        return "Could not find dictionary: "

    dictionary = enchant.Dict(locale)
    is_correct = dictionary.check(inp)
    suggestions = dictionary.suggest(inp)
    s_string = ', '.join(suggestions[:10])

    if is_correct:
        return '"{}" appears to be \x02valid\x02! ' \
               '(suggestions: {})'.format(inp, s_string)
    else:
        return '"{}" appears to be \x02invalid\x02! ' \
               '(suggestions: {})'.format(inp, s_string)
Exemplo n.º 36
0
def spell(self, channel, sender, line):
    if len(line.split()) == 2:
        lang, word = line.split()
        try:
            d = enchant.Dict(lang)
        except enchant.errors.DictNotFoundError:
            return 'Languages: ' + ', '.join(enchant.list_languages())
        return sugg(d, word)
    if len(line.split()) == 1:
        d = enchant.Dict('en_US')
        return sugg(d, line)
    if not line:
        return 'Usage: [language] {<word for suggestions> | <sentence>}'
    words = line.split()
    if enchant.dict_exists(words[0]):
        c = enchant.checker.SpellChecker(words.pop(0))
    else:
        c = enchant.checker.SpellChecker('en_US')
    c.set_text(' '.join(words))
    return 'Unknown words: ' + ', '.join([i.word for i in c])
Exemplo n.º 37
0
def spell(inp):
    "spell <word> -- Check spelling of <word>."

    if ' ' in inp:
        return "This command only supports one word at a time."

    if not enchant.dict_exists(locale):
        return "Could not find dictionary: {}".format(locale)

    dictionary = enchant.Dict(locale)
    is_correct = dictionary.check(inp)
    suggestions = dictionary.suggest(inp)
    s_string = ', '.join(suggestions[:10])

    if is_correct:
        return '"{}" appears to be \x02valid\x02! ' \
               '(suggestions: {})'.format(inp, s_string)
    else:
        return '"{}" appears to be \x02invalid\x02! ' \
               '(suggestions: {})'.format(inp, s_string)
Exemplo n.º 38
0
def spell_check(request):
    """Spellcheck view."""
    import enchant

    raw = request.body
    input = json.loads(raw)
    id = input['id'] if ('id' in input) else None
    method = input['method'] if ('method' in input) else {}
    params = input['params'] if ('params' in input) else {}

    result = {}

    try:
        if params and method:
            lang = params['lang'] if ('lang' in params) else 'en-au'
            arg = params['words'] if ('words' in params) else []

            if not enchant.dict_exists(str(lang)):
                raise RuntimeError("dictionary not found for language '%s'" %
                                   lang)

            checker = enchant.Dict(str(lang))

            if method == 'spellcheck':
                for x in [
                        word for word in arg
                        if word and not checker.check(word)
                ]:
                    result[x] = checker.suggest(x)

    except Exception:
        pass

    output = {
        'id': id,
        'result': result,
    }
    # except Exception:
    #    logging.exception("Error running spellchecker")
    #    return HttpResponse("Error running spellchecker")
    return HttpResponse(json.dumps(output), content_type='application/json')
Exemplo n.º 39
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        if not enchant:
            raise RuntimeError(
                "install pyenchant for spellchecker functionality")

        raw = force_text(request.body)
        input = json.loads(raw)
        id = input['id']
        method = input['method']
        params = input['params']
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError(
                "dictionary not found for language {!r}".format(lang))

        checker = enchant.Dict(str(lang))

        if method == 'checkWords':
            result = [word for word in arg if word and not checker.check(word)]
        elif method == 'getSuggestions':
            result = checker.suggest(arg)
        else:
            raise RuntimeError(
                "Unknown spellcheck method: {!r}".format(method))
        output = {
            'id': id,
            'result': result,
            'error': None,
        }
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(json.dumps(output), content_type='application/json')
Exemplo n.º 40
0
def spell_check(request):
    """Spellcheck view."""
    import enchant

    raw = request.body
    input = json.loads(raw)
    id = input['id'] if ('id' in input) else None
    method = input['method'] if ('method' in input) else {}
    params = input['params'] if ('params' in input) else {}

    result = {}

    try:
        if params and method:
            lang = params['lang'] if ('lang' in params) else 'en-au'
            arg = params['words'] if ('words' in params) else []

            if not enchant.dict_exists(str(lang)):
                raise RuntimeError(
                        "dictionary not found for language '%s'" % lang)

            checker = enchant.Dict(str(lang))

            if method == 'spellcheck':
                for x in [word for word in arg if word and not
                          checker.check(word)]:
                    result[x] = checker.suggest(x)

    except Exception:
        pass

    output = {
        'id': id,
        'result': result,
        }
    # except Exception:
    #    logging.exception("Error running spellchecker")
    #    return HttpResponse("Error running spellchecker")
    return HttpResponse(json.dumps(output), content_type='application/json')
Exemplo n.º 41
0
def spell(inp):
    """spell <word/sentence> -- Check spelling of a word or sentence."""

    if not enchant.dict_exists(locale):
        return "Could not find dictionary: {}".format(locale)

    if len(inp.split(" ")) > 1:
        # input is a sentence
        checker = SpellChecker(locale)
        checker.set_text(inp)

        offset = 0
        for err in checker:
            # find the location of the incorrect word
            start = err.wordpos + offset
            finish = start + len(err.word)
            # get some suggestions for it
            suggestions = err.suggest()
            s_string = '/'.join(suggestions[:3])
            s_string = "\x02{}\x02".format(s_string)
            # calculate the offset for the next word
            offset = (offset + len(s_string)) - len(err.word)
            # replace the word with the suggestions
            inp = inp[:start] + s_string + inp[finish:]
        return inp
    else:
        # input is a word
        dictionary = enchant.Dict(locale)
        is_correct = dictionary.check(inp)
        suggestions = dictionary.suggest(inp)
        s_string = ', '.join(suggestions[:10])
        if is_correct:
            return '"{}" appears to be \x02valid\x02! ' \
                   '(suggestions: {})'.format(inp, s_string)
        else:
            return '"{}" appears to be \x02invalid\x02! ' \
                   '(suggestions: {})'.format(inp, s_string)
Exemplo n.º 42
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        if not enchant:
            raise RuntimeError("install pyenchant for spellchecker functionality")

        raw = force_text(request.body)
        input = json.loads(raw)
        id = input['id']
        method = input['method']
        params = input['params']
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError("dictionary not found for language {!r}".format(lang))

        checker = enchant.Dict(str(lang))

        if method == 'checkWords':
            result = [word for word in arg if word and not checker.check(word)]
        elif method == 'getSuggestions':
            result = checker.suggest(arg)
        else:
            raise RuntimeError("Unknown spellcheck method: {!r}".format(method))
        output = {
            'id': id,
            'result': result,
            'error': None,
        }
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(json.dumps(output),
                        content_type='application/json')
Exemplo n.º 43
0
def spell_check(request):
    """
    Returns a HttpResponse that implements the TinyMCE spellchecker protocol.
    """
    try:
        if not enchant:
            raise RuntimeError(
                "install pyenchant for spellchecker functionality")

        raw = force_text(request.body)
        input = json.loads(raw)
        id = input["id"]
        method = input["method"]
        params = input["params"]
        lang = params[0]
        arg = params[1]

        if not enchant.dict_exists(str(lang)):
            raise RuntimeError(f"dictionary not found for language {lang}")

        checker = enchant.Dict(str(lang))

        if method == "checkWords":
            result = [word for word in arg if word and not checker.check(word)]
        elif method == "getSuggestions":
            result = checker.suggest(arg)
        else:
            raise RuntimeError(f"Unknown spellcheck method: {method}")
        output = {
            "id": id,
            "result": result,
            "error": None,
        }
    except Exception:
        logging.exception("Error running spellchecker")
        return HttpResponse(_("Error running spellchecker"))
    return HttpResponse(json.dumps(output), content_type="application/json")
Exemplo n.º 44
0
def spell(inp):
    """spell <word/sentence> -- Check spelling of a word or sentence."""

    if not enchant.dict_exists(locale):
        return "Could not find dictionary: {}".format(locale)

    if len(inp.split(" ")) > 1:
        # input is a sentence
        chkr = SpellChecker(locale)
        chkr.set_text(inp)

        offset = 0
        for err in chkr:
            # find the location of the incorrect word
            start = err.wordpos + offset
            finish = start + len(err.word)
            # get some suggestions for it
            suggestions = err.suggest()
            s_string = '/'.join(suggestions[:3])
            s_string = "\x02{}\x02".format(s_string)
            # calculate the offset for the next word
            offset = (offset + len(s_string)) - len(err.word)
            # replace the word with the suggestions
            inp = inp[:start] + s_string + inp[finish:]
        return inp
    else:
        # input is a word
        dictionary = enchant.Dict(locale)
        is_correct = dictionary.check(inp)
        suggestions = dictionary.suggest(inp)
        s_string = ', '.join(suggestions[:10])
        if is_correct:
            return '"{}" appears to be \x02valid\x02! ' \
                   '(suggestions: {})'.format(inp, s_string)
        else:
            return '"{}" appears to be \x02invalid\x02! ' \
                   '(suggestions: {})'.format(inp, s_string)
Exemplo n.º 45
0
import os

from django.conf import settings
from django.contrib.staticfiles import finders

import enchant
enchant.dict_exists('pl')

DEFAULT_CONFIG = getattr(
    settings, 'TINYMCE_DEFAULT_CONFIG', {
        'plugins': "spellchecker",
        'theme': "advanced",
        'toolbar': "undo redo | styleselect | bold italic | link image",
        'relative_urls': False,
        'width': '100%',
        'height': 500,
    })

USE_SPELLCHECKER = getattr(settings, 'TINYMCE_SPELLCHECKER', True)

USE_COMPRESSOR = getattr(settings, 'TINYMCE_COMPRESSOR', False)

INCLUDE_JQUERY = getattr(settings, 'TINYMCE_INCLUDE_JQUERY', True)

USE_EXTRA_MEDIA = getattr(settings, 'TINYMCE_EXTRA_MEDIA', None)

USE_FILEBROWSER = getattr(settings, 'TINYMCE_FILEBROWSER', 'filebrowser'
                          in settings.INSTALLED_APPS)

if 'staticfiles' in settings.INSTALLED_APPS or 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
    JS_URL = getattr(settings, 'TINYMCE_JS_URL',
Exemplo n.º 46
0
"""
Django settings for CompeteFashion project.

Generated by 'django-admin startproject' using Django 1.9.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import enchant
enchant.dict_exists('en')

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
STATIC_PATH = os.path.join(BASE_DIR, 'static')
SITE_ID = 1
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'eiyn&c$k!k@7z+clovku%o_)rp5u0en!_=tr=^!=h$_tv6*awu'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
Exemplo n.º 47
0
def check_collection(inpath, outpath, lang, wordFiles=[]):
    """
    Checks the orthography of the text in a collection. The expected input are plain text files.
    
    Arguments:
    inpath (string): path to the input files, including file name pattern
    outpath (string): path to the output file, including the output file's name
    lang (string): which dictionary to use, e.g. "es", "fr", "de"
    wordFiles (list): optional; list of strings; paths to files with lists of words which will not be treated as errors (e.g. named entities)
    """

    try:
        enchant.dict_exists(lang)
        try:
            tknzr = get_tokenizer(lang)
        except enchant.errors.TokenizerNotFoundError:    
            tknzr = get_tokenizer()
        chk = checker.SpellChecker(lang, tokenize=tknzr)
        
    except enchant.errors.DictNotFoundError:
        print("ERROR: The dictionary " + lang + "doesn't exist. Please choose another dictionary.")
        sys.exit(0)

    all_words = []
    all_num = []
    all_idnos = []

    print("...checking...")
    for file in glob.glob(inpath):
        idno = os.path.basename(file)[-10:-4]
        all_idnos.append(idno)
        
        err_words = []

        with open(file, "r", encoding="UTF-8") as fin:
            intext = fin.read().lower()
            chk.set_text(intext)

        if len(wordFiles) !=0:
            allCorrects = ""
            for file in wordFiles:
                with open(file, "r", encoding="UTF-8") as f:
                     corrects = f.read().lower()
                     allCorrects = allCorrects + corrects

        for err in chk:
            if not wordFiles or err.word not in allCorrects: 
                err_words.append(err.word)
            all_words.append(err_words)

        err_num = collections.Counter(err_words)
        all_num.append(err_num)
        
        print("..." + str(len(err_num)) + " different errors found in " + idno)
        
    df = pd.DataFrame(all_num,index=all_idnos).T
    
    df = df.fillna(0)
    df = df.astype(int)
    
    df["sum"] = df.sum(axis=1)
    # df = df.sort("sum", ascending=False)
    df = df.sort_values(by="sum", ascending=False)
    
    df.to_csv(outpath)
    print("done")
Exemplo n.º 48
0
def test_HasENUS():
    """Test that the en_US language is available through default broker."""
    assert dict_exists("en_US")
 def __init__(self, lang):
     import enchant
     self.lang = str(lang)
     if not enchant.dict_exists(lang):
         raise RuntimeError("dictionary not found for language '%s'" % lang)
     self.backend = enchant.Dict(lang)
Exemplo n.º 50
0
 def setDict(self, d):
     self.currentDict = d
     if d and enchant.dict_exists(d):
         self._dict = enchant.Dict(d)
     if self.highlighter:
         self.highlighter.rehighlight()
Exemplo n.º 51
0
 def set_language(self, language):
     if not enchant.dict_exists(language):
         return
     self.language = language
     self.spellchecker = enchant.Dict(language)
Exemplo n.º 52
0
print "Check a mis-spelled word: "
print "\tcheck(\"Helo\"): {}".format(d.check("Helo"))
print "\n"

print "suggest a word:"
print "* ordered from most likely replacement to least likely"
print "\tsuggest(\"Helo\"): {}".format(d.suggest("Helo"))
print "\n"

print "attempt to determine language:"
print "\ttag: {}".format(d.tag)
print "\n"

print "check whether dictionaries exist:"
print "\tenchant.dict_exists(\"en_US\"): {}".format(enchant.dict_exists("en_US"))
print "\tenchant.dict_exists(\"fake\"): {}".format(enchant.dict_exists("fake"))
print "\n"

print "List available dictionaries:"
print "\tenchant.list_languages(): {}".format(enchant.list_languages())
print "\n"

print "Providers:"
print "* Different providers exist using different frameworks: (aspell, MySpell)."
print "\tprovider: {}".format(d.provider)
print "\tprovider.name: {}".format(d.provider.name)
print "\tprovider.file: {}".format(d.provider.file)
print "\n"

print "Brokers:"
Exemplo n.º 53
0
 def set_language(self,  language):
     if not enchant.dict_exists(language):
         return
     self.language = language
     self.spellchecker = enchant.Dict(language)
Exemplo n.º 54
0
def check_collection(inpath, outpath, lang, wordFiles=[]):
    """
    Checks the orthography of the text in a collection. The expected input are plain text files.
    
    Arguments:
    inpath (string): path to the input files, including file name pattern
    outpath (string): path to the output file, including the output file's name
    lang (string): which dictionary to use, e.g. "es", "fr", "de"
    wordFiles (list): optional; list of strings; paths to files with lists of words which will not be treated as errors (e.g. named entities)
    """

    try:
        enchant.dict_exists(lang)
        try:
            tknzr = get_tokenizer(lang)
        except enchant.errors.TokenizerNotFoundError:
            tknzr = get_tokenizer()
        chk = checker.SpellChecker(lang, tokenize=tknzr)

    except enchant.errors.DictNotFoundError:
        print("ERROR: The dictionary " + lang +
              "doesn't exist. Please choose another dictionary.")
        sys.exit(0)

    all_words = []
    all_num = []
    all_idnos = []

    print("...checking...")
    for file in glob.glob(inpath):
        idno, ext = os.path.basename(file).split(".")
        all_idnos.append(idno)

        err_words = []

        with open(file, "r", encoding="UTF-8") as fin:
            intext = fin.read().lower()
            chk.set_text(intext)

        if len(wordFiles) != 0:
            allCorrects = ""
            for file in wordFiles:
                with open(file, "r", encoding="UTF-8") as f:
                    corrects = f.read().lower()
                    allCorrects = allCorrects + corrects

        for err in chk:
            if not wordFiles or err.word not in allCorrects:
                err_words.append(err.word)
            all_words.append(err_words)

        err_num = collections.Counter(err_words)
        all_num.append(err_num)

        print("..." + str(len(err_num)) + " different errors found in " + idno)

    df = pd.DataFrame(all_num, index=all_idnos).T

    df = df.fillna(0)
    df = df.astype(int)

    df["sum"] = df.sum(axis=1)
    # df = df.sort("sum", ascending=False)
    df = df.sort_values(by="sum", ascending=False)

    df.to_csv(outpath)
    print("done")