spirit avatar spirit committed d91ee55

use_enchant() function instead of USE_ENCHANT global

Comments (0)

Files changed (2)

guess_language/__init__.py

 
 __all__ = [
     "guess_language", "guess_language_tag", "guess_language_id",
-    "guess_language_name", "guess_language_info", "UNKNOWN",
+    "guess_language_name", "guess_language_info", "UNKNOWN", "use_enchant",
 ]
 
-USE_ENCHANT = True
 MAX_LENGTH = 4096
 MIN_LENGTH = 20
 MAX_GRAMS = 300
     return UNKNOWN
 
 
-def check(words, langs, use_enchant=None):
-    """Check what is the best match.
+def check_with_all(words, langs):
+    """Check what the best match is.
     """
-    if use_enchant is None:
-        use_enchant = USE_ENCHANT
+    return check_with_enchant(words, langs) or check_with_models(words, langs)
 
-    if use_enchant:
-        tag = check_with_enchant(words, langs)
-        if tag:
-            return tag
 
+check = check_with_all
+
+
+def use_enchant(use_enchant=True):
+    """Enable or disable checking with PyEnchant.
+    """
+    global check
+    check = check_with_all if use_enchant else check_with_models
+
+
+def check_with_models(words, langs):
+    """Check against known models.
+    """
     sample = " ".join(words)
 
     if len(sample) < MIN_LENGTH:

guess_language/__main__.py

 import os
 import sys
 
-import guess_language.console_mode
+import guess_language.console_mode #@UnusedImport
 
 
 def parse_args():
     with open(file, encoding=encoding) as f:
         text = "".join(f.readlines())
 
-    guess_language.USE_ENCHANT = args.use_enchant
+    if not args.use_enchant:
+        guess_language.use_enchant(False)
     tag = guess_language.guess_language(text)
     print(tag)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.