5

Probably my problem is very simple but I couldn't have found a solution to it anywhere else.

How can I find a word of a certain length using a DictionaryLookup function? What things stand for a universal single letter?

I appreciate your help in resolving that problem.

Jinxed
  • 3,753
  • 10
  • 24
Arthur Altman
  • 153
  • 1
  • 5

2 Answers2

7

You may find some help in the documentation for string patterns: _ is the universal placeholder you seek, so we can construct an appropriate pattern for handing to DictionaryLookup:

dl[len_]:=DictionaryLookup[Repeated[_, {len}]]

(meaning: "there must be exactly len arbitrary characters in the string").

Example:

dl[20]
(* {"Andrianampoinimerina", "buckminsterfullerene", 
    "compartmentalization", "counterrevolutionary", 
    "electroencephalogram", "great-granddaughters", 
    "institutionalization", "internationalization", 
    "magnetohydrodynamics", "uncharacteristically"} *)

Timings:

AbsoluteTiming[dl[20]][[1]]
(* 0.020801 *)
AbsoluteTiming[DictionaryLookup[a__ /; StringLength[a] == 20]][[1]]
(* 0.608601 *)

So, if you also need speed, dl might be preferable.

Jinxed
  • 3,753
  • 10
  • 24
5
DictionaryLookup[a__ /; StringLength[a] == 5]

Blank (_) stands for a single letter. __ stands for one or more letters. ___ stands for zero or more letters. a_, a__ or a___ give the matching string the name a and /; StringLength[a] == 5 imposes a condition on the pattern which says that "this pattern only matches when a is five characters long."

C. E.
  • 70,533
  • 6
  • 140
  • 264