~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/SlowSynonymMap.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.analysis;
19
 
 
20
 
import org.apache.lucene.analysis.Token;
21
 
import org.apache.lucene.analysis.CharArrayMap;
22
 
import org.apache.lucene.util.Version;
23
 
 
24
 
import java.util.*;
25
 
 
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
28
 
 */
29
 
@Deprecated
30
 
class SlowSynonymMap {
31
 
  /** @lucene.internal */
32
 
  public CharArrayMap<SlowSynonymMap> submap; // recursive: Map<String, SynonymMap>
33
 
  /** @lucene.internal */
34
 
  public Token[] synonyms;
35
 
  int flags;
36
 
 
37
 
  static final int INCLUDE_ORIG=0x01;
38
 
  static final int IGNORE_CASE=0x02;
39
 
 
40
 
  public SlowSynonymMap() {}
41
 
  public SlowSynonymMap(boolean ignoreCase) {
42
 
    if (ignoreCase) flags |= IGNORE_CASE;
43
 
  }
44
 
 
45
 
  public boolean includeOrig() { return (flags & INCLUDE_ORIG) != 0; }
46
 
  public boolean ignoreCase() { return (flags & IGNORE_CASE) != 0; }
47
 
 
48
 
  /**
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
53
 
   */
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());
61
 
      }
62
 
 
63
 
      SlowSynonymMap map = currMap.submap.get(str);
64
 
      if (map==null) {
65
 
        map = new SlowSynonymMap();
66
 
        map.flags |= flags & IGNORE_CASE;
67
 
        currMap.submap.put(str, map);
68
 
      }
69
 
 
70
 
      currMap = map;
71
 
    }
72
 
 
73
 
    if (currMap.synonyms != null && !mergeExisting) {
74
 
      throw new RuntimeException("SynonymFilter: there is already a mapping for " + singleMatch);
75
 
    }
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;
80
 
  }
81
 
 
82
 
 
83
 
  @Override
84
 
  public String toString() {
85
 
    StringBuilder sb = new StringBuilder("<");
86
 
    if (synonyms!=null) {
87
 
      sb.append("[");
88
 
      for (int i=0; i<synonyms.length; i++) {
89
 
        if (i!=0) sb.append(',');
90
 
        sb.append(synonyms[i]);
91
 
      }
92
 
      if ((flags & INCLUDE_ORIG)!=0) {
93
 
        sb.append(",ORIG");
94
 
      }
95
 
      sb.append("],");
96
 
    }
97
 
    sb.append(submap);
98
 
    sb.append(">");
99
 
    return sb.toString();
100
 
  }
101
 
 
102
 
 
103
 
 
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");
110
 
      ret.add(newTok);
111
 
    }
112
 
    return ret;
113
 
  }
114
 
 
115
 
 
116
 
  /**
117
 
   * Merge two lists of tokens, producing a single list with manipulated positionIncrements so that
118
 
   * the tokens end up at the same position.
119
 
   *
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)
122
 
   *
123
 
   */
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);
129
 
      return result;
130
 
    }
131
 
 
132
 
    int pos=0;
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);
144
 
        result.add(tok);
145
 
        pos=pos1;
146
 
        tok1 = iter1.hasNext() ? iter1.next() : null;
147
 
        pos1 += tok1!=null ? tok1.getPositionIncrement() : 0;
148
 
      }
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);
153
 
        result.add(tok);
154
 
        pos=pos2;
155
 
        tok2 = iter2.hasNext() ? iter2.next() : null;
156
 
        pos2 += tok2!=null ? tok2.getPositionIncrement() : 0;
157
 
      }
158
 
    }
159
 
    return result;
160
 
  }
161
 
 
162
 
}