• 0

Generate set of words from a sentense


Question

Hi,

 

Not sure how to describe this but I need quick help with some code. Generally I do this easily but this must be one of those days after a long and heavy weekend.

 

Given a sentence, "This is test statement"; I need to produce a list of words in the combination like shown below.

 

this

this is

this test

this statement

this is test

this is statement

this is test statement

 

Cheers :)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

String.Split is given. Random? Like I said I am not in the zone today.

string sentence = "This is a sentence."
string[] words = sentence.Split(' ');
This will separate the sentence into an array of the words.

 

int wordIndex = new Random().Next(0, words.Length - 1);
to pick random words. Once you have the amount you want you can use:

 

string fragment = string.Join(wordArray, " ");
Combine the selected words into a space-delimited string. If you want to ensure that words are not reused for each sentence fragment you'd want to keep track of the word index for each iteration. (Perhaps by putting the indices in a List and remove them as they're selected in the Random portion of the code.)
Link to comment
Share on other sites

  • 0

Hey, i guess you meant something like this?

static void Main(string[] args)
{
    string sentence = "this is test statement";

    string[] parts = sentence.Split(' ');

    int maxIndex = parts.Length;
    int currentIndex = 1;

    List<string> results = new List<string>();

    results.Add(parts[0]);

    while (currentIndex < maxIndex)
    {
        string str = "";

        //build up string
        for (int i = 0; i < currentIndex; i++)
        {
            str += parts[i] + " ";
        }

        //iterate over next parts
        for (int i = currentIndex; i < maxIndex; i++)
        {
            results.Add(str + parts[i]);
        }

        currentIndex++;
    }

    foreach (string str in results)
    {
        Console.WriteLine(str);
    }

    Console.ReadKey();
}

if you ever need a more complex form of permutation you could also try this class 

class PermuteState<T>
{
    private int index;
    private int iterations;

    public PermuteState<T> NextState = null;

    public T[] possibilities;

    public PermuteState(T[] Possibilities)
    {
        index = 0;
        iterations = Possibilities.Length;
        possibilities = Possibilities;
    }

    public bool NextPermute(List<T> state)
    {
        bool iterationComplete = false;

        Permute(state, ref iterationComplete);

        return iterationComplete;
    }

    private void Permute(List<T> state, ref bool iterationComplete)
    {
        state.Add(possibilities[index]);

        //call the next state
        if (NextState != null)
        {
            bool nextIterationComplete = false;

            NextState.Permute(state, ref nextIterationComplete);

            if (nextIterationComplete)
            {
                index++;
            }
        }
        else
        {
            index++;
        }

        //done with this iteration
        if (index == iterations)
        {
            index = 0;
            iterationComplete = true;
        }
    }

    public static PermuteState<T> ChainPermutes(List<PermuteState<T>> permutes)
    {
        if (permutes.Count < 1)
            return null;

        PermuteState<T> currentState = permutes[0];

        for (int i = 1; i < permutes.Count; i++)
        {
            currentState.NextState = permutes[i];

            currentState = currentState.NextState;
        }

        return permutes[0];
    }

    public static PermuteState<T> GetPermuteChain(T[] possibilities, int depth = -1)
    {
        if (depth == -1)
        {
            depth = possibilities.Length;
        }

        List<PermuteState<T>> Permutes = new List<PermuteState<T>>();

        for (int i = 0; i < depth; i++)
        {
            Permutes.Add(new PermuteState<T>(possibilities));
        }

        return ChainPermutes(Permutes);
    }
}

example usage:

string sentence = "this is test sentence";

string[] parts = sentence.Split(' ');

PermuteState<string> permuter = PermuteState<string>.GetPermuteChain(parts);

while (true)
{
    List<string> results = new List<string>();

    bool done = permuter.NextPermute(results);

    StringBuilder str = new StringBuilder();
    foreach (string part in results)
    {
        str.Append(part + " ");
    }

    Console.WriteLine(str.ToString());

    if (done)
        break;
}

Regards

C0rrupted

Link to comment
Share on other sites

  • 0

Hey, i guess you meant something like this?

static void Main(string[] args)
{
    string sentence = "this is test statement";

    string[] parts = sentence.Split(' ');

    int maxIndex = parts.Length;
    int currentIndex = 1;

    List<string> results = new List<string>();

    results.Add(parts[0]);

    while (currentIndex < maxIndex)
    {
        string str = "";

        //build up string
        for (int i = 0; i < currentIndex; i++)
        {
            str += parts[i] + " ";
        }

        //iterate over next parts
        for (int i = currentIndex; i < maxIndex; i++)
        {
            results.Add(str + parts[i]);
        }

        currentIndex++;
    }

    foreach (string str in results)
    {
        Console.WriteLine(str);
    }

    Console.ReadKey();
}

Regards

C0rrupted

 

Hey thanks.

 

I will use the simpler approach as in future this will be replaced by the proper full-text search anyways. Your solution produces linear output which is close to what I need but missing out on a lot of other combinations. For the time being I am going to use the binary approach as that has the problem fixed.

Link to comment
Share on other sites

This topic is now closed to further replies.