### Question:
LuceneHelper { public static Map<Integer, Float> computeSimilarity2( final Map<Integer, String> queries, String haystack, int gramLength) { if (queries == null || queries.isEmpty() || haystack == null || haystack.length() == 0) return null; try { RAMDirectory idx = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_41, anal); IndexWriter writer = new IndexWriter(idx, iwc); Document doc = new Document(); doc.add(new TextField(CONTENT_FIELD, haystack, Store.YES)); writer.addDocument(doc); writer.close(); Map<Integer, Float> ret = newHashMap(); IndexReader reader = open(idx); for (Entry<Integer, String> query : queries.entrySet()) { Query q = parser.parse(getNGram( query.getValue().replaceAll("[^A-Za-z0-9]", " ") .replaceAll(" +", " "), gramLength)); IndexSearcher searcher = new IndexSearcher(reader); TopDocs results = searcher.search(q, 1); if (results.totalHits == 1) ret.put(query.getKey(), results.getMaxScore()); else ret.put(query.getKey(), 0f); } reader.close(); return ret; } catch (Exception e) { LOG.warn("could not computeSimilarity", e); } return null; }  static Map<Integer, Float> computeSimilarity(
            final Map<Integer, String> corpus, String needle, int gramLength); static Map<Integer, Float> computeSimilarity2(
            final Map<Integer, String> queries, String haystack, int gramLength); static float subsumes(String haystack, final String needle);  }### Answer:
@Test public void testSimilarity2() { Map<Integer, String> queries = newHashMap(); queries.put( 1, "This is a remarkably good book on graphical models. Filled with lots of examples and really takes its time trying to build intuition. Seriously, consider this book if the idea of going through Koller’s 1400 page tome feels intimidating. The book is more gentle than the others listed here but still manages a remarkable amount. Note it says Bayesian in the title so non-Bayesian algorithms and models are mostly ignored. You will need to supplement with a book like EoSL or Murphy’s book. There is a free copy on his website so be sure to preview and see if its at the right level for you."); queries.put(2, "This is remarkably good book on graphical models"); queries.put(3, "This is remarkably good book on models"); queries.put(4, "what a mistake to go fishing last saturday"); Map<Integer, Float> similarities = LuceneHelper.computeSimilarity2( queries, "This is a remarkably good book on graphical models", 3); float s1 = similarities.get(1), s2 = similarities.get(2), s3 = similarities .get(3), s4 = similarities.get(4); LOG.debug("\ns1 {}\ns2 {}\ns3 {}\ns4 {}", new Object[] { s1, s2, s3, s4 }); assertTrue("we have a match", s1 > 0); assertTrue("we have a match", s2 > 0); assertTrue("we have a match", s3 > 0); assertTrue("no match", s4 == 0); assertTrue(s2 > s1); assertTrue(s2 > s3); }