I have a C# problem at the moment that I haven’t been able to get my head around. Essentially I need to generate a list or array of strings which are sentences based on the fact that in the sentence, one or more words may have different spellings or uses. I intend to have a number of different possibilities for (potentially) each word in the sentence.
For example if I define that the word ‘are’ could be written as ‘are’ or ‘r’ and the word ‘you’ be written ‘you’ or ‘u’, expected output for passing something along the lines of “how are you” would be:
- “how are you”
- “how r you”
- “how are u”
- “how r u”
I’ve considered that I could use Enums for word types, e.g:
public enum Word
{
Are,
You
}
and return an array of possible uses using some kind of helper method:
public static string GetVariants(Word w)
{
switch(w)
{
case Word.Are:
return new string[] { "are", "r" };
case Word.You:
return new string[] { "you", "u"};
}
But I cannot seem to find a decent way to define a sentence using a mix of fixed strings and these variable word type identifiers and create possible combinations.
The words need to be in the right order as they would be written, I just need to be able to generate a number of different ways of writing the same thing. Once I’ve got something going with this, I’d also like for it to be not just applicable to this particular sentence structure. Is this possible?
Incase anybody comes across a similar problem, I eventually got this working as I wanted using a hack of Eric Lippert’s Cartesian Product solution:
http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx