Lingpipe中的spell模块-拼写纠错

来源:互联网 发布:淘宝女装宝贝描述 编辑:程序博客网 时间:2024/05/22 10:51

link:

pai.iteye.com/blog/1428074

基本模型

基本技术工作如下:搜索引擎提供可使用的稳定被用来索引和被训练成为一种语言模型。这种语言模型存储的是短语和短语统计的特征信息。当提交一个查询时,
类src/QuerySpellCheck.java 在模型中寻找与之匹配的字符编辑操作,诸如字符截取,插入,替换,转换和删除等等,这样使查询更好的适用于语言模型。如果
你提交一个查询"Gretski",模型中的数据来源为rec.sport.hockey,这个模型将会把'Gretzky' 推荐出来。

领域相关性
基于字典的拼写校正方案的最大优势是校正结果依赖于搜索索引数据。因此查询词"trt"在法律领域别校正为"tort",在厨餐领域被校正为"tart",在生物信息学
领域被校正为"TRt"。但是在Goole中,没有被纠正的推荐词,因为web域名"trt.com"分别是Thessaly Radio Television 和 Turkiye Radyo Televizyon的缩略形式。

上下文相关校正
Yahoo和Google采用的是上下文相关的校正。例如,查询"frod"(来自于德语的老英文单词,意思是智慧的、有经验的)被建议校正为"ford"(一家汽车公司);然而
查询词"frod baggins"被纠正为"frodo baggins"(一个20世纪编造的虚词)。以上是雅虎的校正结果。Google并不校正"frod baggins",页面返回有大约785条结果
满足它,而有820000条结果满足"frodo baggins"。而且Google也不校正"frdo and frdo baggins"。Amazon表现相似,但Bing纠正frd baggins为ford baggins比
frodo baggins要多。
Lingpipe的模型精确的支持这种上下文相关校正

运行的例子
下载并安装Lingpipe.然后修改路径如下:
Java代码  收藏代码
  1. > cd LING_PIPE/demos/tutorial/querySpellCheck  



使用ant命令
Java代码  收藏代码
  1. ant querySpelcheck  



运行程序在windows上
Java代码  收藏代码
  1. java  
  2. -cp "../../../lingpipe-4.0.0.jar;  
  3.      ../../lib/lucene-core-2.3.0.jar;  
  4.      querySpellCheck.jar"  
  5. QuerySpellCheck  



下面的会话,用户输入的是斜体
Java代码  收藏代码
  1. PHASE I: TRAINING  
  2.      CONFIGURATION:  
  3.      Model File: SpellCheck.model  
  4.      N-gram Length: 5  
  5.      File=..\..\data\rec.sport.hockey\train/52550  
  6.      File=..\..\data\rec.sport.hockey\train/52551  
  7. ...  
  8.      File=..\..\data\rec.sport.hockey\train/55022  
  9. Writing model to file=SpellCheck.model  
  10. Writing lucene index to =lucene  
  11. Reading model from file=SpellCheck.model  
  12.   
  13. PHASE II: CORRECTION  
  14.      Constructing Spell Checker from File  
  15.   
  16. Enter query <RETURN>.  
  17.      Use just <RETURN>b to exit.  
  18.   
  19. >Wayn Gretsky  
  20. Found 0 document(s) that matched query 'Wayn Gretsky':  
  21. Found 43 document(s) matching best alt='Wayne Gretzky':  
  22.   
  23. >Wayne Gretzky  
  24. Found 43 document(s) that matched query 'Wayne Gretzky':  
  25.  No spelling correction found.  
  26.   
  27. >Stanley Cub Plaoofs  
  28. Found 90 document(s) that matched query 'Stanley Cub Plaoofs':  
  29. Found 208 document(s) matching best alt='Stanley Cup Playoffs':  
  30.   
  31. >StanleyCup  
  32. Found 0 document(s) that matched query 'StanleyCup':  
  33. Found 143 document(s) matching best alt='Stanley Cup':  
  34.   
  35. >Stan ley Cup  
  36. Found 132 document(s) that matched query 'Stan ley Cup':  
  37. Found 143 document(s) matching best alt='Stanley Cup':  
  38.   
  39. >  
  40.      Detected empty line.  
  41.      Ending test.  



通过拼写校正后我们发现查询索引文档,返回的文档数量增长的比较显著。尽管纠正的结果不是所有的都正确,但对一些模糊查询往往能得到比较好的结果。
lingpipe的拼写纠错模型与许多其他普通的拼写纠错模型的不同之处在于,它能对查询词进行分词,如查询"Stanleycup"时一个几个单词兼并的词,能被纠正为
"Stan ley Cup".

训练数据采用5-grams;编辑距离字符操作数限制在8到10;训练数据中校正需要250到1000倍左右的次的编辑

开始集成到搜索引擎中
类QuerySpellCheck.java 集成的拼写模块以及lucene搜索引擎
基本数据流:
1.搭建语言模型,拼写纠错模块和Lucene类
2.集成语言模型和Lucene索引
3.命令行上输入查询

搭建Spell Checker
需要几个相关类组合。首先我们需要TrainSpellChecker对象训练搜索引擎索引中的数据。这一部如下代码
Java代码  收藏代码
  1. FixedWeightEditDistance fixedEdit =  
  2.     new FixedWeightEditDistance(MATCH_WEIGHT,  
  3.                                 DELETE_WEIGHT,  
  4.                                 INSERT_WEIGHT,  
  5.                                 SUBSTITUTE_WEIGHT,  
  6.                                 TRANSPOSE_WEIGHT);  
  7.   
  8. NGramProcessLM lm = new NGramProcessLM(NGRAM_LENGTH);  
  9. TokenizerFactory tokenizerFactory  
  10.     = new IndoEuropeanTokenizerFactory();  
  11. TrainSpellChecker sc  
  12.     = new TrainSpellChecker(lm,fixedEdit,  
  13.                             tokenizerFactory);  



工作流自顶向下,通过创建FixedWeightEditDistance对象,它能设置像删除,插入等操作的权重。合理的值已经设置在demo中了,它只能在某种特定数据集中
的纠错效果较好。
下一步就是要创建NGramProcessLM ,搭建字符级别的语言模型在我们的demo中。这时我们需要知道数据集中所有的字符数量。越小的数据量。再者,你能评估
查询和查询纠正的质量
另外我们需要TokenizerFactory对象帮助查询的编辑,这个tokenizer和Lucene中的tokenizer是完全匹配的 ,即是org.apache.lucene.analysis.Analyzer接口
的实现,看下文
上面的对象在类TrainSpellChecker 中聚集到一起,做分词相关的训练

搭建Lucene搜素引擎
Lucene中的类IndexWriter确定了数据写索引的路径,数据分词器,以及新索引是否创建的布尔标志位等等。
Java代码  收藏代码
  1. static final File LUCENE_INDEX_DIR = new File("lucene");  
  2.     static final StandardAnalyzer ANALYZER = new StandardAnalyzer(Version.LUCENE_30);  
  3.     static final String TEXT_FIELD = "text";  
  4. ...  
  5.         FSDirectory fsDir   
  6.             = new SimpleFSDirectory(LUCENE_INDEX_DIR,  
  7.                                     new NativeFSLockFactory());  
  8.         IndexWriter luceneIndexWriter   
  9.             = new IndexWriter(fsDir,  
  10.                               ANALYZER,  
  11.                               IndexWriter.MaxFieldLength.LIMITED);  


...
例子中使用的是Lucene的StandardAnalyzer,它提供了一些标准的Filtersh和大小写转换

文档索引和训练拼写纠错模块
增加 的数据时非常简单,所有的数据需要分别进行语言模型的训练和索引过程
Java代码  收藏代码
  1. String[] filesToIndex = DATA.list();  
  2. for (int i = 0; i < filesToIndex.length; ++i) {  
  3.     System.out.println("     File="  
  4.                        + DATA + "/"  
  5.                        +filesToIndex[i]);  
  6.     File file = new File(DATA,filesToIndex[i]);  
  7.     String charSequence  
  8.         = Files.readFromFile(file);  
  9.     sc.handle(charSequence);  
  10.     Document luceneDoc = new Document();  
  11.     Field textField  
  12.         = new Field(TEXT_FIELD,charSequence,  
  13.                     Field.Store.YES,Field.Index.TOKENIZED);  
  14.     luceneDoc.add(textField);  
  15.     luceneIndexWriter.addDocument(luceneDoc);  
  16. }  
  17.   
  18. System.out.println("Writing model to file="  
  19.                    + MODEL_FILE);  
  20. writeModel(sc,MODEL_FILE);  
  21.   
  22. System.out.println("Writing lucene index to ="  
  23.                    + LUCENE_INDEX_DIR);  
  24. luceneIndexWriter.close();  




一旦我们将文件转换为字符串,训练语言模型为代码sc.handle(charSequence)。训练后的数据加入到Lucene需要一些复杂过程,因为这些结构化的数据以一种
复杂的方式来支持所有的搜索引擎特征的。于是我们创建新的文本对象,添加数据导域名中(查询时需要的field名),然后添加文档到索引中。

下面我们简要阐述下spell checker序列化过程以及Lucne索引写到磁盘上,Lucene关闭和写索引以代码行luceneIndexWriter.close()为准

Java代码  收藏代码
  1. private static void writeModel(TrainSpellChecker sc,  
  2.                                File modelFile)  
  3.     throws IOException {  
  4.   
  5.     FileOutputStream fileOut  
  6.         = new FileOutputStream(modelFile);  
  7.     BufferedOutputStream bufOut  
  8.         = new BufferedOutputStream(fileOut);  
  9.     ObjectOutputStream objOut  
  10.         = new ObjectOutputStream(bufOut);  
  11.   
  12.     // write the spell checker to the file  
  13.     sc.compileTo(objOut);  
  14.   
  15.     Streams.closeOutputStream(objOut);  
  16.     Streams.closeOutputStream(bufOut);  
  17.     Streams.closeOutputStream(fileOut);  
  18. }  



从磁盘上读数据
一旦模型和索引写到磁盘上,我们能读它们,然后纠正查询。这种读模型方法显示了如何兼并相关的Java I/O 序列化连载类。我们通过请求产生纠错结果的
CompiledSpellChecker从而改变类。主要原因是编译的语言模型要比未编译版本快得多


// read compiled model from model file
Java代码  收藏代码
  1. System.out.println("Reading model from file="  
  2.                    + MODEL_FILE);  
  3. CompiledSpellChecker compiledSC  
  4.     = readModel(MODEL_FILE);  
  5. IndexSearcher luceneIndex  
  6.     = new IndexSearcher(fsDir);  
  7.   
  8. System.out.print(TEST_INTRO);  
  9. testModelLoop(compiledSC,luceneIndex);  


下载Lucene相似度改变了IndexSearcher的核心类,我们开始纠正查询了。上面最后一一行代码就是命令行的插查询纠错循环。

查询纠错循环

一个搜索引擎的基本部署需要比我们demo更多的功能,比如检索文档的排序。但是下面我们要尽量得到更好的拼写纠错模块,比想象中的部署要全面,
这个方法是一个无限循环命令行代码。流程是得到查询,流程是输入一个查询,运行Lucene并得到结果。
Java代码  收藏代码
  1. static void testModelLoop(SpellChecker sc,  
  2.                           IndexSearcher luceneIndex)  
  3.         throws IOException, ParseException {  
  4.   
  5.     InputStreamReader isReader  
  6.         = new InputStreamReader(System.in);  
  7.     BufferedReader bufReader  
  8.         = new BufferedReader(isReader);  
  9.   
  10.     QueryParser queryParser   
  11.         = new QueryParser(Version.LUCENE_30,  
  12.                           TEXT_FIELD,  
  13.                           ANALYZER);  
  14.   
  15.     while (true) {  
  16.         // collect query or end if null  
  17.         System.out.print(">");  
  18.         System.out.flush();  
  19.         String queryString = bufReader.readLine();  
  20.         if (queryString == null || queryString.length() == 0)  
  21.             break;  
  22.   
  23.         Query query = queryParser.parse(queryString);  
  24.         TopDocs results = searcher.search(query,MAX_HITS);  
  25.         System.out.println("Found "  
  26.             + results.totalHits  
  27.             + " document(s) that matched query '"  
  28.             + query + "':");  
  29.   
  30.         // compute alternative spelling  
  31.         String bestAlternative = sc.didYouMean(queryString);  
  32.   
  33.         Query alternativeQuery  
  34.             = queryParser.parse(bestAlternative);  
  35.         TopDocs results2  
  36.             = luceneIndex.search(alternativeQuery,MAX_HITS);  
  37.   
  38.         System.out.println("Found " + results2.totalHits  
  39.             + " document(s) matched best alternate '"  
  40.             + bestAlternative + "':");  
  41.     }  
  42. }  



查阅更多相关代码,处理搞乱I/O和输入空查询时而破坏循环。首先我们使用Lucnene中的QueryParser产生查询。这样携带参数并保护查询串,然后Lucene索引的域被检索
(TEXT_FIELD),然后Analyzer对查询进行分词处理(UCENE_TOKENIZER),同样的采用相同的分词器对索引数据进行处理。

下一步我们收集匹配查询的文档Hits hits = luceneIndex.search(origQuery)。我们需要从Lucene中获取文档数量,以及Hits类提供的方法即及时每个文档与查询的相似度得分。

最后我们得到拼写纠错结果代码 String bestAlternative = sc.didYouMean(query);它采用了语言模型,我们通过有关查询的文本中的简单编辑距离发现更好的匹配。
如果纠错结果中没有更高得分的查询,那边系统就返回原始查询串

协调和部署
部署好的查询纠错模块需要比这个Demo系统更加完善,比如系统需要能兼容各种版本。协调模型有点复杂,另外模型训练数据量大小也是个问题。通常理想情况下,你需要
查询纠错系统评测集,结果好坏来自于实际不同使用者,你可以选择合适参数来协调系统。
0 0