pcy.rule_based.dictionary package¶
Submodules¶
pcy.rule_based.dictionary.autocomplete module¶
Make an Autocomplete class to autocomplete incomplete words using a dictionary tree.
-
class
pcy.rule_based.dictionary.autocomplete.
Autocomplete
(dictionary)¶ Bases:
object
Make an Autocomplete class to autocomplete incomplete words using a dictionary tree.
The Autocomplete class receives a Dictionary tree in its __init__ method. To get the Dictionary tree, we need to use the DictionaryGenerator class.
To generate word suggestions, you need to call the .autocomplete method. Look at the example below to understand everything.
- Code Example:
from pcy.rule_based.dictionary import Autocomplete # here the tree is the dictionary auto = Autocomplete(tree) # calling the method to get complete words based on the incomplete word # here the word "hous" is the incomplete word and 10 is the number of suggestions to return words = auto.autocomplete("hous", 10) # printing the words print(words) # --> ['housage', 'housal', 'housatonic', 'house', 'houseball', 'houseboat', 'houseboating', 'houseboats', # 'houseboy', 'houseboys', 'housebote']
- Parameters
dictionary (node) – Instance of the node class. It can be created using the DictionaryGenerator class.
- Raises
ValueError – When the provided dictionary is not valid
-
autocomplete
(incomplete_word, no_of_suggestions)¶ Return an array of suggestions.
It returns the suggestions based on incomplete_word and no_of_suggestions.
- Parameters
incomplete_word (str) – Incomplete word to autocomplete
no_of_suggestions (int) – Number of suggestions (words) to provide
- Raises
ValueError – When the incomplete_word is not valid string
ValueError – When incomplete_word is empty
ValueError – When the no_of_suggestions is less than zero
- Returns
An array of strings containing suggestions
- Return type
str[]
pcy.rule_based.dictionary.dictionary_generator module¶
Generate a dictionary tree based on words.
-
class
pcy.rule_based.dictionary.dictionary_generator.
DictionaryGenerator
(words)¶ Bases:
object
Generate a dictionary tree based on words.
The DictionaryGenerator class generates a dictionary tree that can be used by the Autocomplete class to autocomplete incomplete words.
- Code Example:
from pcy.rule_based.dictionary import DictionaryGenerator # words list words = [{"words": "Apple", "data": {"color": "red"}}, {"words": "Anaconda", "data": {"color": "black"}}] # initializing the DictionaryGenerator class gen = DictionaryGenerator(words) # generating the tree tree = gen.generate_dictionary()
- Raises
ValueError – When the words are not a valid list
ValueError – When there are no words in the words list
ValueError – When the word is not a valid string
ValueError – When the word is not a valid
ValueError – When there is no dictionary tree
ValueError – When the path to save the tree is not valid
ValueError – When the path to save the tree is not valid
- Returns
Returns the tree
- Return type
Node
-
add_word_to_dictionary
(word, data=None)¶ Add word to generated dictionary.
- Parameters
word (any) – Word that needed to be added
data – Data that needed to be added with the word
- Raises
ValueError – When the word is not valid string
ValueError – When the word is not a valid word
ValueError – When there is no dictionary generated.
- Returns
New dictionary tree
- Return type
Node
-
generate_dictionary
()¶ Generate a new dictionary based on the words list.
- Returns
New dictionary tree
- Return type
Node
-
classmethod
load_dictionary
(path)¶ Load a dictionary from the local file.
- Parameters
path (str) – Path where the dictionary stored in the filesystem
- Raises
ValueError – When the path to the saved dictionary is not valid
- Returns
Returns a new dictionary tree
- Return type
Node
-
save_dictionary
(path)¶ Save dictionary to the local filesystem.
- Parameters
path (str) – Path to store the dictionary
- Raises
ValueError – When the path is not valid
Module contents¶
Autocomplete incomplete words based on generated dictionary tree.