package jim.pinyinReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class pinyinReader {
Document doc = null;
Field field = null;
IndexWriter writer = null;
public void ReadPY(){
BufferedReader myIn = null;
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer());//选择lucene的版本以及分词器的版本
try {
Directory directory = FSDirectory.open(new File("index"));//创建directory,其储存方式为在
writer = new IndexWriter(directory,iwc);
} catch (CorruptIndexException e1) {
e1.printStackTrace();
} catch (LockObtainFailedException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}//创建引索器
try
{
myIn = new BufferedReader(new FileReader("test.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("File not be found!!");
}
int s = 0;
try {
while((s = myIn.read()) != -1)
{
if(s != '\n')
if(!((s < 'a'||s>'z')&& (s != ' ')&&(s != '\'')))
{
sb1.append((char)s); //添加拼音
}
else
{
sb2.append((char)s); //添加汉字
}
else
{
System.out.println("换行");
doc = new Document();//创建索引文件
System.out.println("sb1: "+sb1);
field = new Field("pinyin",sb1.toString(),Field.Store.YES,Index.ANALYZED);//创建索引
doc.add(field);
sb1.delete(0,sb1.capacity()-1);
System.out.println("sb2: "+sb2);
field = new Field("ciyu",sb2.toString(),Field.Store.YES,Index.NOT_ANALYZED);//创建索引
doc.add(field);
sb2.delete(0,sb2.capacity()-1);
writer.addDocument(doc);
}
}
writer.close();//关闭索引器
} catch (IOException e) {
System.out.println("Read Error");
e.printStackTrace();
}
try {
myIn.close();
} catch (IOException e) {
System.out.println("Close Error");
e.printStackTrace();
}
}
public void check() throws IOException{ //检查索引是否被正确建立(打印索引)
Directory directory = FSDirectory.open(new File("index"));//创建directory,其储存方式为在
directory = FSDirectory.open(new File("index"));
IndexReader reader = IndexReader.open(directory);
for(int i = 0;i<reader.numDocs();i++){
System.out.println(reader.document(i));
}
}
public static void main(String args[]){
pinyinReader reader = new pinyinReader();
reader.ReadPY();
// try {
// reader.check();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class pinyinReader {
Document doc = null;
Field field = null;
IndexWriter writer = null;
public void ReadPY(){
BufferedReader myIn = null;
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer());//选择lucene的版本以及分词器的版本
try {
Directory directory = FSDirectory.open(new File("index"));//创建directory,其储存方式为在
writer = new IndexWriter(directory,iwc);
} catch (CorruptIndexException e1) {
e1.printStackTrace();
} catch (LockObtainFailedException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}//创建引索器
try
{
myIn = new BufferedReader(new FileReader("test.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("File not be found!!");
}
int s = 0;
try {
while((s = myIn.read()) != -1)
{
if(s != '\n')
if(!((s < 'a'||s>'z')&& (s != ' ')&&(s != '\'')))
{
sb1.append((char)s); //添加拼音
}
else
{
sb2.append((char)s); //添加汉字
}
else
{
System.out.println("换行");
doc = new Document();//创建索引文件
System.out.println("sb1: "+sb1);
field = new Field("pinyin",sb1.toString(),Field.Store.YES,Index.ANALYZED);//创建索引
doc.add(field);
sb1.delete(0,sb1.capacity()-1);
System.out.println("sb2: "+sb2);
field = new Field("ciyu",sb2.toString(),Field.Store.YES,Index.NOT_ANALYZED);//创建索引
doc.add(field);
sb2.delete(0,sb2.capacity()-1);
writer.addDocument(doc);
}
}
writer.close();//关闭索引器
} catch (IOException e) {
System.out.println("Read Error");
e.printStackTrace();
}
try {
myIn.close();
} catch (IOException e) {
System.out.println("Close Error");
e.printStackTrace();
}
}
public void check() throws IOException{ //检查索引是否被正确建立(打印索引)
Directory directory = FSDirectory.open(new File("index"));//创建directory,其储存方式为在
directory = FSDirectory.open(new File("index"));
IndexReader reader = IndexReader.open(directory);
for(int i = 0;i<reader.numDocs();i++){
System.out.println(reader.document(i));
}
}
public static void main(String args[]){
pinyinReader reader = new pinyinReader();
reader.ReadPY();
// try {
// reader.check();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}