\begingroup
我使用的是 v12.2.0。作为一个最小示例,想法是找到所有包含两对匹配字符和两个其他字符的单词,总共包含6
字符。这个问题类似于(正如我后来发现的那样)。
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]
真的
{}
{“更诡异”}
在云端尝试这些模式,结果每个都有 793 个条目。这似乎是我的版本的一个错误。所以,我想以某种方式合并OrderlessPatternSequence
。
问题
我的问题是,是否StringExpression
可以结合使用OrderlessPatternSequence
而不是依赖Permutations
于此类任务?为了提高可读性,可以Repeated
结合这种模式吗?
\endgroup
1
最佳答案
2
\begingroup
它似乎DictionaryLookup
不是为与早期版本一起工作而设计的Alternatives
,StringExpression
并且只返回第一个替代方案的结果。
相反,它可以Alternatives
应用于单个字符。
第一行与 一起工作"e" | "t"
,但是第四行DictionaryLookup[(x_ ~~ x_ ~~ y_) | (x_ ~~ y_ ~~ y_)]
返回的结果就像只是 一样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"}
您可以使用以下解决方法:
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
|
\begingroup
p = {a_, a_, c_, d_, b_, b_};
Select[DictionaryLookup[],
MatchQ[Characters@#, {OrderlessPatternSequence @@ p}] &];
Length[%]
793
\endgroup
1
-
\begingroup
感谢您的努力。但是我想探索是否可以用不同的方式来写StringJoin @@@ Cases[Characters /@ DictionaryLookup[], {OrderlessPatternSequence @@ {a_, a_, c_, d_, b_, b_}}, 1];
with 的用法。StringExpression
\endgroup
–
|
要在 StringExpression 上使用 OrderlessPatternSequence,您需要将字符串更改为字符列表。
\endgroup
–
|