Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def detect_language(cls, code):
code = cls.normalize_string(code)
first = code.split(" ")[0]
languages = cls.list_languages()
for lang in languages:
mnemo = cls(lang)
if first in mnemo.wordlist:
return lang
raise ConfigurationError("Language not detected")
def __init__(self, language):
self.radix = 2048
if sys.version < "3":
with open("%s/%s.txt" % (self._get_directory(), language), "r") as f:
self.wordlist = [w.strip().decode("utf8") for w in f.readlines()]
else:
with open(
"%s/%s.txt" % (self._get_directory(), language), "r", encoding="utf-8"
) as f:
self.wordlist = [w.strip() for w in f.readlines()]
if len(self.wordlist) != self.radix:
raise ConfigurationError(
"Wordlist should contain %d words, but it contains %d words."
% (self.radix, len(self.wordlist))
)
def check_mnemonic(mnemonic):
try:
return Mnemonic(language='english').check(mnemonic)
except ConfigurationError:
return False
if not check_mnemonic(mnemonic):