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

« back to all changes in this revision

Viewing changes to lucene/contrib/analyzers/common/src/java/org/apache/lucene/analysis/synonym/WordnetSynonymParser.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
 
package org.apache.lucene.analysis.synonym;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * the License.  You may obtain a copy of the License at
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import java.io.IOException;
21
 
import java.io.LineNumberReader;
22
 
import java.io.Reader;
23
 
import java.text.ParseException;
24
 
 
25
 
import org.apache.lucene.analysis.Analyzer;
26
 
import org.apache.lucene.util.CharsRef;
27
 
 
28
 
/**
29
 
 * Parser for wordnet prolog format
30
 
 * <p>
31
 
 * See http://wordnet.princeton.edu/man/prologdb.5WN.html for a description of the format.
32
 
 * @lucene.experimental
33
 
 */
34
 
// TODO: allow you to specify syntactic categories (e.g. just nouns, etc)
35
 
public class WordnetSynonymParser extends SynonymMap.Builder {
36
 
  private final boolean expand;
37
 
  private final Analyzer analyzer;
38
 
  
39
 
  public WordnetSynonymParser(boolean dedup, boolean expand, Analyzer analyzer) {
40
 
    super(dedup);
41
 
    this.expand = expand;
42
 
    this.analyzer = analyzer;
43
 
  }
44
 
  
45
 
  public void add(Reader in) throws IOException, ParseException {
46
 
    LineNumberReader br = new LineNumberReader(in);
47
 
    try {
48
 
      String line = null;
49
 
      String lastSynSetID = "";
50
 
      CharsRef synset[] = new CharsRef[8];
51
 
      int synsetSize = 0;
52
 
      
53
 
      while ((line = br.readLine()) != null) {
54
 
        String synSetID = line.substring(2, 11);
55
 
 
56
 
        if (!synSetID.equals(lastSynSetID)) {
57
 
          addInternal(synset, synsetSize);
58
 
          synsetSize = 0;
59
 
        }
60
 
 
61
 
        if (synset.length <= synsetSize+1) {
62
 
          CharsRef larger[] = new CharsRef[synset.length * 2];
63
 
          System.arraycopy(synset, 0, larger, 0, synsetSize);
64
 
          synset = larger;
65
 
        }
66
 
        
67
 
        synset[synsetSize] = parseSynonym(line, synset[synsetSize]);
68
 
        synsetSize++;
69
 
        lastSynSetID = synSetID;
70
 
      }
71
 
      
72
 
      // final synset in the file
73
 
      addInternal(synset, synsetSize);
74
 
    } catch (IllegalArgumentException e) {
75
 
      ParseException ex = new ParseException("Invalid synonym rule at line " + br.getLineNumber(), 0);
76
 
      ex.initCause(e);
77
 
      throw ex;
78
 
    } finally {
79
 
      br.close();
80
 
    }
81
 
  }
82
 
 
83
 
  private CharsRef parseSynonym(String line, CharsRef reuse) throws IOException {
84
 
    if (reuse == null) {
85
 
      reuse = new CharsRef(8);
86
 
    }
87
 
    
88
 
    int start = line.indexOf('\'')+1;
89
 
    int end = line.lastIndexOf('\'');
90
 
    
91
 
    String text = line.substring(start, end).replace("''", "'");
92
 
    return analyze(analyzer, text, reuse);
93
 
  }
94
 
  
95
 
  private void addInternal(CharsRef synset[], int size) throws IOException {
96
 
    if (size <= 1) {
97
 
      return; // nothing to do
98
 
    }
99
 
    
100
 
    if (expand) {
101
 
      for (int i = 0; i < size; i++) {
102
 
        for (int j = 0; j < size; j++) {
103
 
          add(synset[i], synset[j], false);
104
 
        }
105
 
      }
106
 
    } else {
107
 
      for (int i = 0; i < size; i++) {
108
 
        add(synset[i], synset[0], false);
109
 
      }
110
 
    }
111
 
  }
112
 
}