xref: /Lucene/lucene/analysis/stempel/src/java/org/egothor/stemmer/DiffIt.java (revision 2971f311a2b4a9139e3a74edbe76b08bc0e288a3)
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.LineNumberReader;
58 import java.nio.charset.Charset;
59 import java.nio.file.Files;
60 import java.nio.file.Paths;
61 import java.util.Locale;
62 import java.util.StringTokenizer;
63 import org.apache.lucene.util.SuppressForbidden;
64 
65 /** The DiffIt class is a means generate patch commands from an already prepared stemmer table. */
66 public class DiffIt {
67 
68   /** no instantiation */
DiffIt()69   private DiffIt() {}
70 
get(int i, String s)71   static int get(int i, String s) {
72     try {
73       return Integer.parseInt(s.substring(i, i + 1));
74     } catch (
75         @SuppressWarnings("unused")
76         Exception x) {
77       return 1;
78     }
79   }
80 
81   /**
82    * Entry point to the DiffIt application.
83    *
84    * <p>This application takes one argument, the path to a file containing a stemmer table. The
85    * program reads the file and generates the patch commands for the stems.
86    *
87    * @param args the path to a file containing a stemmer table
88    */
89   @SuppressForbidden(reason = "System.out required: command line tool")
main(java.lang.String[] args)90   public static void main(java.lang.String[] args) throws Exception {
91 
92     int ins = get(0, args[0]);
93     int del = get(1, args[0]);
94     int rep = get(2, args[0]);
95     int nop = get(3, args[0]);
96 
97     for (int i = 1; i < args.length; i++) {
98       // System.out.println("[" + args[i] + "]");
99       Diff diff = new Diff(ins, del, rep, nop);
100       String charset = System.getProperty("egothor.stemmer.charset", "UTF-8");
101       try (LineNumberReader in =
102           new LineNumberReader(
103               Files.newBufferedReader(Paths.get(args[i]), Charset.forName(charset)))) {
104         for (String line = in.readLine(); line != null; line = in.readLine()) {
105           try {
106             line = line.toLowerCase(Locale.ROOT);
107             StringTokenizer st = new StringTokenizer(line);
108             String stem = st.nextToken();
109             System.out.println(stem + " -a");
110             while (st.hasMoreTokens()) {
111               String token = st.nextToken();
112               if (token.equals(stem) == false) {
113                 System.out.println(stem + " " + diff.exec(token, stem));
114               }
115             }
116           } catch (
117               @SuppressWarnings("unused")
118               java.util.NoSuchElementException x) {
119             // no base token (stem) on a line
120           }
121         }
122       }
123     }
124   }
125 }
126