Lucene+Sen組合實現簡單的查詢引擎(日本語對應)

來源:互聯網
上載者:User

相關資料:http://ultimania.org/sen/

perl5.6以降:http://www.activestate.com/store/download.aspx?prdGUID=81fbce82-6bd5-49bc-a915-08d58c2648ca
ant1.5以降:http://ant.apache.org/bindownload.cgi
注意)ant -Dperl.bin=D:/Java/Perl/bin/perl.exe
:https://sen.dev.java.net/servlets/ProjectDocumentList?folderID=755&expandFolder=755&folderID=0

代碼如下:

package demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.ja.JapaneseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

public class HelloLucene ...{

private static final String FIELD_CONTENT = "content";
    
    private static final String SEN_XML_PATH = "D:/Java/sen-1.2.2.1/conf/sen.xml";
    
    private static final Directory directory = new RAMDirectory();

    private static final Analyzer analyzer = new JapaneseAnalyzer(SEN_XML_PATH);

    private static final QueryParser qp = new QueryParser( FIELD_CONTENT, analyzer );



    private static final String[] contents = ...{

        "カツオはサザエの弟", "サザエはワカメの姉", "ワカメはカツオの妹",

        "カツオは長男", "サザエは長女", "ワカメは次女",

        "マスオはサザエの夫", "波平は舟の夫", "タラちゃんのパパはマスオ",

        "サザエとマスオは夫婦", "波平はタラちゃんの祖父", "舟はカツオの母",

        "マスオはカツオの義兄", "カツオはタラちゃんの叔父", "舟はワカメの母"

    };



    public static void main( String[] args ) throws IOException, ParseException ...{

        makeIndex();

        backupIndex();

        BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );

        String q = null;

        while( q == null || !q.equals( "q" ) )...{

            System.out.print( " 検索質問(qで終了)> " );

            System.out.flush();

            q = br.readLine();

            if( !q.equals( "q" ) )

                searchIndex( q );

        }

        br.close();

        if( directory != null )

            directory.close();

    }



    private static void makeIndex() throws IOException ...{
        
        IndexWriter writer = new IndexWriter( directory, analyzer, true );

        for( int i = 0; i < contents.length; i++ )...{

            Document doc = new Document();

            doc.add( new Field( FIELD_CONTENT, contents[i], Field.Store.YES, Field.Index.TOKENIZED ) );

            writer.addDocument( doc );

        }

        writer.close();

    }



    private static void searchIndex( final String q ) throws IOException, ParseException ...{

        IndexSearcher searcher = new IndexSearcher( directory );

        Query query = qp.parse( q );

        Hits hits = searcher.search( query );

        int length = hits.length();

        System.out.println( Integer.toString( length ) + "件ヒットしました。" );

        for( int i = 0; i < length; i++ )...{

            Document doc = hits.doc( i );

            System.out.println( " " + doc.get( FIELD_CONTENT ) );

        }

        searcher.close();

    }

    

    private static void backupIndex() throws IOException...{

        Directory persistent = FSDirectory.getDirectory( "index" );

        Directory.copy( directory, persistent, false );

        persistent.close();

    }
}

參考資料:

http://lucene.jugem.jp/?month=200703

http://www.geocities.jp/another4000yrs/lucene/

http://repository.kulib.kyoto-u.ac.jp/dspace-memo/japanization.html

http://lucene.jugem.jp/?eid=53

http://lucene.jugem.jp/?eid=97

http://www.getopt.org/luke/

中文相關:

http://www.javaeye.com/topic/49441?page=6

http://blog.cnblog.org/archives/2005/07/luceneaecee.html

http://blog.csdn.net/ugg/archive/2007/03/06/1522624.aspx

http://www.ideagrace.com/html/doc/2007/02/07/08630.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.