Skip to content

Instantly share code, notes, and snippets.

@whnr
Created February 20, 2019 22:28
Show Gist options
  • Save whnr/f8a431dc2945c511bc0c4b07ff997fe0 to your computer and use it in GitHub Desktop.
Save whnr/f8a431dc2945c511bc0c4b07ff997fe0 to your computer and use it in GitHub Desktop.
Combination of language detection (with python package langdetect) and aspell for mutt. To autochoose the right dictionary when spellcheking.
#!/usr/bin/python
from langdetect import detect, detect_langs
from sys import stdin
import argparse
parser = argparse.ArgumentParser(description="Detect the most likely language of a text. Returns a two letter language code")
parser.add_argument('-f', '--file', help="File to be checked, otherwise stdin is used")
# For the future if my pull-request is merged. A limitation to 2 langugages improves speed by a factor of 10.
# parser.add_argument('-l', '--lang', nargs='*', help="List of languages to be checked. If ommitted, all languages are checked.")
parser.add_argument('-v', '--verbose', action='store_true', help="Return the detection probabilities as a list")
args = parser.parse_args()
text = ''
if args.file:
with open(args.file) as f:
text=f.read()
else:
text = stdin.read()
if args.verbose:
lang = detect_langs(text)
else:
lang = detect(text)
print(lang)
#!/bin/bash
# Detecting the language before passing to aspell
# Mutt adds the -x option to the comand set as ispell
# therefore the filename is $2
# Usage in neomutt:
# 1) make script executable
# 2) adjust path to detect_language.py
# 2) set ispell = "/path/to/script/muttspell.sh"
lang=$($HOME/bin/detect_language.py -f $2)
case $lang in
de)
lang='de_DE'
;;
*)
lang=$lang
;;
esac
aspell -l $lang -e -c $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment