Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need code for fuzzy search in java ?thank Solution import java.io.IOException;

ID: 3616772 • Letter: I

Question

i need code for fuzzy search in java ?thank

Explanation / Answer

import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.spell.Dictionary; import org.apache.lucene.search.spell.LuceneDictionary; import fuzzy.BKTree; import fuzzy.DistanceImpl; /** * Modified class from Lucene core 3.0 Includes BKTree-basedprocessing * * TODO: BKTree is staticaly cached on first call; if you need toupdate index * you will have to restart application * * TODO: consider integer parameter to QueryParser instead offloat. * * * */ public class FuzzyTermEnumNEW extends FilteredTermEnum { static Map cache = newTreeMap(); private Term searchTerm = null; private final String field; Map termMap; Iterator termIterator; /** the current term */ protected Term currentTerm = null; private final float minimumSimilarity; @Override public boolean next() throws IOException { if (!termIterator.hasNext()) { currentTerm = null; return false; } String termString = termIterator.next(); Term term = new Term(field, termString); currentTerm = term; return true; } @Override public Term term() { return currentTerm; } /** * Constructor for enumeration of all terms from specified * reader which share a prefix oflength * prefixLength withterm and which have a * fuzzy similarity >minSimilarity. *

* After calling the constructor the enumeration is already pointingto the * first valid term if such a term exists. * * @param reader *           Delivers terms. * @param term *           Pattern term. * @param minSimilarity *           Minimum required similarity for terms from the reader. Default *           value is 0.5f. * @param prefixLength *           Length of required common prefix. Default value is 0. * @throws IOException */ public FuzzyTermEnumNEW(IndexReader reader, Term term, floatminSimilarity, int prefixLength) throws IOException { super(); if (minSimilarity >= 1.0f) throw new IllegalArgumentException("minimumSimilarity cannot begreater than or equal to 1"); else if (minSimilarity < 0.0f) throw new IllegalArgumentException("minimumSimilarity cannot beless than 0"); if (prefixLength < 0) throw new IllegalArgumentException("prefixLength cannot be lessthan 0"); this.minimumSimilarity = minSimilarity; this.searchTerm = term; this.field = searchTerm.field(); // TODO: Not the best way... BKTree bkTree = cache.get(field); if (bkTree == null) { synchronized (this) { bkTree = new BKTree(new DistanceImpl()); Dictionary dictionary = new LuceneDictionary(reader, field); Iterator iterator =dictionary.getWordsIterator(); while (iterator.hasNext()) { bkTree.add(iterator.next()); } cache.put(field, bkTree); } } int searchTextLength = searchTerm.text().length(); float threshold = (1 - minimumSimilarity) * searchTextLength; int t = (int) threshold; //if (t>2) { // System.out.println ("t > 2!"); // t=2; //} this.termMap = bkTree.query(term.text(), t); this.termIterator = termMap.keySet().iterator(); } /** * The termCompare method in FuzzyTermEnum uses Levenshtein distanceto * calculate the distance between the given term and the comparingterm. */ @Override protected final boolean termCompare(Term term) { return true; } public final float difference() { int score = termMap.get(currentTerm.text()); if (score == 0) return 1.0f; else return (float) (1 / (float) score); } public final boolean endEnum() { return !termIterator.hasNext(); } public void close() throws IOException { termIterator = null; // call super.close() and let the garbage collector do itswork. super.close(); } }