1 2 3 4 package net.nutch.util; 5 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 9 13 public class SuffixStringMatcher extends TrieStringMatcher { 14 15 19 public SuffixStringMatcher(String [] suffixes) { 20 super(); 21 for (int i= 0; i < suffixes.length; i++) 22 addPatternBackward(suffixes[i]); 23 } 24 25 30 public SuffixStringMatcher(Collection suffixes) { 31 super(); 32 Iterator iter= suffixes.iterator(); 33 while (iter.hasNext()) 34 addPatternBackward((String )iter.next()); 35 } 36 37 41 public boolean matches(String 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 58 public String shortestMatch(String 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 74 public String longestMatch(String input) { 75 TrieNode node= root; 76 String 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 [] argv) { 88 SuffixStringMatcher matcher= 89 new SuffixStringMatcher( 90 new String [] 91 {"a", "abcd", "bcd", "bcdefg", "defg", "aac", "baz", "foo", "foobar"} ); 92 93 String [] 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 |