Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active October 3, 2021 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/1cffe6974c43184cb73f49e26af1a7f4 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/1cffe6974c43184cb73f49e26af1a7f4 to your computer and use it in GitHub Desktop.
Find Synonyms of any word & Search Synonyms in Multiples Files and Folders using java
// Printing the Synonym Search results in Java
System.out.println("Query: " + query);
System.out.println("Documents: " + result.getDocumentCount());
System.out.println("Word & Synonym 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 synonyms in multiples files and folders using Java
String indexFolder = "path/indexFolder";
String documentsFolder = "path/documentsFolder";
String query = "make";
// 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.setUseSynonymSearch(true); // Enable Synonym Search
// Search for the word 'make'
// In addition to the word 'make', the synonyms 'do, get, have, ...' 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 & Synonym 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 synonyms in multiples files and folders using Java
String indexFolder = "path/indexFolder";
String documentsFolder = "path/documentsFolder";
String query = "make";
// 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.setUseSynonymSearch(true); // Enable Synonym Search
// Search for the word 'make'
// In addition to the word 'make', the synonyms 'do, get, have, ...' 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 & Synonym Occurrences: " + result.getOccurrenceCount());
// Get groups of synonyms according to different meanings of the same word using Java
String query = "show";
String[][] groups = new Index().getDictionaries().getSynonymDictionary().getSynonymGroups(query);
System.out.println("Synonym groups for " + query +":");
for (String[] group : groups) {
for (String group1 : group) {
System.out.print(group1 + " ");
}
System.out.println();
}
// Get all the synonyms of any word in Java
String query = "show";
String[] synonyms = new Index().getDictionaries().getSynonymDictionary().getSynonyms(query);
System.out.println("Synonyms for " + query +":");
for (String synonym : synonyms) {
System.out.println(synonym);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment