public static Map<Integer, Float> computeSimilarity(
            final Map<Integer, String> corpus, String needle, int gramLength) {

        if (corpus == null || corpus.isEmpty() || needle == null
                || needle.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
            for (Entry<Integer, String> entry : corpus.entrySet()) {
                Document doc = new Document();
                doc.add(new IntField(ID_FIELD, entry.getKey(), Store.YES));
                doc.add(new TextField(CONTENT_FIELD, entry.getValue(),
                        Store.YES));
                writer.addDocument(doc);
            }
            writer.close();

            // search
            IndexReader reader = open(idx);
            Query query = parser.parse(getNGram(needle, gramLength));
            IndexSearcher searcher = new IndexSearcher(reader);
            TopDocs results = searcher.search(query, 20);

            Map<Integer, Float> ret = newHashMap();
            for (ScoreDoc hit : results.scoreDocs) {
                Document doc = searcher.doc(hit.doc);
                int id = new Integer(doc.get(ID_FIELD));
                ret.put(id, hit.score);
            }
            reader.close();

            // ensure all keys are mapped, add 0 otherwise
            for (Integer corpusId : corpus.keySet()) {
                if (!ret.containsKey(corpusId))
                    ret.put(corpusId, 0f);
            }
            return ret;

        } catch (Exception e) {
            LOG.warn("could not computeSimilarity for " + needle, e);
        }
        return null;
    }
public static Map < Integer , Float > computeSimilarity ( final Map < Integer , String > corpus , String needle , int gramLength ) { if ( corpus == null || corpus . isEmpty ( ) || needle == null || needle . length ( ) == 0 ) return null ; try { RAMDirectory idx = new RAMDirectory ( ) ; IndexWriterConfig iwc = new IndexWriterConfig ( LUCENE_41 , anal ) ; IndexWriter writer = new IndexWriter ( idx , iwc ) ; for ( Entry < Integer , String > entry : corpus . entrySet ( ) ) { Document doc = new Document ( ) ; doc . add ( new IntField ( ID_FIELD , entry . getKey ( ) , Store . YES ) ) ; doc . add ( new TextField ( CONTENT_FIELD , entry . getValue ( ) , Store . YES ) ) ; writer . addDocument ( doc ) ; } writer . close ( ) ; IndexReader reader = open ( idx ) ; Query query = parser . parse ( getNGram ( needle , gramLength ) ) ; IndexSearcher searcher = new IndexSearcher ( reader ) ; TopDocs results = searcher . search ( query , 20 ) ; Map < Integer , Float > ret = newHashMap ( ) ; for ( ScoreDoc hit : results . scoreDocs ) { Document doc = searcher . doc ( hit . doc ) ; int id = new Integer ( doc . get ( ID_FIELD ) ) ; ret . put ( id , hit . score ) ; } reader . close ( ) ; for ( Integer corpusId : corpus . keySet ( ) ) { if ( ! ret . containsKey ( corpusId ) ) ret . put ( corpusId , 0f ) ; } return ret ; } catch ( Exception e ) { LOG . warn ( "could not computeSimilarity for " + needle , e ) ; } return null ; }
@return a map of scores that reflects how similar the needle is to the
corpus