2016-04-23 110 views
1

我是lucene的新手,我試圖做查詢擴展。查詢擴展lucene

我已經提到了這兩個帖子(first,second),並且我已經設法以適合版本6.0.0的方式重新使用代碼,因爲前面的代碼已被棄用。

問題是,我沒有得到結果,或者我沒有正確訪問結果(擴展查詢)。

這裏是我的代碼:

import com.sun.corba.se.impl.util.Version; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.StringReader; 
import java.io.UnsupportedEncodingException; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 
import java.text.ParseException; 
import org.apache.lucene.analysis.Analyzer; 
import org.apache.lucene.analysis.TokenStream; 
import org.apache.lucene.analysis.Tokenizer; 
import org.apache.lucene.analysis.core.LowerCaseFilter; 
import org.apache.lucene.analysis.standard.ClassicTokenizer; 
import org.apache.lucene.analysis.standard.StandardFilter; 
import org.apache.lucene.analysis.synonym.SynonymFilter; 
import org.apache.lucene.analysis.synonym.SynonymMap; 
import org.apache.lucene.analysis.synonym.WordnetSynonymParser; 
import org.apache.lucene.analysis.util.CharArraySet; 
import org.apache.lucene.util.*; 


public class Graph extends Analyzer 
{ 

    protected static TokenStreamComponents createComponents(String fieldName, Reader reader) throws ParseException{ 
     System.out.println("1"); 
    // TODO Auto-generated method stub 
    Tokenizer source = new ClassicTokenizer(); 

    source.setReader(reader); 
    TokenStream filter = new StandardFilter(source); 

    filter = new LowerCaseFilter(filter); 
    SynonymMap mySynonymMap = null; 

    try { 

     mySynonymMap = buildSynonym(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    filter = new SynonymFilter(filter, mySynonymMap, false);  

    return new TokenStreamComponents(source, filter); 

} 

private static SynonymMap buildSynonym() throws IOException, ParseException 
{ System.out.print("build"); 
    File file = new File("wn\\wn_s.pl"); 

    InputStream stream = new FileInputStream(file); 

    Reader rulesReader = new InputStreamReader(stream); 
    SynonymMap.Builder parser = null; 
    parser = new WordnetSynonymParser(true, true, new StandardAnalyzer(CharArraySet.EMPTY_SET)); 
    System.out.print(parser.toString()); 
    ((WordnetSynonymParser) parser).parse(rulesReader); 
    SynonymMap synonymMap = parser.build(); 
    return synonymMap; 
} 

public static void main (String[] args) throws UnsupportedEncodingException, IOException, ParseException 
{ 
Reader reader = new FileReader("C:\\input.txt"); // here I have the queries that I want to expand 
TokenStreamComponents TSC = createComponents("" , new StringReader("some text goes here")); 
**System.out.print(TSC); //How to get the result from TSC????** 
} 

    @Override 
    protected TokenStreamComponents createComponents(String string) 
    { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 

請建議的方式來幫助我訪問擴展查詢!

回答

1

那麼,你只是想弄清楚如何遍歷主要方法中的TokenStreamComponents的條款?

事情是這樣的:

TokenStreamComponents TSC = createComponents("" , new StringReader("some text goes here")); 
TokenStream stream = TSC.getTokenStream(); 
CharTermAttribute termattr = stream.addAttribute(CharTermAttribute.class); 
stream.reset(); 
while (stream.incrementToken()) { 
    System.out.println(termattr.toString()); 
} 
+0

感謝femtoRgon這正是我想要的! – Manar