formatting/indenting changes only

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1023283 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-10-16 13:48:51 +00:00
parent f0e36d867b
commit d99c26f81f
3 changed files with 155 additions and 155 deletions

View File

@ -20,49 +20,49 @@ package org.apache.solr.spelling;
import org.apache.solr.common.util.NamedList;
public class SpellCheckCollation implements Comparable<SpellCheckCollation> {
private NamedList<String> misspellingsAndCorrections;
private int hits;
private int internalRank;
private String collationQuery;
public int compareTo(SpellCheckCollation scc) {
int c = new Integer(internalRank).compareTo(scc.internalRank);
if (c == 0) {
return collationQuery.compareTo(scc.collationQuery);
}
return c;
}
private NamedList<String> misspellingsAndCorrections;
private int hits;
private int internalRank;
private String collationQuery;
public NamedList<String> getMisspellingsAndCorrections() {
return misspellingsAndCorrections;
}
public int compareTo(SpellCheckCollation scc) {
int c = new Integer(internalRank).compareTo(scc.internalRank);
if (c == 0) {
return collationQuery.compareTo(scc.collationQuery);
}
return c;
}
public void setMisspellingsAndCorrections(
NamedList<String> misspellingsAndCorrections) {
this.misspellingsAndCorrections = misspellingsAndCorrections;
}
public NamedList<String> getMisspellingsAndCorrections() {
return misspellingsAndCorrections;
}
public int getHits() {
return hits;
}
public void setMisspellingsAndCorrections(
NamedList<String> misspellingsAndCorrections) {
this.misspellingsAndCorrections = misspellingsAndCorrections;
}
public void setHits(int hits) {
this.hits = hits;
}
public int getHits() {
return hits;
}
public String getCollationQuery() {
return collationQuery;
}
public void setHits(int hits) {
this.hits = hits;
}
public void setCollationQuery(String collationQuery) {
this.collationQuery = collationQuery;
}
public int getInternalRank() {
return internalRank;
}
public String getCollationQuery() {
return collationQuery;
}
public void setInternalRank(int internalRank) {
this.internalRank = internalRank;
}
public void setCollationQuery(String collationQuery) {
this.collationQuery = collationQuery;
}
public int getInternalRank() {
return internalRank;
}
public void setInternalRank(int internalRank) {
this.internalRank = internalRank;
}
}

View File

@ -36,107 +36,107 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SpellCheckCollator {
private static final Logger LOG = LoggerFactory.getLogger(SpellCheckCollator.class);
private static final Logger LOG = LoggerFactory.getLogger(SpellCheckCollator.class);
public List<SpellCheckCollation> collate(SpellingResult result, String originalQuery, ResponseBuilder ultimateResponse,
int maxCollations, int maxTries) {
List<SpellCheckCollation> collations = new ArrayList<SpellCheckCollation>();
public List<SpellCheckCollation> collate(SpellingResult result, String originalQuery, ResponseBuilder ultimateResponse,
int maxCollations, int maxTries) {
List<SpellCheckCollation> collations = new ArrayList<SpellCheckCollation>();
QueryComponent queryComponent = null;
if (ultimateResponse.components != null) {
for (SearchComponent sc : ultimateResponse.components) {
if (sc instanceof QueryComponent) {
queryComponent = (QueryComponent) sc;
break;
}
}
}
boolean verifyCandidateWithQuery = true;
if (maxTries < 1) {
maxTries = 1;
verifyCandidateWithQuery = false;
}
if (queryComponent == null && verifyCandidateWithQuery) {
LOG.warn("Could not find an instance of QueryComponent. Disabling collation verification against the index.");
maxTries = 1;
verifyCandidateWithQuery = false;
}
int tryNo = 0;
int collNo = 0;
PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions());
while (tryNo < maxTries && collNo < maxCollations && possibilityIter.hasNext()) {
QueryComponent queryComponent = null;
if (ultimateResponse.components != null) {
for (SearchComponent sc : ultimateResponse.components) {
if (sc instanceof QueryComponent) {
queryComponent = (QueryComponent) sc;
break;
}
}
}
RankedSpellPossibility possibility = possibilityIter.next();
String collationQueryStr = getCollation(originalQuery, possibility.getCorrections());
int hits = 0;
if (verifyCandidateWithQuery) {
tryNo++;
boolean verifyCandidateWithQuery = true;
if (maxTries < 1) {
maxTries = 1;
verifyCandidateWithQuery = false;
}
if (queryComponent == null && verifyCandidateWithQuery) {
LOG.warn("Could not find an instance of QueryComponent. Disabling collation verification against the index.");
maxTries = 1;
verifyCandidateWithQuery = false;
}
ResponseBuilder checkResponse = new ResponseBuilder();
checkResponse.setQparser(ultimateResponse.getQparser());
checkResponse.setFilters(ultimateResponse.getFilters());
checkResponse.setQueryString(collationQueryStr);
checkResponse.components = Arrays.asList(new SearchComponent[] { queryComponent });
ModifiableSolrParams params = new ModifiableSolrParams(ultimateResponse.req.getParams());
params.remove(CommonParams.Q);
params.add(CommonParams.Q, collationQueryStr);
params.remove(CommonParams.START);
params.remove(CommonParams.ROWS);
params.add(CommonParams.FL, "id");
params.add(CommonParams.ROWS, "0");
//Would rather have found a concrete class to use...
checkResponse.req = new SolrQueryRequestBase(ultimateResponse.req.getCore(), params) { };
checkResponse.rsp = new SolrQueryResponse();
try {
queryComponent.prepare(checkResponse);
queryComponent.process(checkResponse);
hits = (Integer) checkResponse.rsp.getToLog().get("hits");
} catch (Exception e) {
Log.warn("Exception trying to re-query to check if a spell check possibility would return any hits.", e);
}
}
if (hits > 0 || !verifyCandidateWithQuery) {
collNo++;
SpellCheckCollation collation = new SpellCheckCollation();
collation.setCollationQuery(collationQueryStr);
collation.setHits(hits);
collation.setInternalRank(possibility.getRank());
int tryNo = 0;
int collNo = 0;
PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions());
while (tryNo < maxTries && collNo < maxCollations && possibilityIter.hasNext()) {
NamedList<String> misspellingsAndCorrections = new NamedList<String>();
for (SpellCheckCorrection corr : possibility.getCorrections()) {
misspellingsAndCorrections.add(corr.getOriginal().toString(), corr.getCorrection());
}
collation.setMisspellingsAndCorrections(misspellingsAndCorrections);
collations.add(collation);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Collation: " + collationQueryStr + (verifyCandidateWithQuery ? (" will return " + hits + " hits.") : ""));
}
}
return collations;
}
RankedSpellPossibility possibility = possibilityIter.next();
String collationQueryStr = getCollation(originalQuery, possibility.getCorrections());
int hits = 0;
private String getCollation(String origQuery,
List<SpellCheckCorrection> corrections) {
StringBuilder collation = new StringBuilder(origQuery);
int offset = 0;
for (SpellCheckCorrection correction : corrections) {
Token tok = correction.getOriginal();
// we are replacing the query in order, but injected terms might cause
// illegal offsets due to previous replacements.
if (tok.getPositionIncrement() == 0)
continue;
collation.replace(tok.startOffset() + offset, tok.endOffset() + offset,
correction.getCorrection());
offset += correction.getCorrection().length()
- (tok.endOffset() - tok.startOffset());
}
return collation.toString();
}
if (verifyCandidateWithQuery) {
tryNo++;
ResponseBuilder checkResponse = new ResponseBuilder();
checkResponse.setQparser(ultimateResponse.getQparser());
checkResponse.setFilters(ultimateResponse.getFilters());
checkResponse.setQueryString(collationQueryStr);
checkResponse.components = Arrays.asList(new SearchComponent[] { queryComponent });
ModifiableSolrParams params = new ModifiableSolrParams(ultimateResponse.req.getParams());
params.remove(CommonParams.Q);
params.add(CommonParams.Q, collationQueryStr);
params.remove(CommonParams.START);
params.remove(CommonParams.ROWS);
params.add(CommonParams.FL, "id");
params.add(CommonParams.ROWS, "0");
//Would rather have found a concrete class to use...
checkResponse.req = new SolrQueryRequestBase(ultimateResponse.req.getCore(), params) { };
checkResponse.rsp = new SolrQueryResponse();
try {
queryComponent.prepare(checkResponse);
queryComponent.process(checkResponse);
hits = (Integer) checkResponse.rsp.getToLog().get("hits");
} catch (Exception e) {
Log.warn("Exception trying to re-query to check if a spell check possibility would return any hits.", e);
}
}
if (hits > 0 || !verifyCandidateWithQuery) {
collNo++;
SpellCheckCollation collation = new SpellCheckCollation();
collation.setCollationQuery(collationQueryStr);
collation.setHits(hits);
collation.setInternalRank(possibility.getRank());
NamedList<String> misspellingsAndCorrections = new NamedList<String>();
for (SpellCheckCorrection corr : possibility.getCorrections()) {
misspellingsAndCorrections.add(corr.getOriginal().toString(), corr.getCorrection());
}
collation.setMisspellingsAndCorrections(misspellingsAndCorrections);
collations.add(collation);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Collation: " + collationQueryStr + (verifyCandidateWithQuery ? (" will return " + hits + " hits.") : ""));
}
}
return collations;
}
private String getCollation(String origQuery,
List<SpellCheckCorrection> corrections) {
StringBuilder collation = new StringBuilder(origQuery);
int offset = 0;
for (SpellCheckCorrection correction : corrections) {
Token tok = correction.getOriginal();
// we are replacing the query in order, but injected terms might cause
// illegal offsets due to previous replacements.
if (tok.getPositionIncrement() == 0)
continue;
collation.replace(tok.startOffset() + offset, tok.endOffset() + offset,
correction.getCorrection());
offset += correction.getCorrection().length()
- (tok.endOffset() - tok.startOffset());
}
return collation.toString();
}
}

View File

@ -19,32 +19,32 @@ package org.apache.solr.spelling;
import org.apache.lucene.analysis.Token;
public class SpellCheckCorrection {
private Token original;
private String correction;
private int numberOfOccurences;
private Token original;
private String correction;
private int numberOfOccurences;
public Token getOriginal() {
return original;
}
public Token getOriginal() {
return original;
}
public void setOriginal(Token original) {
this.original = original;
}
public void setOriginal(Token original) {
this.original = original;
}
public String getCorrection() {
return correction;
}
public String getCorrection() {
return correction;
}
public void setCorrection(String correction) {
this.correction = correction;
}
public void setCorrection(String correction) {
this.correction = correction;
}
public int getNumberOfOccurences() {
return numberOfOccurences;
}
public int getNumberOfOccurences() {
return numberOfOccurences;
}
public void setNumberOfOccurences(int numberOfOccurences) {
this.numberOfOccurences = numberOfOccurences;
}
public void setNumberOfOccurences(int numberOfOccurences) {
this.numberOfOccurences = numberOfOccurences;
}
}