Homophone dictionary

The HomophoneDictionary class is designed to store homophones in an index. For information on searching using homophones, see the Homophone search page.

The homophone dictionary can be changed at any time, also in an index that contains documents. The dictionary keeps words in lower case without leading and trailing white space, so the methods that get homophones ignore the letter case of the specified word.

To get the number of words in the dictionary, use the count property.

To add groups of homophones to the dictionary, use the add_range method.

The clear method is used to remove all homophones from the dictionary.

The get_homophones method is used to get a list of homophones for a given word. The returned list does not contain the word itself.

The get_homophone_groups method is used to get all homophone groups to which a given word belongs.

To get all homophone groups from the dictionary, use the get_all_homophone_groups method.

To export homophones to a file, use the export_dictionary method.

To import homophones from a file, use the import_dictionary method. Errors of exporting and importing are reported by the error_occurred event of the index instead of exceptions.

The following example demonstrates the use of methods of the homophone dictionary.

from groupdocs.search import Index

# Creating or opening an index in the specified folder
index = Index("./index")

homophones = index.dictionaries.homophone_dictionary

# Getting homophones for the word 'braid'; the letter case of the word is ignored
words = homophones.get_homophones("Braid")
print("Homophones for 'braid':")
for word in words:
    print(word)

# Getting groups of homophones to which the word 'braid' belongs
groups = homophones.get_homophone_groups("braid")
print("Homophone groups for 'braid':")
for group in groups:
    print(" ".join(group))

if homophones.count > 0:
    # Removing all homophones from the dictionary
    homophones.clear()

# Adding homophones to the dictionary
homophone_groups = [
    ["awe", "oar", "or", "ore"],
    ["aye", "eye", "i"],
]
homophones.add_range(homophone_groups)
print("Words in the dictionary:", homophones.count)

# Exporting homophones to a file
homophones.export_dictionary("./Homophones.dat")

# Importing homophones from a file
homophones.import_dictionary("./Homophones.dat")
Homophones for 'braid':
brayed
Homophone groups for 'braid':
braid brayed
Words in the dictionary: 7

Download full output

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.