3
$\begingroup$

I am using v12.2.0. As a minimal example, the idea is to find all words that have two pairs of matching characters and two other characters for a total of 6 characters. The question is similar to 67518 (as I discovered later).

p1 = Alternatives @@ 
   Permutations@StringExpression[a_, a_, c_, d_, b_, b_];
p2 = Alternatives @@ 
   Permutations@StringExpression[a_, a_, b_, c_, d_, b_];

Sort[p1] === Sort[p2]
DictionaryLookup[p1]
DictionaryLookup[p2]

True

{}

{"eerier"}

Trying these patterns on the cloud gives results with 793 entries each. This seems like a bug for my version. So, I wanted to somehow incorporate the OrderlessPatternSequence.


Question

My question is whether StringExpression can be used in conjunction with OrderlessPatternSequence instead of relying on Permutations for such tasks? For more readability, can Repeated be incorporated with such a pattern?

$\endgroup$
1
  • $\begingroup$ To use OrderlessPatternSequence on StringExpression you would need to change the string into a list of characters. $\endgroup$ Commented Nov 7 at 8:14

2 Answers 2

4
$\begingroup$

It seems like DictionaryLookup was not designed to work with Alternatives of StringExpression in earlier versions and returns only the result for the first alternative.

Instead it works with Alternatives applied to a single character.

First line works with "e" | "t" but fourth line DictionaryLookup[(x_ ~~ x_ ~~ y_) | (x_ ~~ y_ ~~ y_)] returns result like if it was just DictionaryLookup[(x_ ~~ x_ ~~ y_)].

DictionaryLookup[x_ ~~ "e" | "t"]

DictionaryLookup[x_ ~~ x_ ~~ y_]

DictionaryLookup[x_ ~~ y_ ~~ y_]

DictionaryLookup[(x_ ~~ x_ ~~ y_) | (x_ ~~ y_ ~~ y_)]

{"at", "be", "he", "it", "Le", "me", "re", "we", "ye"}

{"aah", "BBC", "DDT", "eek", "eel", "LLB", "ooh", "ppm", "ssh", \
"WWW", "XXL"}

{"add", "all", "Ann", "ass", "baa", "bee", "boo", "brr", "CNN", \
"coo", "Dee", "ebb", "eff", "egg", "ell", "err", "fee", "gee", "goo", \
"ill", "inn", "lee", "Lee", "loo", "moo", "nee", "odd", "off", "Orr", \
"pee", "see", "shh", "tee", "too", "wee", "woo", "WWW", "zoo"}

{"aah", "BBC", "DDT", "eek", "eel", "LLB", "ooh", "ppm", "ssh", \
"WWW", "XXL"}

As a workaround you can use:

DictionaryLookup /@ 
  List @@ ((x_ ~~ x_ ~~ y_) | (x_ ~~ y_ ~~ y_)) // Flatten

{"aah", "BBC", "DDT", "eek", "eel", "LLB", "ooh", "ppm", "ssh", \
"WWW", "XXL", "add", "all", "Ann", "ass", "baa", "bee", "boo", "brr", \
"CNN", "coo", "Dee", "ebb", "eff", "egg", "ell", "err", "fee", "gee", \
"goo", "ill", "inn", "lee", "Lee", "loo", "moo", "nee", "odd", "off", \
"Orr", "pee", "see", "shh", "tee", "too", "wee", "woo", "WWW", "zoo"}
$\endgroup$
3
$\begingroup$
p = {a_, a_, c_, d_, b_, b_};

Select[DictionaryLookup[], 
  MatchQ[Characters@#, {OrderlessPatternSequence @@ p}] &];

Length[%]

793

$\endgroup$
1
  • $\begingroup$ Thanks for your effort . Also StringJoin @@@ Cases[Characters /@ DictionaryLookup[], {OrderlessPatternSequence @@ {a_, a_, c_, d_, b_, b_}}, 1]; but I wanted to explore if the usage with StringExpression could somehow be written in a different way. $\endgroup$
    – Syed
    Commented Nov 7 at 14:24

Your Answer

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

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