What is the best approach to a problem like this in Mathematica?
I can only manage this:
func[stringlist_] :=
With[{i =
Last@Flatten@
Table[DictionaryLookup[StringTake[#[[2]], n]], {n, 14}]}, {i,
StringDrop[#[[2]], StringLength@i]}] &@stringlist;
func1[string_] :=
Rest@NestWhileList[func, {"", # <> CharacterRange["a", "z"]},
Length@Characters@#[[2]] > 26 &][[All, 1]] &@string;
func1["tableapplechairtablecupboard"]
(*
{"table", "apple", "chair", "table", "cupboard"}
*)
with its obvious shortcomings.
Added
Here is a pre-made file (made from WordFrequencyData /@ DictionaryLookup[]
- very slow!!) incase anyone wanted to make a weighted function:
wfd = ToExpression@
Import["https://raw.githubusercontent.com/martinq321/dictionary/master/values", "List"];
eg something like this (based on @kirma's answer below) can be used to faithfully recreate about 50% of test cases:
suggested@StringJoin@removespaces@
TextSentences[WikipediaData["PageID" -> "5094570", "ArticlePlaintext"]][[1]]
(*
the great wall of china is a series of fortifications made of stone
brick tamped earth wood and other materials generally built along an
east to west line across the historical northern borders of china to
protect the chinese states and empires against the raids and
invasions of the various nomadic groups of the eurasian steppe
*)
though I am sure there are far quicker and more efficient methods.
{"table", "apple", "chair", "table", "cup", "board"}
? $\endgroup$ – J. M.'s ennui♦ Jul 3 '16 at 2:40altelem = Alternatives @@ DictionaryLookup[];
and using myf2 @ "tableapplecharitablecupboarding"
appears faster than kirma'sModule
. (WithAlternatives @@ DictionaryLookup[]
moved outside the latter of course.) $\endgroup$ – Mr.Wizard Jul 19 '16 at 11:19