I have the following working Java code for searching for a word against a list of words and it works perfectly and as expected:
public class Levenshtein {
private int[][] wordMartix;
public Set similarExists(String searchWord) {
int maxDistance = searchWord.length();
int curDistance;
int sumCurMax;
String checkWord;
// preventing double words on returning list
Set<String> fuzzyWordList = new HashSet<>();
for (Object wordList : Searcher.wordList) {
checkWord = String.valueOf(wordList);
curDistance = calculateDistance(searchWord, checkWord);
sumCurMax = maxDistance + curDistance;
if (sumCurMax == checkWord.length()) {
fuzzyWordList.add(checkWord);
}
}
return fuzzyWordList;
}
public int calculateDistance(String inputWord, String checkWord) {
wordMartix = new int[inputWord.length() + 1][checkWord.length() + 1];
for (int i = 0; i <= inputWord.length(); i++) {
wordMartix[i][0] = i;
}
for (int j = 0; j <= checkWord.length(); j++) {
wordMartix[0][j] = j;
}
for (int i = 1; i < wordMartix.length; i++) {
for (int j = 1; j < wordMartix[i].length; j++) {
if (inputWord.charAt(i - 1) == checkWord.charAt(j - 1)) {
wordMartix[i][j] = wordMartix[i - 1][j - 1];
} else {
int minimum = Integer.MAX_VALUE;
if ((wordMartix[i - 1][j]) + 1 < minimum) {
minimum = (wordMartix[i - 1][j]) + 1;
}
if ((wordMartix[i][j - 1]) + 1 < minimum) {
minimum = (wordMartix[i][j - 1]) + 1;
}
if ((wordMartix[i - 1][j - 1]) + 1 < minimum) {
minimum = (wordMartix[i - 1][j - 1]) + 1;
}
wordMartix[i][j] = minimum;
}
}
}
return wordMartix[inputWord.length()][checkWord.length()];
}
}
Right now when I search for a word like job
it returns a list:
Output
joborienterede
jobannoncer
jobfunktioner
perjacobsen
jakobsen
jobprofiler
jacob
jobtitler
jobbet
jobdatabaserne
jobfunktion
jakob
jobs
studenterjobber
johannesburg
jobmuligheder
jobannoncerne
jobbaser
job
joberfaringer
As you can see the output has a lot of related words but has also non-related ones like jakob
, jacob
etc., which is correct regarding the Levenshtein formula, but I would like to build further and write a method that can fine-tune my search so I can get more relevant and related words.
I have worked a few hours on it and lost sight of creativity.
My Question: Is it possible to fine-tune the existing method to return relevant/related words Or should I take another approach Or??? in all cases YES or NO, I appreciated it if can get input and inspiration regarding improving searching results.
UPDATE
After asking this question a long time back I have not really found a solution and I am back to it because it is the time when I need a useful answer, it is fine to supply the answer with JAVA code samples, but what is most important is a detailed answer with a description of available methods and approaches used to index best and most relevant search results and ignoring none appropriate words. I know this is an open and endless area, but I need the inspiration to start somewhere.
Note: The oldest answer right now is based on one of the comment inputs and is not helpful (useless), it just sorts the distance, which does not mean getting better search results/quality.
So I did distance sorting and the results was like this:
job
jobs
jacob
jakob
jobbet
jakobsen
jobbaser
jobtitler
jobannoncer
jobfunktion
jobprofiler
perjacobsen
johannesburg
jobannoncerne
joberfaringer
jobfunktioner
jobmuligheder
jobdatabaserne
joborienterede
studenterjobber
so the word jobbaser is relevant and jacob/jakob is not relevant, but the distance for jobbaser is more considerable than jacob/jakob. So that did not really help.
General feedback regarding answers
- @SergioMontoro, it solves almost the problem.
- @uSeemSurprised, it solves the problem but needs continual manipulation.
- @Gene concept is excellent, but it is relaying on an external URL.
Thanks I would like to personally thank all of you who contributed to this question, I have got nice answers and useful comments.
Special thanks to answers from @SergioMontoro, @uSeemSurprised, and @Gene, those are different but valid and useful answers.
@D.Kovács is pointing out some interesting solutions.
I wish I could give a bounty to all of those answers. Chose one answer and give it a bounty, that does not mean the other answers are not valid, but that only means that the particular answer I chose was useful for me.