7
$\begingroup$

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

$\endgroup$

1 Answer 1

8
$\begingroup$
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"}
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Syed
    Sep 6, 2021 at 12:34
  • $\begingroup$ @Syed, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Sep 6, 2021 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.