Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TestApp.cs
Go to the documentation of this file.
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 using System;
18 namespace SF.Snowball
19 {
20 
21  public class TestApp
22  {
23  [STAThread]
24  public static void Main(System.String[] args)
25  {
26 
27  if (args.Length < 2)
28  {
29  ExitWithUsage();
30  }
31 
32  System.Type stemClass = System.Type.GetType("SF.Snowball.Ext." + args[0] + "Stemmer");
33  SnowballProgram stemmer = (SnowballProgram) System.Activator.CreateInstance(stemClass);
34  System.Reflection.MethodInfo stemMethod = stemClass.GetMethod("stem", (new System.Type[0] == null)?new System.Type[0]:(System.Type[]) new System.Type[0]);
35 
36  System.IO.StreamReader reader;
37  reader = new System.IO.StreamReader(new System.IO.FileStream(args[1], System.IO.FileMode.Open, System.IO.FileAccess.Read), System.Text.Encoding.Default);
38  reader = new System.IO.StreamReader(reader.BaseStream, reader.CurrentEncoding);
39 
40  System.Text.StringBuilder input = new System.Text.StringBuilder();
41 
42  System.IO.Stream outstream = System.Console.OpenStandardOutput();
43 
44  if (args.Length > 2 && args[2].Equals("-o"))
45  {
46  outstream = new System.IO.FileStream(args[3], System.IO.FileMode.Create);
47  }
48  else if (args.Length > 2)
49  {
50  ExitWithUsage();
51  }
52 
53  System.IO.StreamWriter output = new System.IO.StreamWriter(outstream, System.Text.Encoding.Default);
54  output = new System.IO.StreamWriter(output.BaseStream, output.Encoding);
55 
56  int repeat = 1;
57  if (args.Length > 4)
58  {
59  repeat = System.Int32.Parse(args[4]);
60  }
61 
62  System.Object[] emptyArgs = new System.Object[0];
63  int character;
64  while ((character = reader.Read()) != - 1)
65  {
66  char ch = (char) character;
67  if (System.Char.IsWhiteSpace(ch))
68  {
69  if (input.Length > 0)
70  {
71  stemmer.SetCurrent(input.ToString());
72  for (int i = repeat; i != 0; i--)
73  {
74  stemMethod.Invoke(stemmer, (System.Object[]) emptyArgs);
75  }
76  output.Write(stemmer.GetCurrent());
77  output.Write('\n');
78  input.Remove(0, input.Length - 0);
79  }
80  }
81  else
82  {
83  input.Append(System.Char.ToLower(ch));
84  }
85  }
86  output.Flush();
87  }
88 
89  private static void ExitWithUsage()
90  {
91  System.Console.Error.WriteLine("Usage: TestApp <stemmer name> <input file> [-o <output file>]");
92  System.Environment.Exit(1);
93  }
94  }
95 }