1 package org.feistymeow.textual;
3 import java.util.HashSet;
6 public class WordBreakFinder
8 public WordBreakFinder(SimpleDictionary lookups) {
12 public Set<String> findStringsWithGoodSpacing(String orig) {
13 HashSet<String> toReturn = new HashSet<String>();
14 int startingChunk = Math.min(orig.length(), _lookups.longestWord());
15 for (int i = startingChunk; i >= 1; i--) {
16 String first = orig.substring(0, i);
17 if (!_lookups.lookup(first)) {
18 // fail fast. this length chunk of the word doesn't match.
21 String second = orig.substring(i, orig.length());
22 // first part of string is listed in dictionary. we can trivially add it if the other part is empty.
23 if (second.length() == 0) {
27 Set<String> matches = findStringsWithGoodSpacing(second);
28 if (matches != null) {
29 for (String matched : matches) {
30 toReturn.add(first + " " + matched);
34 if (toReturn.isEmpty()) return null;
38 SimpleDictionary _lookups;
40 public static void main(String argv[]) {
41 String smallWordList[] = {
42 "hops", "barley", "malt", "beer", "turnip"
44 SimpleDictionary lookups = new SimpleDictionary(smallWordList);
45 WordBreakFinder finder = new WordBreakFinder(lookups);
47 String toBreak = "maltbeerbeerturniphops";
48 Set<String> matches = finder.findStringsWithGoodSpacing(toBreak);
49 if (matches != null) {
50 System.out.println("matches found:");
51 for (String match : matches) {
52 System.out.println(match);
55 System.out.println("ERROR: failed to find matches!");
58 toBreak = "maltbeerbeturniphops";
59 matches = finder.findStringsWithGoodSpacing(toBreak);
60 if (matches != null) {
61 System.out.println("ERROR: matches found:");
62 for (String match : matches) {
63 System.out.println(match);
66 System.out.println("found no matches, which is correct.");