Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Spell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-# Albert launcher spell extension


## Installation
Put extension into right location
```
~/.local/share/albert/org.albert.extension.python/modules/
```

## Preparing dependencies

Be sure that you have installed needed packages.
```bash
sudo apt install xclip aspell aspell-en aspell-pl aspell-de
```

## Additional dictionaries

All installed aspell language will be automatically used by this plugin. By default your system should have some languages already installed, like your current default system language. You can list all installed languages with command:
```bash
aspell dump dicts | grep -o -P '[a-z]{2}' | sort -du
```
If you need more languages just install them with:
```bash
audo apt install aspell-XX
```
where XX is your language iso code.

## Usage examples
```
spell en great
spell pl góra
spell de viel
```
156 changes: 156 additions & 0 deletions Spell/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# -*- coding: utf-8 -*-

"""
Check spelling of any word in any language you want. Example: spell en great
"""
from time import sleep

from albertv0 import Item
from albertv0 import ClipAction
import os
import re
import subprocess

__iid__ = "PythonInterface/v0.2"
__prettyname__ = "Spell checker"
__version__ = "2.0"
__trigger__ = "spell "
__author__ = "Marek Mazur"
__dependencies__ = []


dict_file = os.path.dirname(__file__) + "/dictionaries/{language}.gz"
dicts_path = os.path.dirname(__file__) + "/dictionaries"
icon_path = os.path.dirname(__file__) + "/spell.svg"
limit = 5


def initialize():
"""
Initialize
"""
aspellDictionaries = findInstalledAspellDictionaries()
for language in aspellDictionaries:
if not dictionaryExists(language):
dumpAspellDictionary(language)


def findInstalledAspellDictionaries():
"""
Find installed Aspell dictionaries
"""
results = ''
try:
results = subprocess.check_output(['aspell', 'dump', 'dicts'])
except subprocess.CalledProcessError:
pass
results = results.splitlines()
return [result.decode('utf-8') for result in results if result.isalpha()]


def dumpAspellDictionary(language):
"""
Dump aspell dictionary

:param str language: Language
"""
command = 'aspell -l {} dump master | aspell -l {} expand | gzip -c > {}/{}.gz'
command = command.format(language, language, dicts_path, language)
subprocess.call(command, shell=True)


def handleQuery(query):
"""
Handle query

:param str query: Query

:return list
"""
if not query.isTriggered:
return

if len(query.string.split()) < 2:
return prepareErrorMessage("Enter a query in the form of 'spell [language] [phrase]'")

language, phrase = prepareParams(query)

if not dictionaryExists(language):
return prepareErrorMessage("Dictionary '{language}' not found!".format(language=language))

results = findInDictionary(language, phrase)
return [prepareResultsItem(query, result) for result in results]


def prepareParams(query):
"""
Prepare params

:return list
"""
match = re.search('^spell (?P<language>[^ ]{2,}) (?P<phrase>.*)$', query.rawString)
if not match:
return ('', '')
return (match.group('language'), match.group('phrase'))


def findInDictionary(language, phrase):
"""
Find in dictionary

:param str language: Language
:param str phrase : Phrase

:return str
"""
a_dict_file = dict_file.format(language=language)
results = ''
try:
results = subprocess.check_output(['zgrep', "^{}".format(re.escape(phrase)), '-m', str(limit), a_dict_file])
except subprocess.CalledProcessError:
pass
results = results.splitlines()
return results


def prepareResultsItem(query, result):
"""
Prepare resuls item

:param str query : Query
:param str result: Result

:return Item
"""
value = result.decode('utf-8').split(' ')[0]
item = Item(id=__prettyname__, icon=icon_path, completion=query.rawString)
item.text = value
item.subtext = result
item.addAction(ClipAction("Copy result to clipboard", value))
return item


def prepareErrorMessage(message):
"""
Prepare error messae

:param str message: Message

:return str
"""
item = Item(id=__prettyname__, icon=icon_path)
item.text = __prettyname__
item.subtext = message
return item


def dictionaryExists(language):
"""
Check if dictionary exists

:param str language: Language

:return bool
"""
a_dict_file = dict_file.format(language=language)
return os.path.exists(os.path.expanduser(a_dict_file))
Empty file added Spell/dictionaries/.keep
Empty file.
Binary file added Spell/dictionaries/de.gz
Binary file not shown.
Binary file added Spell/dictionaries/en.gz
Binary file not shown.
Binary file added Spell/dictionaries/pl.gz
Binary file not shown.
1 change: 1 addition & 0 deletions Spell/spell.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.