2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.analysis;
20
import org.apache.lucene.analysis.Token;
21
import org.apache.lucene.analysis.CharArrayMap;
22
import org.apache.lucene.util.Version;
26
/** Mapping rules for use with {@link SlowSynonymFilter}
27
* @deprecated (3.4) use {@link SynonymFilterFactory} instead. only for precise index backwards compatibility. this factory will be removed in Lucene 5.0
30
class SlowSynonymMap {
31
/** @lucene.internal */
32
public CharArrayMap<SlowSynonymMap> submap; // recursive: Map<String, SynonymMap>
33
/** @lucene.internal */
34
public Token[] synonyms;
37
static final int INCLUDE_ORIG=0x01;
38
static final int IGNORE_CASE=0x02;
40
public SlowSynonymMap() {}
41
public SlowSynonymMap(boolean ignoreCase) {
42
if (ignoreCase) flags |= IGNORE_CASE;
45
public boolean includeOrig() { return (flags & INCLUDE_ORIG) != 0; }
46
public boolean ignoreCase() { return (flags & IGNORE_CASE) != 0; }
49
* @param singleMatch List<String>, the sequence of strings to match
50
* @param replacement List<Token> the list of tokens to use on a match
51
* @param includeOrig sets a flag on this mapping signaling the generation of matched tokens in addition to the replacement tokens
52
* @param mergeExisting merge the replacement tokens with any other mappings that exist
54
public void add(List<String> singleMatch, List<Token> replacement, boolean includeOrig, boolean mergeExisting) {
55
SlowSynonymMap currMap = this;
56
for (String str : singleMatch) {
57
if (currMap.submap==null) {
58
// for now hardcode at 2.9, as its what the old code did.
59
// would be nice to fix, but shouldn't store a version in each submap!!!
60
currMap.submap = new CharArrayMap<SlowSynonymMap>(Version.LUCENE_29, 1, ignoreCase());
63
SlowSynonymMap map = currMap.submap.get(str);
65
map = new SlowSynonymMap();
66
map.flags |= flags & IGNORE_CASE;
67
currMap.submap.put(str, map);
73
if (currMap.synonyms != null && !mergeExisting) {
74
throw new RuntimeException("SynonymFilter: there is already a mapping for " + singleMatch);
76
List<Token> superset = currMap.synonyms==null ? replacement :
77
mergeTokens(Arrays.asList(currMap.synonyms), replacement);
78
currMap.synonyms = superset.toArray(new Token[superset.size()]);
79
if (includeOrig) currMap.flags |= INCLUDE_ORIG;
84
public String toString() {
85
StringBuilder sb = new StringBuilder("<");
88
for (int i=0; i<synonyms.length; i++) {
89
if (i!=0) sb.append(',');
90
sb.append(synonyms[i]);
92
if ((flags & INCLUDE_ORIG)!=0) {
104
/** Produces a List<Token> from a List<String> */
105
public static List<Token> makeTokens(List<String> strings) {
106
List<Token> ret = new ArrayList<Token>(strings.size());
107
for (String str : strings) {
108
//Token newTok = new Token(str,0,0,"SYNONYM");
109
Token newTok = new Token(str, 0,0,"SYNONYM");
117
* Merge two lists of tokens, producing a single list with manipulated positionIncrements so that
118
* the tokens end up at the same position.
120
* Example: [a b] merged with [c d] produces [a/b c/d] ('/' denotes tokens in the same position)
121
* Example: [a,5 b,2] merged with [c d,4 e,4] produces [c a,5/d b,2 e,2] (a,n means a has posInc=n)
124
public static List<Token> mergeTokens(List<Token> lst1, List<Token> lst2) {
125
ArrayList<Token> result = new ArrayList<Token>();
126
if (lst1 ==null || lst2 ==null) {
127
if (lst2 != null) result.addAll(lst2);
128
if (lst1 != null) result.addAll(lst1);
133
Iterator<Token> iter1=lst1.iterator();
134
Iterator<Token> iter2=lst2.iterator();
135
Token tok1 = iter1.hasNext() ? iter1.next() : null;
136
Token tok2 = iter2.hasNext() ? iter2.next() : null;
137
int pos1 = tok1!=null ? tok1.getPositionIncrement() : 0;
138
int pos2 = tok2!=null ? tok2.getPositionIncrement() : 0;
139
while(tok1!=null || tok2!=null) {
140
while (tok1 != null && (pos1 <= pos2 || tok2==null)) {
141
Token tok = new Token(tok1.startOffset(), tok1.endOffset(), tok1.type());
142
tok.copyBuffer(tok1.buffer(), 0, tok1.length());
143
tok.setPositionIncrement(pos1-pos);
146
tok1 = iter1.hasNext() ? iter1.next() : null;
147
pos1 += tok1!=null ? tok1.getPositionIncrement() : 0;
149
while (tok2 != null && (pos2 <= pos1 || tok1==null)) {
150
Token tok = new Token(tok2.startOffset(), tok2.endOffset(), tok2.type());
151
tok.copyBuffer(tok2.buffer(), 0, tok2.length());
152
tok.setPositionIncrement(pos2-pos);
155
tok2 = iter2.hasNext() ? iter2.next() : null;
156
pos2 += tok2!=null ? tok2.getPositionIncrement() : 0;