mirror of https://github.com/apache/lucene.git
SOLR-822: rename correctPosition() to correct() in CharFilter because it is for correcting token offset, not for token position
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@755945 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
87b5b4768f
commit
b46fca3934
|
@ -28,44 +28,44 @@ import java.util.List;
|
|||
*/
|
||||
public abstract class BaseCharFilter extends CharFilter {
|
||||
|
||||
private List<PosCorrectMap> pcmList;
|
||||
private List<OffCorrectMap> pcmList;
|
||||
|
||||
public BaseCharFilter( CharStream in ){
|
||||
super(in);
|
||||
}
|
||||
|
||||
protected int correctPosition( int currentPos ){
|
||||
if( pcmList == null || pcmList.isEmpty() ) return currentPos;
|
||||
protected int correct( int currentOff ){
|
||||
if( pcmList == null || pcmList.isEmpty() ) return currentOff;
|
||||
for( int i = pcmList.size() - 1; i >= 0; i-- ){
|
||||
if( currentPos >= pcmList.get( i ).pos )
|
||||
return currentPos + pcmList.get( i ).cumulativeDiff;
|
||||
if( currentOff >= pcmList.get( i ).off )
|
||||
return currentOff + pcmList.get( i ).cumulativeDiff;
|
||||
}
|
||||
return currentPos;
|
||||
return currentOff;
|
||||
}
|
||||
|
||||
protected int getLastCumulativeDiff(){
|
||||
return pcmList == null || pcmList.isEmpty() ? 0 : pcmList.get( pcmList.size() - 1 ).cumulativeDiff;
|
||||
}
|
||||
|
||||
protected void addPosCorrectMap( int pos, int cumulativeDiff ){
|
||||
if( pcmList == null ) pcmList = new ArrayList<PosCorrectMap>();
|
||||
pcmList.add( new PosCorrectMap( pos, cumulativeDiff ) );
|
||||
protected void addOffCorrectMap( int off, int cumulativeDiff ){
|
||||
if( pcmList == null ) pcmList = new ArrayList<OffCorrectMap>();
|
||||
pcmList.add( new OffCorrectMap( off, cumulativeDiff ) );
|
||||
}
|
||||
|
||||
static class PosCorrectMap {
|
||||
static class OffCorrectMap {
|
||||
|
||||
int pos;
|
||||
int off;
|
||||
int cumulativeDiff;
|
||||
|
||||
PosCorrectMap( int pos, int cumulativeDiff ){
|
||||
this.pos = pos;
|
||||
OffCorrectMap( int off, int cumulativeDiff ){
|
||||
this.off = off;
|
||||
this.cumulativeDiff = cumulativeDiff;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('(');
|
||||
sb.append(pos);
|
||||
sb.append(off);
|
||||
sb.append(',');
|
||||
sb.append(cumulativeDiff);
|
||||
sb.append(')');
|
||||
|
|
|
@ -37,18 +37,18 @@ public abstract class CharFilter extends CharStream {
|
|||
|
||||
/**
|
||||
*
|
||||
* Subclass may want to override to correct the current position.
|
||||
* Subclass may want to override to correct the current offset.
|
||||
*
|
||||
* @param pos current position
|
||||
* @return corrected position
|
||||
* @param currentOff current offset
|
||||
* @return corrected offset
|
||||
*/
|
||||
protected int correctPosition( int pos ){
|
||||
return pos;
|
||||
protected int correct( int currentOff ){
|
||||
return currentOff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int correctOffset(int currentOff) {
|
||||
return input.correctOffset( correctPosition( currentOff ) );
|
||||
return input.correctOffset( correct( currentOff ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -57,10 +57,10 @@ public class MappingCharFilter extends BaseCharFilter {
|
|||
int prevCumulativeDiff = getLastCumulativeDiff();
|
||||
if( result.diff < 0 ){
|
||||
for( int i = 0; i < -result.diff ; i++ )
|
||||
addPosCorrectMap( nextCharCounter + i - prevCumulativeDiff, prevCumulativeDiff - 1 - i );
|
||||
addOffCorrectMap( nextCharCounter + i - prevCumulativeDiff, prevCumulativeDiff - 1 - i );
|
||||
}
|
||||
else{
|
||||
addPosCorrectMap( nextCharCounter - result.diff - prevCumulativeDiff, prevCumulativeDiff + result.diff ) ;
|
||||
addOffCorrectMap( nextCharCounter - result.diff - prevCumulativeDiff, prevCumulativeDiff + result.diff ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,20 @@
|
|||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.solr.analysis;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
@ -8,22 +25,22 @@ public class TestCharFilter extends TestCase {
|
|||
|
||||
public void testCharFilter1() throws Exception {
|
||||
CharStream cs = new CharFilter1( new CharReader( new StringReader("") ) );
|
||||
assertEquals( "corrected position is invalid", 1, cs.correctOffset( 0 ) );
|
||||
assertEquals( "corrected offset is invalid", 1, cs.correctOffset( 0 ) );
|
||||
}
|
||||
|
||||
public void testCharFilter2() throws Exception {
|
||||
CharStream cs = new CharFilter2( new CharReader( new StringReader("") ) );
|
||||
assertEquals( "corrected position is invalid", 2, cs.correctOffset( 0 ) );
|
||||
assertEquals( "corrected offset is invalid", 2, cs.correctOffset( 0 ) );
|
||||
}
|
||||
|
||||
public void testCharFilter12() throws Exception {
|
||||
CharStream cs = new CharFilter2( new CharFilter1( new CharReader( new StringReader("") ) ) );
|
||||
assertEquals( "corrected position is invalid", 3, cs.correctOffset( 0 ) );
|
||||
assertEquals( "corrected offset is invalid", 3, cs.correctOffset( 0 ) );
|
||||
}
|
||||
|
||||
public void testCharFilter11() throws Exception {
|
||||
CharStream cs = new CharFilter1( new CharFilter1( new CharReader( new StringReader("") ) ) );
|
||||
assertEquals( "corrected position is invalid", 2, cs.correctOffset( 0 ) );
|
||||
assertEquals( "corrected offset is invalid", 2, cs.correctOffset( 0 ) );
|
||||
}
|
||||
|
||||
static class CharFilter1 extends CharFilter {
|
||||
|
@ -33,8 +50,8 @@ public class TestCharFilter extends TestCase {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected int correctPosition(int currentPos) {
|
||||
return currentPos + 1;
|
||||
protected int correct(int currentOff) {
|
||||
return currentOff + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,8 +62,8 @@ public class TestCharFilter extends TestCase {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected int correctPosition(int currentPos) {
|
||||
return currentPos + 2;
|
||||
protected int correct(int currentOff) {
|
||||
return currentOff + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue