Combinations Of String While Maintaining Order Of Words

我是研究僧i 提交于 2019-12-20 05:23:08

问题


Given a string:

String words = "Mary had a little lamb";

how to obtain a combination of sentence fragments while the order of occurrence of words in the original sentence is maintained ???

example:

{'Mary had a little lamb'}
{'Mary had a little', 'lamb'}
{'Mary had a', 'little lamb'}, {'Mary had a', 'little', 'lamb'}
{'Mary had', 'a little lamb'}, {'Mary had', 'a little', 'lamb'}, {'Mary had', 'a', 'little lamb'}, {'Mary had', 'a', 'little', 'lamb'}
{'Mary', 'had a little lamb'}, {'Mary', 'had a little', 'lamb'}, {'Mary', 'had a', 'little lamb'} and so on...

Thanks in advance :)


回答1:


Think about it this way:

Mary <1> had <2> a <3> little <4> lamb

Each of these <number>s can be either true or false. If it is true, then you cut the sentence in that location.

So, if you have n+1 words, your problem gets reduced to going through binary representation of numbers with n bit, that is from 0 to 2^n-1

Examples:

0110 -> {'Mary had', 'a', 'little lamb'}
1111 -> {'Mary', 'had', 'a', 'little', 'lamb'}
0001 -> {'Mary had a little', 'lamb'}
1011 -> {'Mary', 'had a', 'little', 'lamb'}



回答2:


To get the output shown in your question, though not in the same order, this is what I would do.
I will be using Mathematica code, but the concepts are universal.

string = "Mary had a little lamb";
set = StringSplit[string]
n = Length@set
{"Mary", "had", "a", "little", "lamb"}
5

So you will need a function that breaks the sentence into words (StringSplit).

Then you will need a function to generate integer partitions and a permutation function that is aware of duplicate elements. Algorithms for both can be found here on StackOverflow.

IntegerPartitions[n]
{{5}, {4, 1}, {3, 2}, {3, 1, 1}, {2, 2, 1}, {2, 1, 1, 1}, {1, 1, 1, 1, 1}}

Once we permute each partition ("for each" is /@) we get all ways to linearly split a set of five parts:

parts = Join @@ Permutations /@ IntegerPartitions[n]
{{5}, {4, 1}, {1, 4}, {3, 2}, {2, 3}, {3, 1, 1}, {1, 3, 1},
 {1, 1, 3}, {2, 2, 1}, {2, 1, 2}, {1, 2, 2}, {2, 1, 1, 1}, {1, 2, 1, 1},
 {1, 1, 2, 1}, {1, 1, 1, 2}, {1, 1, 1, 1, 1}}

Finally we need a function to split a set according to a sequences of lengths. I call mine dynamicPartition:

dynamicPartition[set, #] & /@ parts // Column
{{Mary,had,a,little,lamb}}
{{Mary,had,a,little},{lamb}}
{{Mary},{had,a,little,lamb}}
{{Mary,had,a},{little,lamb}}
{{Mary,had},{a,little,lamb}}
{{Mary,had,a},{little},{lamb}}
{{Mary},{had,a,little},{lamb}}
{{Mary},{had},{a,little,lamb}}
{{Mary,had},{a,little},{lamb}}
{{Mary,had},{a},{little,lamb}}
{{Mary},{had,a},{little,lamb}}
{{Mary,had},{a},{little},{lamb}}
{{Mary},{had,a},{little},{lamb}}
{{Mary},{had},{a,little},{lamb}}
{{Mary},{had},{a},{little,lamb}}
{{Mary},{had},{a},{little},{lamb}}



回答3:


You have 4 word delimiters (spaces) here. Every space may be replaced (or not) by sentence delimiter. So there are 16 = 2^4 cases, which correspond to binary numbers 0000...1111.



来源:https://stackoverflow.com/questions/8952717/combinations-of-string-while-maintaining-order-of-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!