Python enchant.dict_exists() Function

Enchant Module in Python:

Enchant is a Python module that checks a word’s spelling and provides suggestions for correcting it. The antonyms and synonyms of the words are also provided. It determines whether or not a word is in the dictionary.

Python enchant.dict_exists() Function:

enchant.dict_exists() is an enchant module built-in method. It is used to check if a dictionary for a specific language is available or NOT.

Syntax:

enchant.dict_exists(tag)

Parameters

tag: It is the code of the language dictionary given as input.

Return Value:

This function returns a boolean value. True if the dictionary exists, False otherwise.

enchant.dict_exists() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import enchant module using the import keyword
  • Give the tag(language code) to be checked as static input and store it in a variable
  • Pass the given tag as an argument to the dict_exists() function of the enchant module to check whether the dictionary exists or NOT for the given tag
  • Check if the above result is True using the if conditional statement
  • If it is true, then print “The dictionary exists for the given tag”
  • Else print “The dictionary does NOT exist for the given tag”.
  • The Exit of the Program.

Below is the implementation:

# Import enchant module using the import keyword
import enchant

# Give the tag(language code) to be checked as static input and store it in a variable
gvn_tag = "en_US"

# Pass the given tag as an argument to the dict_exists() function 
# of the enchant module to check whether the dictionary exists or NOT for the given tag
dict_exists = enchant.dict_exists(gvn_tag)

# Check if the above result is True using the if conditional statement
if dict_exists == True:
    # If it is true, then print "The dictionary exists for the given tag"
    print("The dictionary exists for the given tag{", gvn_tag, "}")
else:
    # Else print "The dictionary does NOT exist for the given tag"
    print("The dictionary does NOT exist for the given tag{", gvn_tag, "}")

Output:

The dictionary exists for the given tag{ en_US }

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import enchant module using the import keyword
  • Give the tag(language code) to be checked as user input using the input() function and store it in a variable
  • Pass the given tag as an argument to the dict_exists() function of the enchant module to check whether the dictionary exists or NOT for the given tag
  • Check if the above result is True using the if conditional statement
  • If it is true, then print “The dictionary exists for the given tag”
  • Else print “The dictionary does NOT exist for the given tag”.
  • The Exit of the Program.

Below is the implementation:

# Import enchant module using the import keyword
import enchant

# Give the tag(language code) to be checked as user input using the input() function 
# and store it in a variable
gvn_tag = input("Enter some random tag(language code) = ")

# Pass the given tag as an argument to the dict_exists() function 
# of the enchant module to check whether the dictionary exists or NOT for the given tag
dict_exists = enchant.dict_exists(gvn_tag)

# Check if the above result is True using the if conditional statement
if dict_exists == True:
    # If it is true, then print "The dictionary exists for the given tag"
    print("The dictionary exists for the given tag{", gvn_tag, "}")
else:
    # Else print "The dictionary does NOT exist for the given tag"
    print("The dictionary does NOT exist for the given tag{", gvn_tag, "}")

Output:

Enter some random tag(language code) = hi_IN
The dictionary does NOT exist for the given tag{ hi_IN }