pinyinReader:
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();
// }
}
}
Searcher:
package jim.pinyinReader;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
class Search {
Directory directory = null;//存储方式
String indexPath = "index";//引索存放的目录
IndexReader reader = null;//读入引索
IndexSearcher searcher = null;//确定搜索对象
QueryParser parser = null;//用于确定搜索时的引索的版本以及分词器
Query query = null;//记录要搜索的词语
TopDocs tds = null;//记录搜索后返回的结果
Document document = null;//存放搜索结果以便于提取结果
ScoreDoc[] sds = null;//存放TopDocs传来的内容(搜索结果)
public Search(String KeyWord){
try {
directory = FSDirectory.open(new File(indexPath));
} catch (IOException e) {
System.out.println("创建Directory时发生错误!");
e.printStackTrace();
}//创建directory,其储存方式为在硬盘上储存
try {
reader = IndexReader.open(directory);
} catch (CorruptIndexException e) {
System.out.println("创建IndexReader时发生错误!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("创建IndexReader时发生错误!");
e.printStackTrace();
}
searcher = new IndexSearcher(reader);
parser = new QueryParser(Version.LUCENE_35,"pinyin",new IKAnalyzer());
try {
query = parser.parse(KeyWord); // 在此输入拼音
} catch (ParseException e) {
System.out.println("query = parser.parse(\"keyword\")时发生错误");
e.printStackTrace();
}
try {
tds = searcher.search(query,5);
} catch (IOException e) {
System.out.println("std = searcher.search(query,5);时发生错误");
e.printStackTrace();
}
sds = tds.scoreDocs;
System.out.println("一共搜索到: "+sds.length+" 条");
if(sds.length != 0){
for( ScoreDoc sd:sds){
try {
document = searcher.doc(sd.doc);
} catch (CorruptIndexException e) {
System.out.println("document = searcher.doc(sd.doc);时发生错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("document = searcher.doc(sd.doc);时发生错误");
e.printStackTrace();
}
System.out.println(document.get("pinyin")+"["+document.get("ciyu")+"]");
}
}
else
System.out.println("The word you enter can't be found!");
try {
reader.close();
} catch (IOException e) {
System.out.println("关闭reader时发生错误!");
e.printStackTrace();
}
System.out.println("Finished");
}
}
public class Searcher {
public static void main(String [] args){
new Search("wo");
}
}
今天的收获:
今天把输拼音出汉字搞定了,虽然不是很准确.
今天的不足:
没用弄好汉字转拼音