add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / stempel / src / java / org / egothor / stemmer / Compile.java
1 /*
2                     Egothor Software License version 1.00
3                     Copyright (C) 1997-2004 Leo Galambos.
4                  Copyright (C) 2002-2004 "Egothor developers"
5                       on behalf of the Egothor Project.
6                              All rights reserved.
7
8    This  software  is  copyrighted  by  the "Egothor developers". If this
9    license applies to a single file or document, the "Egothor developers"
10    are the people or entities mentioned as copyright holders in that file
11    or  document.  If  this  license  applies  to the Egothor project as a
12    whole,  the  copyright holders are the people or entities mentioned in
13    the  file CREDITS. This file can be found in the same location as this
14    license in the distribution.
15
16    Redistribution  and  use  in  source and binary forms, with or without
17    modification, are permitted provided that the following conditions are
18    met:
19     1. Redistributions  of  source  code  must retain the above copyright
20        notice, the list of contributors, this list of conditions, and the
21        following disclaimer.
22     2. Redistributions  in binary form must reproduce the above copyright
23        notice, the list of contributors, this list of conditions, and the
24        disclaimer  that  follows  these  conditions  in the documentation
25        and/or other materials provided with the distribution.
26     3. The name "Egothor" must not be used to endorse or promote products
27        derived  from  this software without prior written permission. For
28        written permission, please contact Leo.G@seznam.cz
29     4. Products  derived  from this software may not be called "Egothor",
30        nor  may  "Egothor"  appear  in  their name, without prior written
31        permission from Leo.G@seznam.cz.
32
33    In addition, we request that you include in the end-user documentation
34    provided  with  the  redistribution  and/or  in the software itself an
35    acknowledgement equivalent to the following:
36    "This product includes software developed by the Egothor Project.
37     http://egothor.sf.net/"
38
39    THIS  SOFTWARE  IS  PROVIDED  ``AS  IS''  AND ANY EXPRESSED OR IMPLIED
40    WARRANTIES,  INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41    MERCHANTABILITY  AND  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42    IN  NO  EVENT  SHALL THE EGOTHOR PROJECT OR ITS CONTRIBUTORS BE LIABLE
43    FOR   ANY   DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
44    CONSEQUENTIAL  DAMAGES  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45    SUBSTITUTE  GOODS  OR  SERVICES;  LOSS  OF  USE,  DATA, OR PROFITS; OR
46    BUSINESS  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47    WHETHER  IN  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
48    OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
49    IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
51    This  software  consists  of  voluntary  contributions  made  by  many
52    individuals  on  behalf  of  the  Egothor  Project  and was originally
53    created by Leo Galambos (Leo.G@seznam.cz).
54  */
55 package org.egothor.stemmer;
56
57 import java.io.BufferedOutputStream;
58 import java.io.BufferedReader;
59 import java.io.DataOutputStream;
60 import java.io.FileInputStream;
61 import java.io.FileNotFoundException;
62 import java.io.FileOutputStream;
63 import java.io.IOException;
64 import java.io.InputStreamReader;
65 import java.io.LineNumberReader;
66 import java.util.StringTokenizer;
67
68 /**
69  * The Compile class is used to compile a stemmer table.
70  */
71 public class Compile {
72   
73   static boolean backward;
74   static boolean multi;
75   static Trie trie;
76   
77   /**
78    * Entry point to the Compile application.
79    * <p>
80    * This program takes any number of arguments: the first is the name of the
81    * desired stemming algorithm to use (a list is available in the package
82    * description) , all of the rest should be the path or paths to a file or
83    * files containing a stemmer table to compile.
84    * 
85    * @param args the command line arguments
86    */
87   public static void main(java.lang.String[] args) {
88     if (args.length < 1) {
89       return;
90     }
91     
92     args[0].toUpperCase();
93     
94     backward = args[0].charAt(0) == '-';
95     int qq = (backward) ? 1 : 0;
96     boolean storeorig = false;
97     
98     if (args[0].charAt(qq) == '0') {
99       storeorig = true;
100       qq++;
101     }
102     
103     multi = args[0].charAt(qq) == 'M';
104     if (multi) {
105       qq++;
106     }
107     
108     String charset = System.getProperty("egothor.stemmer.charset", "UTF-8");
109     
110     char optimizer[] = new char[args[0].length() - qq];
111     for (int i = 0; i < optimizer.length; i++) {
112       optimizer[i] = args[0].charAt(qq + i);
113     }
114     
115     for (int i = 1; i < args.length; i++) {
116       LineNumberReader in;
117       // System.out.println("[" + args[i] + "]");
118       Diff diff = new Diff();
119       try {
120         int stems = 0;
121         int words = 0;
122         
123         allocTrie();
124         
125         System.out.println(args[i]);
126         in = new LineNumberReader(new BufferedReader(new InputStreamReader(
127             new FileInputStream(args[i]), charset)));
128         for (String line = in.readLine(); line != null; line = in.readLine()) {
129           try {
130             line = line.toLowerCase();
131             StringTokenizer st = new StringTokenizer(line);
132             String stem = st.nextToken();
133             if (storeorig) {
134               trie.add(stem, "-a");
135               words++;
136             }
137             while (st.hasMoreTokens()) {
138               String token = st.nextToken();
139               if (token.equals(stem) == false) {
140                 trie.add(token, diff.exec(token, stem));
141                 words++;
142               }
143             }
144           } catch (java.util.NoSuchElementException x) {
145             // no base token (stem) on a line
146           }
147         }
148         
149         Optimizer o = new Optimizer();
150         Optimizer2 o2 = new Optimizer2();
151         Lift l = new Lift(true);
152         Lift e = new Lift(false);
153         Gener g = new Gener();
154         
155         for (int j = 0; j < optimizer.length; j++) {
156           String prefix;
157           switch (optimizer[j]) {
158             case 'G':
159               trie = trie.reduce(g);
160               prefix = "G: ";
161               break;
162             case 'L':
163               trie = trie.reduce(l);
164               prefix = "L: ";
165               break;
166             case 'E':
167               trie = trie.reduce(e);
168               prefix = "E: ";
169               break;
170             case '2':
171               trie = trie.reduce(o2);
172               prefix = "2: ";
173               break;
174             case '1':
175               trie = trie.reduce(o);
176               prefix = "1: ";
177               break;
178             default:
179               continue;
180           }
181           trie.printInfo(prefix + " ");
182         }
183                
184         DataOutputStream os = new DataOutputStream(new BufferedOutputStream(
185             new FileOutputStream(args[i] + ".out")));
186         os.writeUTF(args[0]);
187         trie.store(os);
188         os.close();
189         
190       } catch (FileNotFoundException x) {
191         x.printStackTrace();
192       } catch (IOException x) {
193         x.printStackTrace();
194       }
195     }
196   }
197   
198   static void allocTrie() {
199     if (multi) {
200       trie = new MultiTrie2(!backward);
201     } else {
202       trie = new Trie(!backward);
203     }
204   }
205 }