【发布时间】:2014-06-18 14:09:28
【问题描述】:
这可能类似于Java : Cartesian Product of a List of Lists,但没有回答我的问题。
我创建了以下
TreeMap<String, Set<String>> aMapOfSet
aMapOfSet 表示句子中的不同单词,Set<String> 将包含单词的所有变体,如果没有变体,则该单词键的 set 将为空/null。
我想写一个方法,它接受 aMapOfSet 并返回一个包含所有可能句子的集合。
例如,原句可以是:
tss xes wxy xyz
假设单词“wxy”总共有 3 个变体,单词“xyz”总共有 2 个变体
那么aMapOfSet 应该是这样的
tss
xes
wxy -> [wxys,wxyes]
xyz -> [xyzs]
答案是 resultSet 中的 6 个句子
tss xes wxy xyz
tss xes wxys xyz
tss xes wxyes xyz
tss xes wxy xyzs
tss xes wxys xyzs
tss xes wxyes xyzs
我使用 treeMap 来保存单词的序列。
这是我正在进行的代码:
Set<String> getCartesianProduct(TreeMap<String, Set<String>> wordVariationSet)
{
Set<String> resultSet =new HashSet<String>();// to store answer
for(String theOriginalWord: wordVariationSet.keySet())
{
for(String word:wordVariationSet.get(theOriginalWord))
{
// TODO create a sentence with 1 space between words and add to resultSet
}
}
return resultSet;
}
随着我的进步,我会更新代码。
遍历所有变体的最佳方法是什么,以便获得所有 6 个句子。
【问题讨论】:
-
您可以使用 Google Guava 或查看其实现。见docs.guava-libraries.googlecode.com/git/javadoc/com/google/…
-
@MichaelEaster +1 谢谢!
标签: java string dictionary treemap