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 {
            // index the cleaned text
            RAMDirectory idx = new RAMDirectory();
            IndexWriterConfig iwc = new IndexWriterConfig(LUCENE_41, anal);
            IndexWriter writer = new IndexWriter(idx, iwc);

            // index
            Document doc = new Document();
            doc.add(new TextField(CONTENT_FIELD, haystack, Store.YES));
            writer.addDocument(doc);
            writer.close();

            // search
            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));
                // less discriminative
                // Query q = parser.parse(query.getValue()
                // .replaceAll("[^A-Za-z0-9]", " ").replaceAll(" +", " "));
                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;
    }
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 ; }
@return a score that reflects how similar the queryStr is to the corpus