Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active January 13, 2022 06:33
Show Gist options
  • Save GroupDocsGists/e150579822915caefa017952b0f19b3d to your computer and use it in GitHub Desktop.
Save GroupDocsGists/e150579822915caefa017952b0f19b3d to your computer and use it in GitHub Desktop.
Search Homophones of any word in Multiples Files and Folders using Java
// Printing the Homophone Search results in Java
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Word & Homophone Occurrences: " + result.getOccurrenceCount());
// Traverse the Documents
for (int i = 0; i < result.getDocumentCount(); i++) {
FoundDocument document = result.getFoundDocument(i);
System.out.println("Document: " + document.getDocumentInfo().getFilePath());
System.out.println("Occurrences: " + document.getOccurrenceCount());
// Traverse the found fields
for (FoundDocumentField field : document.getFoundFields()) {
System.out.println("\tField: " + field.getFieldName());
System.out.println("\tOccurrences: " + document.getOccurrenceCount());
// Printing found terms
if (field.getTerms() != null) {
for (int k = 0; k < field.getTerms().length; k++) {
System.out.println("\t\t" + field.getTerms()[k] + "\t - \t" + field.getTermsOccurrences()[k]);
}
}
}
}
// Search homophones in multiples files and folders using Java
String indexFolder = "path/indexFolder";
String documentsFolder = "path/documentsFolder";
String query = "right";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
index.add(documentsFolder);
// Creating a search options object
SearchOptions options = new SearchOptions();
options.setUseHomophoneSearch(true); // Enable Homophone Search
// Search for the word 'right'
// In addition to the word 'right', the homophones 'rite, write, wright, ...' will also be searched
SearchResult result = index.search(query, options);
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Word & Homophone Occurrences: " + result.getOccurrenceCount());
for (int i = 0; i < result.getDocumentCount(); i++) {
FoundDocument document = result.getFoundDocument(i);
System.out.println("Document: " + document.getDocumentInfo().getFilePath());
System.out.println("Occurrences: " + document.getOccurrenceCount());
for (FoundDocumentField field : document.getFoundFields()) {
System.out.println("\tField: " + field.getFieldName());
System.out.println("\tOccurrences: " + document.getOccurrenceCount());
// Printing found terms
if (field.getTerms() != null) {
for (int k = 0; k < field.getTerms().length; k++) {
System.out.println("\t\t" + field.getTerms()[k] + "\t - \t" + field.getTermsOccurrences()[k]);
}
}
}
}
// Search homophones in multiples files and folders using Java
String indexFolder = "path/indexFolder";
String documentsFolder = "path/documentsFolder";
String query = "right";
// Creating an index in the specified folder
Index index = new Index(indexFolder);
index.add(documentsFolder);
// Creating a search options object
SearchOptions options = new SearchOptions();
options.setUseHomophoneSearch(true); // Enable Homophone Search
// Search for the word 'right'
// In addition to the word 'right', the homophones 'rite, write, wright, ...' will also be searched
SearchResult result = index.search(query, options);
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Word & Homophone Occurrences: " + result.getOccurrenceCount());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment