Synonym search using Java

Hi,

I am using GroupDocs.search for java for synonym search.

My requirement is that I have a data set, with all the synonyms for a word and I have to return the data in case any synonym is matched
Example:
small,little,tiny
speed,quick,fast

In case I search for small or little I should get small in the output.

I have tried below code:
public class Searching {
public static void synonymSearch() {
Index index = new Index(Utilities.INDEX_PATH);
index.getDictionaries().getSynonymDictionary().importDictionary(Utilities.SYNONYM_FILE_PATH);
index.addToIndex(Utilities.DOCUMENTS_PATH);
SearchParameters parameters = new SearchParameters();
parameters.setUseSynonymSearch(true);
parameters.setSearchDocumentFilter(null);

	String searchString = "virtual";
	SearchResults searchResults = index.search(searchString, parameters);
	// Print found results in the console
	for (DocumentResultInfo documentResultInfo : searchResults) {
		System.out.println(documentResultInfo.getFileName() + "\n");
	}
}

}

But I am getting filename in the output instead of data.

Please help me.

Thanks,
Madhur

1 Like

@madhur2928,

You can get data as well. Below is the code to achieve this:

SearchResults searchResults = index.search(searchString, parameters);
index.highlightInText("D:\\highlighted.html", searchResults.get_Item(0)); 

This is what highlighted.zip (1.1 KB) you will get in the output. If you see the code below,

searchResults.get_Item(0)

0 is the file hit number. For example, if there are 10 files, and search result hit is found in the very first file then its hit number is 0.

Thanks for the reply.

My problem statement is that I have a data set of synonyms in csv format.
Example:
big,large,huge
small,tiny,less

User will provide the input, I have to search for it inside my data set and have to return first matching element.

example if I search for big or large my output will be big

Is this possible?? If yes please help me in this.

Thanks,
Madhur

@madhur2928,

We are investigating this scenario. Your investigation ticket ID is SEARCHJAVA-97. As we have any further update, you’ll be notified.

@madhur2928,

We have an update on SEARCHJAVA-97. Synonym Search searches for any of a synonym from a group. As you want to search always the first word from a group for any input word contained in this group. GroupDocs.Search for Java doesn’t provide such feature. However, you can use HashMap<String, String> class to get the first word for any word contained in a group.
For example, if you have a group of 4 words: w1, w2, w3, w4. You could do something like this:

HashMap<String, String> map = new HashMap<String, String>();
map.put(w2, w1);
map.put(w3, w1);
map.put(w4, w1);
...
String word = map.get(query);
if (word == null) word = query;

Hope it will be helpful.