We recently have discussed, how to get all the synonyms of any word. It would be wonderful if we could locate these synonyms within many different documents. In this article, we will see how to search any word and its synonyms in multiple files using Java.
The following are the topics covered below:
- Java API – Synonym Search
- Find synonyms in documents in Java
- Present synonym search results
- Complete Java code – Search & print synonyms search results
Java API - Search Synonyms in Multiple Files
GroupDocs.Search showcases the Java API GroupDocs.Search for Java. It allows searching words and their synonyms in various multiple files of the specified folder. It supports a long list of different file formats and various search techniques. Some of these features are mentioned below and you can use them in combination to achieve your target:
- Boolean Search
- Case-Sensitive Search
- Highlight Search Results
- Homophone Search
- Phrase Search
- Regular Expressions Search
- Search by Chunks
- Synonym Search
Download or Configure
You may download the JAR file from the releases page, or just get the latest repository and dependency configurations for the pom.xml of your maven-based 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 Synonyms in Multiple Files using Java
Let’s quickly move to search synonyms within files. The following steps show how to search synonyms (words with similar meanings) in files within a folder using Java:
- Define the index folder, the documents folder and the query (the word to search).
- Create an index using defined index folder using Index class.
- Add the documents’ folder to the index.
- Enable the synonym search using SearchOptions.
- Call the search method of Index class and pass the query with search options.
- Print the summary using the properties of the retrieved SearchResult class.
The following source code shows how to find all the synonyms within files using Java:
String indexFolder = "c:\\MyIndex\\"; // Specify path to the index folder
String documentsFolder = "c:\\MyDocuments\\"; // Specify path to the folder with documents
String query = "make"; // Specify the search query
// Creating an index in the specified folder
// A new index already contains the default English synonym 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.setUseSynonymSearch(true); // Enabling the synonym search
// Searching for the query and its synonyms
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 following is the output of the above code:
Query: make
Documents: 2
Occurrences: 22
Printing Synonym Search Results using Java
From the search results obtained in the above step, you can get the information regarding each word and synonym of the search. The following steps present the results in detail after getting all the synonyms and their number of occurrences within each document:
- Firstly, perform the search to get the SearchResult.
- Traverse the search result to work with each FoundDocument.
- Print the respective properties of each FoundDocument.
- Now, extract and then traverse the FoundDocumentField within each FoundDocument.
- Each FoundDocumentField has its terms, occurrences, and other properties in it. Use respective getter.
The following source code displays the result of the synonym search along with the number of occurrences of each searched term in Java.
// 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:
Query: make
Documents: 2
Total occurrences: 22
Document: C:/documents/sample.docx
Occurrences: 6
Field: content
Occurrences: 6
make 1
get 2
cause 1
do 2
Document: C:/documents/sample.txt
Occurrences: 16
Field: content
Occurrences: 16
get 4
cause 1
do 11
Search Synonyms and Printing Results in Java – Complete Code
Let’s combine the above two steps, so here is the complete source code. Firstly, it finds all the synonyms according to the provided query. Then, it prints all the occurrences of every synonym in each document in Java.
String indexFolder = "c:\\MyIndex\\"; // Specify path to the index folder
String documentsFolder = "c:\\MyDocuments\\"; // Specify path to the folder with documents
String query = "make"; // Specify the search query
// Creating an index in the specified folder
// A new index already contains the default English synonym 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.setUseSynonymSearch(true); // Enabling the synonym search
// Searching for the query and its synonyms
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]);
}
}
}
Get a Free API License
You can get a free temporary license in order to use the API without the evaluation limitations.
Conclusion
To summarize, we discussed how to search any word along with its synonym in multiple documents using Java. Most importantly, now you can try developing your own Java App for searching just like GroupDocs.Search App.
Learn more about the Java Search Automation API from the documentation. To experience the features, try examples from the GitHub repository. Feel free to reach us for any query via the forum.