X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=kona%2Fsrc%2Forg%2Ffeistymeow%2Ftextual%2FWordBreakFinder.java;fp=kona%2Fsrc%2Forg%2Ffeistymeow%2Ftextual%2FWordBreakFinder.java;h=17388a87de13d34afb95486dcf28593bcfa03f06;hb=e6b0505dafd7d10d7c36757493e9cd05cbfda99d;hp=0000000000000000000000000000000000000000;hpb=daec747ca024ed7e5fbd19ab390c6eaeac2a3737;p=feisty_meow.git diff --git a/kona/src/org/feistymeow/textual/WordBreakFinder.java b/kona/src/org/feistymeow/textual/WordBreakFinder.java new file mode 100644 index 00000000..17388a87 --- /dev/null +++ b/kona/src/org/feistymeow/textual/WordBreakFinder.java @@ -0,0 +1,73 @@ +package org.feistymeow.textual; + +import java.util.HashSet; +import java.util.Set; + +public class WordBreakFinder +{ + public WordBreakFinder(SimpleDictionary lookups) { + _lookups = lookups; + } + + public Set findStringsWithGoodSpacing(String orig) { + HashSet toReturn = new HashSet(); + int startingChunk = Math.min(orig.length(), _lookups.longestWord()); + for (int i = startingChunk; i >= 1; i--) { + String first = orig.substring(0, i); + if (!_lookups.lookup(first)) { + // fail fast. this length chunk of the word doesn't match. + continue; + } + String second = orig.substring(i, orig.length()); + // first part of string is listed in dictionary. we can trivially add it if the other part is empty. + if (second.length() == 0) { + toReturn.add(first); + continue; + } + Set matches = findStringsWithGoodSpacing(second); + if (matches != null) { + for (String matched : matches) { + toReturn.add(first + " " + matched); + } + } + } + if (toReturn.isEmpty()) return null; + return toReturn; + } + + SimpleDictionary _lookups; + + public static void main(String argv[]) { + String smallWordList[] = { + "hops", "barley", "malt", "beer", "turnip" + }; + SimpleDictionary lookups = new SimpleDictionary(smallWordList); + WordBreakFinder finder = new WordBreakFinder(lookups); + + String toBreak = "maltbeerbeerturniphops"; + Set matches = finder.findStringsWithGoodSpacing(toBreak); + if (matches != null) { + System.out.println("matches found:"); + for (String match : matches) { + System.out.println(match); + } + } else { + System.out.println("ERROR: failed to find matches!"); + } + + toBreak = "maltbeerbeturniphops"; + matches = finder.findStringsWithGoodSpacing(toBreak); + if (matches != null) { + System.out.println("ERROR: matches found:"); + for (String match : matches) { + System.out.println(match); + } + } else { + System.out.println("found no matches, which is correct."); + } + } + +} + + +