7

I have a string that has blanks marked by a marker; (in this case, an underscore):

"f_ath_r".

I can execute the following command to get the word.

DictionaryLookup["f" ~~ _ ~~ "ath" ~~ _ ~~ "r"]

{"feather"}

How do I create and provide this pattern to the DictionaryLookup function starting from the given string?

Thanks.

Edit: Example of a typical use case

Starting with a phrase or sentence, for instance: "Actions speak louder than words"; and adding blanks to it randomly.

a_t_o_s _pe_k l_ude_ t_a_ w__ds

which upon Dictionary look up becomes:

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85

1 Answers1

8
pattern = StringReplace["f_ath_r", "_" -> Blank[]]
"f" ~~ _ ~~ "ath" ~~ _ ~~ "r"
DictionaryLookup @ pattern
{"feather"}

Generalizing to allow __ and ___ in the input string:

toSPattern = StringReplace[p : Repeated["_", {1, 3}] :> ToExpression[p]];

DictionaryLookup[toSPattern@"f_ath_r"]

{"feather"}
DictionaryLookup[toSPattern@"f___ath__r"]
{"father", "feather", "featherier", "forefather", "forgather"}
kglr
  • 394,356
  • 18
  • 477
  • 896
  • I hit the accept button many times, but it was frozen in disbelief. As soon as it came out of its catatonic state, it turned green. Many thanks. – Syed Sep 06 '21 at 12:34
  • @Syed, my pleasure. Thank you for the accept. – kglr Sep 06 '21 at 12:35