コード例 #1
0
        static public void query(Sentence[] sentences, string query, string indexPath = "luceneIndex")
        {
            Dictionary <string, Sentence> map = new Dictionary <string, Sentence>();

            foreach (Sentence s in sentences)
            {
                if (!map.ContainsKey(s.sentnece))
                {
                    map.Add(s.sentnece, s);
                }
            }

            java.io.File    indexDir       = new java.io.File(indexPath);
            FSDirectory     indexFSDir     = new SimpleFSDirectory(indexDir);
            IndexSearcher   searcher       = new IndexSearcher(IndexReader.open(indexFSDir));
            EnglishAnalyzer luceneAnalyzer = new EnglishAnalyzer(org.apache.lucene.util.Version.LUCENE_35);
            QueryParser     qp             = new QueryParser(org.apache.lucene.util.Version.LUCENE_35, "text", luceneAnalyzer);

            Query   q     = qp.parse(query);
            TopDocs tdocs = searcher.search(q, 99999999);

            ScoreDoc[] sdocs = tdocs.scoreDocs;
            for (int i = 0; i < sdocs.Length; i++)
            {
                ScoreDoc sd  = sdocs[i];
                Document res = searcher.doc(sd.doc);

                string docText = res.get("text");
                float  score   = sd.score;

                map[docText].lucene = score;
            }
            searcher.close();
        }