我有一个有趣的问题,我无法 - 为了我的生活 - 找出一个好的解决方案。我将得到一个包含 0+ 个“标记”的短语。例如:
现在 %A1% 牛怎么样。%A2% 的 %A3% 形状奇特。
上面,%A1%
和%A2%
都是%A3%
“标记”。每个标记都有自己的可能单词列表,可以用来代替它:
public class Token {
// Ex: %A1%
private String code;
// Ex: "brown", "red", "silly"
private List<String> candidates;
}
我需要编写一些代码来获取任意短语(不仅仅是上面的示例)并扫描它是否存在令牌。如果找到了标记,那么我需要它使用每个标记的候选列表的每个组合来生成短语的每个排列。例如,如果上述 3 个令牌存在以下候选:
%A1%
====
brown
red
silly
%A2%
====
arsonist
banker
%A3%
====
feet
hands
然后将生成以下句子排列:
How now brown cow. The arsonist had oddly-shaped feet.
How now brown cow. The arsonist had oddly-shaped hands.
How now brown cow. The banker had oddly-shaped feet.
How now brown cow. The banker had oddly-shaped hands.
How now red cow. The arsonist had oddly-shaped feet.
How now red cow. The arsonist had oddly-shaped hands.
How now red cow. The banker had oddly-shaped feet.
How now red cow. The banker had oddly-shaped hands.
How now silly cow. The arsonist had oddly-shaped feet.
How now silly cow. The arsonist had oddly-shaped hands.
How now silly cow. The banker had oddly-shaped feet.
How now silly cow. The banker had oddly-shaped hands.
因为有 3 个可能的值,并且%A1%
两者都有 2 个可能的值,所以我们总共有 3 x 2 x 2 = 12 个排列。%A2%
%A3%
如果我们在短语中总是有固定数量的标记,那么问题就容易多了(至少对我来说)。但问题是:
- 我们不确定该短语将包含多少个标记(它甚至可能包含 0 个标记);和
- 我们不知道每个短语中会出现哪些标记(以及哪些候选列表),因此我们需要扫描短语并获取标记列表,然后能够动态地将它们放入“排列生成器”中需要。
出于某种原因,我无法将我的大脑包裹在这个周围。关于如何编码的任何想法?提前致谢!