Search Homophones in Files using GroupDocs

Synonyms are words with similar meaning, and Homophones sounds the same but are different in meanings or spellings. We learned to find synonyms in multiple documents using Java. Today, in this article, we’ll see how to search homophones within multiple documents using Java.

The following topics will be covered below:

Java API for Searching Homophones

GroupDocs.Search showcases the Java API GroupDocs.Search for Java that allows finding any word and its homophones within multiple files of any specific folder. It can search the content of various different formats. In addition to finding the homophones, the API supports many other searching techniques which include:

  • Case-Sensitive Search
  • Fuzzy Search
  • Phrase Search
  • Regular Expressions Search
  • Synonym Search
  • Wild Card Search

You can download the JAR file from the releases page or use the latest repository and dependency Maven configurations within your Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-search</artifactId>
        <version>26.9.1</version>
</dependency>

Find Homophones in Multiple Files in Java

The following steps guide how to search homophones in multiple files of a folder in Java.

  • Define the search word query, indexing folder, and the container folder of your files.
  • Create Index with the defined index folder.
  • Add the documents folder to the index.
  • Define the SearchOptions and enable the homophone search using the setUseHomophoneSearch method.
  • Perform the homophones search using search method.
  • Use the properties of the retrieved SearchResult as needed.

The following Java source code finds all the homophones within files of the defined folder. Further, you can also manage your homophone dictionary.

String indexFolder = "c:\\MyIndex\\"; // Specify path to the index folder
String documentsFolder = "c:\\MyDocuments\\"; // Specify path to the folder with documents
String query = "right"; // Specify the search query

// Creating an index in the specified folder
// A new index already contains the default English homophone dictionary
Index index = new Index(indexFolder);

// Indexing documents from the specified folder
index.add(documentsFolder);

// Creating a search options instance
SearchOptions options = new SearchOptions();
options.setUseHomophoneSearch(true); // Enabling the homophone search

// Searching for the query and its homophones
SearchResult result = index.search(query, options);

// Printing the summary
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Occurrences: " + result.getOccurrenceCount());

The classes used in the example are located in the com.groupdocs.search, com.groupdocs.search.options and com.groupdocs.search.results packages.

The output of the above code is as follows:

Query: right
Documents: 2
Occurrences: 17

You can use the homophones search results by following the steps after getting the homophones and their occurrences from each document.

  • Traverse the search results.
  • Get each FoundDocument using the getFoundDocument method.
  • Use the properties of each FoundDocument as required.
  • Now, traverse the fields of FoundDocument by getting FoundDocumentField.
  • Later, from each FoundDocumentField, get all the terms and their occurrences within each document.

The following Java code example prints the homophone search results along with the number of occurrences of each searched term.

// The 'query' variable and the 'result' object are taken from the previous example
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Total occurrences: " + result.getOccurrenceCount());
System.out.println();

for (int i = 0; i < result.getDocumentCount(); i++) {
    // Getting the found document
    FoundDocument document = result.getFoundDocument(i);
    System.out.println("Document: " + document.getDocumentInfo().getFilePath());
    System.out.println("Occurrences: " + document.getOccurrenceCount());

    // Traversing the fields in which the terms were found
    for (int j = 0; j < document.getFoundFields().length; j++) {
        FoundDocumentField field = document.getFoundFields()[j];
        System.out.println("    Field: " + field.getFieldName());
        System.out.println("    Occurrences: " + field.getOccurrenceCount());

        // Printing each found term with the number of its occurrences
        for (int k = 0; k < field.getTerms().length; k++) {
            System.out.println("        " + String.format("%-15s", field.getTerms()[k]) + field.getTermsOccurrences()[k]);
        }
    }
}

The following is the output of the above code example.

Query: right
Documents: 2
Total occurrences: 17

Document: C:/documents/sample.docx
Occurrences: 11
    Field: content
    Occurrences: 11
        right          3
        rite           4
        wright         1
        write          3
Document: C:/documents/sample.txt
Occurrences: 6
    Field: content
    Occurrences: 6
        right          4
        write          2

Search Homophones and Printing Results using Java - Complete Code

The following Java code combines the above steps. Initially, it finds the homophones as per query, and then prints all the occurrences of homophones from each document within the provided folder.

String indexFolder = "c:\\MyIndex\\"; // Specify path to the index folder
String documentsFolder = "c:\\MyDocuments\\"; // Specify path to the folder with documents
String query = "right"; // Specify the search query

// Creating an index in the specified folder
// A new index already contains the default English homophone dictionary
Index index = new Index(indexFolder);

// Indexing documents from the specified folder
index.add(documentsFolder);

// Creating a search options instance
SearchOptions options = new SearchOptions();
options.setUseHomophoneSearch(true); // Enabling the homophone search

// Searching for the query and its homophones
SearchResult result = index.search(query, options);

// Printing the summary
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Total occurrences: " + result.getOccurrenceCount());
System.out.println();

for (int i = 0; i < result.getDocumentCount(); i++) {
    // Getting the found document
    FoundDocument document = result.getFoundDocument(i);
    System.out.println("Document: " + document.getDocumentInfo().getFilePath());
    System.out.println("Occurrences: " + document.getOccurrenceCount());

    // Traversing the fields in which the terms were found
    for (int j = 0; j < document.getFoundFields().length; j++) {
        FoundDocumentField field = document.getFoundFields()[j];
        System.out.println("    Field: " + field.getFieldName());
        System.out.println("    Occurrences: " + field.getOccurrenceCount());

        // Printing each found term with the number of its occurrences
        for (int k = 0; k < field.getTerms().length; k++) {
            System.out.println("        " + String.format("%-15s", field.getTerms()[k]) + field.getTermsOccurrences()[k]);
        }
    }
}

Conclusion

To conclude, you learned how to find the words and their homophones from multiple documents within a specified folder using Java. You can try developing your own Java application for searching homophones using GroupDocs.Search for Java.

Learn more about the Java Search Automation API from the documentation. To experience its features, you can have a look at the available examples on the GitHub repository. Reach us for any query via the forum.

See Also