KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > SuffixStringMatcher


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 /**
10  * A class for efficiently matching <code>String</code>s against a set
11  * of suffixes. Zero-length <code>Strings</code> are ignored.
12  */

13 public class SuffixStringMatcher extends TrieStringMatcher {
14
15   /**
16    * Creates a new <code>PrefixStringMatcher</code> which will match
17    * <code>String</code>s with any suffix in the supplied array.
18    */

19   public SuffixStringMatcher(String JavaDoc[] suffixes) {
20     super();
21     for (int i= 0; i < suffixes.length; i++)
22       addPatternBackward(suffixes[i]);
23   }
24
25   /**
26    * Creates a new <code>PrefixStringMatcher</code> which will match
27    * <code>String</code>s with any suffix in the supplied
28    * <code>Collection</code>
29    */

30   public SuffixStringMatcher(Collection JavaDoc suffixes) {
31     super();
32     Iterator JavaDoc iter= suffixes.iterator();
33     while (iter.hasNext())
34       addPatternBackward((String JavaDoc)iter.next());
35   }
36
37   /**
38    * Returns true if the given <code>String</code> is matched by a
39    * suffix in the trie
40    */

41   public boolean matches(String JavaDoc input) {
42     TrieNode node= root;
43     for (int i= input.length() - 1; i >= 0; i--) {
44       node= node.getChild(input.charAt(i));
45       if (node == null)
46         return false;
47       if (node.isTerminal())
48         return true;
49     }
50     return false;
51   }
52
53
54   /**
55    * Returns the shortest suffix of <code>input<code> that is matched,
56    * or <code>null<code> if no match exists.
57    */

58   public String JavaDoc shortestMatch(String JavaDoc input) {
59     TrieNode node= root;
60     for (int i= input.length() - 1; i >= 0; i--) {
61       node= node.getChild(input.charAt(i));
62       if (node == null)
63         return null;
64       if (node.isTerminal())
65         return input.substring(i);
66     }
67     return null;
68   }
69
70   /**
71    * Returns the longest suffix of <code>input<code> that is matched,
72    * or <code>null<code> if no match exists.
73    */

74   public String JavaDoc longestMatch(String JavaDoc input) {
75     TrieNode node= root;
76     String JavaDoc result= null;
77     for (int i= input.length() - 1; i >= 0; i--) {
78       node= node.getChild(input.charAt(i));
79       if (node == null)
80         break;
81       if (node.isTerminal())
82         result= input.substring(i);
83     }
84     return result;
85   }
86
87   public static final void main(String JavaDoc[] argv) {
88     SuffixStringMatcher matcher=
89       new SuffixStringMatcher(
90         new String JavaDoc[]
91         {"a", "abcd", "bcd", "bcdefg", "defg", "aac", "baz", "foo", "foobar"} );
92
93     String JavaDoc[] tests= {"a", "ac", "abcd", "abcdefg", "apple", "aa", "aac",
94                     "aaccca", "abaz", "baz", "bazooka", "fo", "foobar",
95                     "kite", };
96
97     for (int i= 0; i < tests.length; i++) {
98       System.out.println("testing: " + tests[i]);
99       System.out.println(" matches: " + matcher.matches(tests[i]));
100       System.out.println(" shortest: " + matcher.shortestMatch(tests[i]));
101       System.out.println(" longest: " + matcher.longestMatch(tests[i]));
102     }
103   }
104 }
105
Popular Tags