Java 如何从字符串集映射得到笛卡尔积
这可能与我的问题相似,但没有回答我的问题 我创建了以下内容Java 如何从字符串集映射得到笛卡尔积,java,string,dictionary,treemap,Java,String,Dictionary,Treemap,这可能与我的问题相似,但没有回答我的问题 我创建了以下内容 TreeMap<String, Set<String>> aMapOfSet 假设单词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
TreeMap<String, Set<String>> aMapOfSet
假设单词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
我用树形图来保存单词的顺序
这是我正在进行的工作代码:
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个句子的最佳方法是什么。这可能是使用递归的好时机:
Set<String> getCartesianProduct(List<String> originalWords, TreeMap<String, Set<String>> wordVariationSet) {
Set<String> resultSet =new HashSet<String>(); // to store answer
varyWord(resultSet, "", originalWords, wordVariationSet, 0); // begin recursion with empty sentence
return resultSet; // return result
}
void varyWord(Set<String> result, String sentence, List<String> originalWords, Map<String, Set<String>> wordVariationSet, int index) {
if (index==originalWords.size()) { // no more words to vary -> sentence is complete
result.add(sentence); // add to results
return; // done (return from recursion)
}
if (index>0) sentence += " "; // add a space if working on any but first word
String theOriginalWord = originalWords.get(index); // grab original word
varyWord(result, sentence + theOriginalWord, originalWords, wordVariationSet, index+1); // add to sentence and vary next word
Set<String> wordVariations = wordVariationSet.get(theOriginalWord); // grab variations of this word
if (wordVariations!=null) // make sure they're not null
for(String word: wordVariations) // iterate over variations
varyWord(result, sentence + word, originalWords, wordVariationSet, index+1); // add to sentence and vary next word
}
我希望这段代码是不言自明的。如果没有,请告诉我,我可以补充一些细节
有几件事需要注意:
你写道,我使用树形图来保留单词的顺序,但不幸的是,树形图是根据键的自然顺序排序的,在本例中是根据字母表排序的,而不是根据它们被添加的时间。这就是为什么我将列表originalWords作为一个参数,它保留了顺序。所以,您需要初始化它,并创建一个ArrayList,然后在您放置。。。在aMapOfSet中添加一个单词,也可以添加。。。它被列入名单。
这段代码缺少一些检查,比如对originalWords和wordVariationSet进行空检查,检查wordVariationSet是否包含与originalWords相同的单词。。。
如果原始单词句子包含同一单词两次,wordVariationSet将无法处理每个单词的不同变体。相反,你的第二把。。。将覆盖您的第一个。
你可以使用谷歌番石榴或者看看它的实现。见@MichaelEaster+1谢谢+这就是我要找的。我会测试的。只是更新了一些需要注意的事情。特别是第3点可能是相关的!您知道,为wordVariationSet使用ArrayList可能会更容易,只需按照将单词添加到originalWords的相同顺序将项目添加到ArrayList中,只需确保根据需要向其中添加空的或空的条目即可。这样,您也可以避免上面的第3版,当然,除非您希望一个单词有一组变体,即使该单词出现多次,在这种情况下,第3版将是一个功能,而不是一个bug。谢谢!没错,我在这里发布了这篇文章之后,实际上最终使用了ArrayList。因为,正如你所指出的,TreeMap没有给出正确的答案。
Set<String> getCartesianProduct(List<String> originalWords, TreeMap<String, Set<String>> wordVariationSet) {
Set<String> resultSet =new HashSet<String>(); // to store answer
varyWord(resultSet, "", originalWords, wordVariationSet, 0); // begin recursion with empty sentence
return resultSet; // return result
}
void varyWord(Set<String> result, String sentence, List<String> originalWords, Map<String, Set<String>> wordVariationSet, int index) {
if (index==originalWords.size()) { // no more words to vary -> sentence is complete
result.add(sentence); // add to results
return; // done (return from recursion)
}
if (index>0) sentence += " "; // add a space if working on any but first word
String theOriginalWord = originalWords.get(index); // grab original word
varyWord(result, sentence + theOriginalWord, originalWords, wordVariationSet, index+1); // add to sentence and vary next word
Set<String> wordVariations = wordVariationSet.get(theOriginalWord); // grab variations of this word
if (wordVariations!=null) // make sure they're not null
for(String word: wordVariations) // iterate over variations
varyWord(result, sentence + word, originalWords, wordVariationSet, index+1); // add to sentence and vary next word
}