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 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.
fromgroupdocs.searchimportIndex# Creating or opening an index in the specified folderindex=Index("./index")homophones=index.dictionaries.homophone_dictionary# Getting homophones for the word 'braid'; the letter case of the word is ignoredwords=homophones.get_homophones("Braid")print("Homophones for 'braid':")forwordinwords:print(word)# Getting groups of homophones to which the word 'braid' belongsgroups=homophones.get_homophone_groups("braid")print("Homophone groups for 'braid':")forgroupingroups:print(" ".join(group))ifhomophones.count>0:# Removing all homophones from the dictionaryhomophones.clear()# Adding homophones to the dictionaryhomophone_groups=[["awe","oar","or","ore"],["aye","eye","i"],]homophones.add_range(homophone_groups)print("Words in the dictionary:",homophones.count)# Exporting homophones to a filehomophones.export_dictionary("./Homophones.dat")# Importing homophones from a filehomophones.import_dictionary("./Homophones.dat")
Homophones for 'braid':
brayed
Homophone groups for 'braid':
braid brayed
Words in the dictionary: 7