public static float subsumes ( String haystack , final String needle ) { if ( needle == null || needle . length ( ) < 0 || haystack == null || haystack . length ( ) < 0 ) return 0f ; 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 IndexReader reader = open ( idx ) ; Query query = parser . parse ( getNGram ( needle , 3 ) ) ; TopDocs results = new IndexSearcher ( reader ) . search ( query , 1 ) ; float score = 0 ; for ( ScoreDoc hit : results . scoreDocs ) { score += hit . score ; } reader . close ( ) ; idx . close ( ) ; return score ; } catch ( Exception e ) { LOG . warn ( "could not computeSimilarity for " + needle , e ) ; } return 0f ; }
depends on document size but > 0 . 01 should be a good threshold