JavaAlgorithms
Elementary and no so elementary Java algorithms
|
00001 00009 package stringAlgorithms; 00010 00031 @SuppressWarnings("javadoc") 00032 public class SentenceRecognize { 00033 00034 // Note that not all words in the dictionary are properly spelled english words 00035 protected final static String dictionary[] = {"a", "an", "at", "attach", "attached", "he", "apple", "are", "area", 00036 "arean", "areana", "arm", "charm", "lever", "clever", "drop", 00037 "droped", "dope", "doped", "it", "the", "edith", "ball"}; 00038 00039 public SentenceRecognize() { } 00040 00046 protected boolean isWord(String word) { 00047 boolean is = false; 00048 if (word != null) { 00049 for (String w : dictionary) { 00050 if (w.equals(word)) { 00051 is = true; 00052 break; 00053 } 00054 } 00055 } 00056 return is; 00057 } // isWord 00058 00091 public boolean isSentence(String sentence) { 00092 boolean is = false; 00093 if (sentence != null && sentence.length() > 0) { 00094 int start = 0; 00095 for (int end = 1; end <= sentence.length() && (!is); end++) { 00096 String word = sentence.substring(start, end); 00097 if (isWord(word)) { 00098 if (end == sentence.length()) { 00099 is = true; 00100 } 00101 else { 00102 is = isSentence( sentence.substring(end)); 00103 } 00104 } 00105 } 00106 } 00107 return is; 00108 } 00109 00110 }