diff --git a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java index d94cedbd5e9..e61490e23b2 100755 --- a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java +++ b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java @@ -1,7 +1,7 @@ package org.apache.lucene.search.spell; /** - * Copyright 2002-2004 The Apache Software Foundation + * Copyright 2002-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,78 +17,79 @@ package org.apache.lucene.search.spell; */ import org.apache.lucene.index.IndexReader; + import java.util.Iterator; + import org.apache.lucene.index.TermEnum; import org.apache.lucene.index.Term; + import java.io.*; /** - * Lucene Dictionnary + * Lucene Dictionnary + * * @author Nicolas Maisonneuve */ -public class LuceneDictionary -implements Dictionary { - IndexReader reader; - String field; +public class LuceneDictionary implements Dictionary { + IndexReader reader; + String field; - public LuceneDictionary (IndexReader reader, String field) { - this.reader=reader; - this.field=field; + public LuceneDictionary(IndexReader reader, String field) { + this.reader = reader; + this.field = field; + } + public final Iterator getWordsIterator() { + return new LuceneIterator(); + } + + + final class LuceneIterator implements Iterator { + private TermEnum enum; + private Term actualTerm; + private boolean has_next_called; + + public LuceneIterator() { + try { + enum = reader.terms(new Term(field, "")); + } catch (IOException ex) { + ex.printStackTrace(); + } } - public final Iterator getWordsIterator () { - return new LuceneIterator(); + public Object next() { + if (!has_next_called) { + hasNext(); + } + has_next_called = false; + return (actualTerm != null) ? actualTerm.text() : null; } -final class LuceneIterator implements Iterator { - private TermEnum enum; - private Term actualTerm; - private boolean has_next_called; - - public LuceneIterator () { - try { - enum=reader.terms(new Term(field, "")); - } - catch (IOException ex) { - ex.printStackTrace(); - } + public boolean hasNext() { + has_next_called = true; + try { + // if there is still words + if (!enum.next()) { + actualTerm = null; + return false; } - - - public Object next () { - if (!has_next_called) {hasNext();} - has_next_called=false; - return (actualTerm!=null) ? actualTerm.text(): null; + // if the next word are in the field + actualTerm = enum.term(); + String fieldt = actualTerm.field(); + if (fieldt != field) { + actualTerm = null; + return false; } - - - public boolean hasNext () { - has_next_called=true; - try { - // if there is still words - if (!enum.next()) { - actualTerm=null; - return false; - } - // if the next word are in the field - actualTerm=enum.term(); - String fieldt=actualTerm.field(); - if (fieldt!=field) { - actualTerm=null; - return false; - } - return true; - } - catch (IOException ex) { - ex.printStackTrace(); - return false; - } - } - - - public void remove () {}; + return true; + } catch (IOException ex) { + ex.printStackTrace(); + return false; + } } + + public void remove() { + }; + } } diff --git a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java index 9467f965a36..565f0e76632 100755 --- a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java +++ b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/PlainTextDictionary.java @@ -1,7 +1,7 @@ package org.apache.lucene.search.spell; /** - * Copyright 2002-2004 The Apache Software Foundation + * Copyright 2002-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,52 +35,48 @@ import java.io.*; */ public class PlainTextDictionary implements Dictionary { - private BufferedReader in; - private String line; - private boolean has_next_called; + private BufferedReader in; + private String line; + private boolean has_next_called; - public PlainTextDictionary (File file) throws FileNotFoundException { - in=new BufferedReader(new FileReader(file)); + public PlainTextDictionary(File file) throws FileNotFoundException { + in = new BufferedReader(new FileReader(file)); + } + + public PlainTextDictionary(InputStream dictFile) { + in = new BufferedReader(new InputStreamReader(dictFile)); + } + + public Iterator getWordsIterator() { + return new fileIterator(); + } + + + final class fileIterator implements Iterator { + public Object next() { + if (!has_next_called) { + hasNext(); + } + has_next_called = false; + return line; } - public PlainTextDictionary (InputStream dictFile) { - in=new BufferedReader(new InputStreamReader(dictFile)); + public boolean hasNext() { + has_next_called = true; + try { + line = in.readLine(); + } catch (IOException ex) { + ex.printStackTrace(); + line = null; + return false; + } + return (line != null) ? true : false; } - public Iterator getWordsIterator () { - - return new fileIterator(); - } - - - final class fileIterator - implements Iterator { - public Object next () { - if (!has_next_called) { - hasNext(); - } - has_next_called=false; - return line; - } - - - public boolean hasNext () { - has_next_called=true; - try { - line=in.readLine(); - } - catch (IOException ex) { - ex.printStackTrace(); - line=null; - return false; - } - return (line!=null)?true:false; - } - - - public void remove () {}; - } + public void remove() { + }; + } } diff --git a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java index 93be0a6a4fd..9cf1042ed10 100755 --- a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java +++ b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java @@ -26,7 +26,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; -import org.apache.lucene.index.TermEnum; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; @@ -113,7 +112,7 @@ public class SpellChecker { * Suggest similar words (restricted or not of a field of a user index) * @param word String the word you want a spell check done on * @param num_sug int the number of suggest words - * @param IndexReader the indexReader of the user index (can be null see field param) + * @param ir the indexReader of the user index (can be null see field param) * @param field String the field of the user index: if field is not null ,the suggest * words are restricted to the words present in this field. * @param morePopular boolean return only the suggest words that are more frequent than the searched word @@ -214,7 +213,7 @@ public class SpellChecker { private static void add (BooleanQuery q, String k, String v, float boost) { Query tq=new TermQuery(new Term(k, v)); tq.setBoost(boost); - q.add(new BooleanClause(tq, false, false)); + q.add(new BooleanClause(tq, BooleanClause.Occur.SHOULD)); } @@ -222,7 +221,7 @@ public class SpellChecker { * Add a clause to a boolean query. */ private static void add (BooleanQuery q, String k, String v) { - q.add(new BooleanClause(new TermQuery(new Term(k, v)), false, false)); + q.add(new BooleanClause(new TermQuery(new Term(k, v)), BooleanClause.Occur.SHOULD)); } @@ -269,12 +268,10 @@ public class SpellChecker { * @throws IOException */ public void indexDictionnary (Dictionary dict) throws IOException { - - int ng1, ng2; IndexReader.unlock(spellindex); IndexWriter writer=new IndexWriter(spellindex, new WhitespaceAnalyzer(), !IndexReader.indexExists(spellindex)); - writer.mergeFactor=300; - writer.minMergeDocs=150; + writer.setMergeFactor(300); + writer.setMaxBufferedDocs(150); Iterator iter=dict.getWordsIterator(); while (iter.hasNext()) { @@ -328,7 +325,7 @@ public class SpellChecker { private static Document createDocument (String text, int ng1, int ng2) { Document doc=new Document(); - doc.add(Field.Keyword(F_WORD, text)); // orig term + doc.add(new Field(F_WORD, text, Field.Store.YES, Field.Index.UN_TOKENIZED)); // orig term addGram(text, doc, ng1, ng2); return doc; } @@ -341,14 +338,14 @@ public class SpellChecker { String end=null; for (int i=0; i