[Snowball-discuss] Adding a public stem() method to SnowballProgram.java

Dawid Weiss dawid.weiss at cs.put.poznan.pl
Tue Jul 24 10:34:55 BST 2007


Hi there!

I would like to suggest a small improvement to the snowball Java library -- all 
generated snowball classes have a public stem() method, but the superclass does 
not declare it. This is a bit awkward, as best shown in TestApp.java, where this 
method is invoked by reflection (unnecessarily).

I attach a patch to the Java sources that:

1) removes unnecessary semicolons from the end of class declarations,
2) adds a public default stem() method to SnowballProgram; this method
returns 'false' by default.
3) modifies TestApp to include a comment about file encoding JVM setting
and uses stem() method, not reflection.

Hope this will be useful for somebody,
Dawid
-------------- next part --------------
Index: TestApp.java
===================================================================
--- TestApp.java	(revision 488)
+++ TestApp.java	(working copy)
@@ -16,9 +16,11 @@
     private static void usage()
     {
         System.err.println("Usage: TestApp <algorithm> <input file> [-o <output file>]");
+        System.err.println("Input and output file's encoding can be set using system property:");
+        System.err.println("-Dfile.encoding=UTF-8");
     }
 
-    public static void main(String [] args) throws Throwable {
+    public static void main(String [] args) throws Exception {
 	if (args.length < 2) {
             usage();
             return;
@@ -27,7 +29,6 @@
 	Class stemClass = Class.forName("org.tartarus.snowball.ext." +
 					args[0] + "Stemmer");
         SnowballProgram stemmer = (SnowballProgram) stemClass.newInstance();
-	Method stemMethod = stemClass.getMethod("stem", new Class[0]);
 
 	Reader reader;
 	reader = new InputStreamReader(new FileInputStream(args[1]));
@@ -63,7 +64,7 @@
 		if (input.length() > 0) {
 		    stemmer.setCurrent(input.toString());
 		    for (int i = repeat; i != 0; i--) {
-			stemMethod.invoke(stemmer, emptyArgs);
+			stemmer.stem();
 		    }
 		    output.write(stemmer.getCurrent());
 		    output.write('\n');
Index: Among.java
===================================================================
--- Among.java	(revision 488)
+++ Among.java	(working copy)
@@ -31,4 +31,4 @@
     public Method method; /* method to use if substring matches */
     public SnowballProgram methodobject; /* object to invoke method on */
    
-};
+}
Index: SnowballProgram.java
===================================================================
--- SnowballProgram.java	(revision 488)
+++ SnowballProgram.java	(working copy)
@@ -23,6 +23,16 @@
     }
 
     /**
+     * Default implementation of <code>stem()</code> method returns
+     * <code>false</code>, overriden by subclasses generated for each
+     * language.
+     */
+    public boolean stem()
+    {
+        return false;
+    }
+
+    /**
      * Get the current string.
      */
     public String getCurrent()
@@ -414,5 +424,5 @@
 }
 */
 
-};
+}
 


More information about the Snowball-discuss mailing list