-
Aug 11th, 2014, 08:34 PM
#1
Thread Starter
^:^...ANGEL...^:^
[RESOLVED] Generate set of words from a sentense
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
Last edited by wrack; Aug 12th, 2014 at 06:31 PM.
-
Aug 11th, 2014, 08:34 PM
#2
Thread Starter
^:^...ANGEL...^:^
Generate set of words from a sentense
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
-
Aug 11th, 2014, 08:46 PM
#3
Re: Generate set of words from a sentense
Firstly, what are the requirements? You've provided an example but an example doesn't define the requirements; it simply exemplifies them. I could recite more than one set of rules that, when obeyed, would produce that result. We don't even know if the example displays a full output or just a partial one.
Secondly, have you considered the logic without considering how to implement it? You should not be writing code without knowing what that code has to do. I don't mean the results it has to produce but rather the steps it has to perform to produce those results. That part isn't even a programming problem. If someone handed yo a pen and paper and asked you to write down the list, what logic would you use to do it?
Finally, with your post count, you should know not to post the same question twice. I've asked the mods to delete the other one. If this is not specifically a VB or C# question then it shouldn't be in either forum. If it is for one language then it should only be in the forum for that language.
-
Aug 11th, 2014, 08:58 PM
#4
Thread Starter
^:^...ANGEL...^:^
Re: Generate set of words from a sentense
Hey John,
Thanks for the reply. It has been a while since I have been here so I didn't know there was a common forum for language agnostic questions. Apologies.
Now to answer your question. The requirement is to break apart a sentence and generate a list of words that act like a search tags. It must have the words of the sentence in the order in which the statement is formed and the generated tags should get progressively use all words in the sentence.
So given a sentence, "This is test statement"; below are the words that are not valid.
is
test
statement
is test
is statement
test statement
is test statement
So the first word is the driver. Hopefully this makes sense.
Cheers
-
Aug 11th, 2014, 09:23 PM
#5
Re: Generate set of words from a sentense
So, to be clear, you need to find all combinations of words of any length that contain the first word of the original sentence and where the words appear in the same order as the original sentence, correct? Is there any rule around duplicate words or is each instance just treated like any other word?
-
Aug 11th, 2014, 10:14 PM
#6
Thread Starter
^:^...ANGEL...^:^
Re: Generate set of words from a sentense
Yeah that is pretty much right. Original sentence will have duplicate words as well as noise words removed anyways so purely a routine that would generate the list.
I am working on a permutation routine in the meantime and see what I come up with but any help or a different approach will surely help.
-
Aug 11th, 2014, 10:24 PM
#7
Re: Generate set of words from a sentense
This is a situation where recursion is appropriate. You could define a method that takes two lists of Strings and creates a new list for each String in the second list that consists of all the items from the first list with the item from the second list appended. The method would then call itself again for each of those lists, passing the new list and another new list that included all the items from the second list after the String that was appended to the first list. That's the basic logic. You can flesh that out to an algorithm and then implement it in code.
-
Aug 11th, 2014, 10:27 PM
#8
Thread Starter
^:^...ANGEL...^:^
Re: Generate set of words from a sentense
Thanks. I am working on it.
Further more clarification. Given the sentence "This is test statement"; the possible combinations are,
this
is
test
statement
this is
this test
this statement
is test
is statement
test statement
this is test
this is statement
this test statement
is test statement
this is test statement
As you can see the order or the words is same as they appear in the original statement.
From the list anything that starts with the first word "this" is valid, rest are throw away.
-
Aug 11th, 2014, 11:35 PM
#9
Thread Starter
^:^...ANGEL...^:^
Re: Generate set of words from a sentense
Ok, here goes nothing. Taking binary approach.
Code:
const string original = "This is test statement and it is working";
var splitdata = original.ToLowerInvariant().Split(new[] { ' ' });
var searchTags = new List<string>();
if (splitdata.Length > 1)
{
var possibleCombinations = (int)Math.Pow(splitdata.Length, 2);
var binaryCombinations = new List<string>();
for (int nIndex = 1; nIndex < possibleCombinations; nIndex++)
{
var result = Reverse(Convert.ToString(nIndex, 2).PadLeft(splitdata.Length, '0'));
if (!result.StartsWith("1", StringComparison.OrdinalIgnoreCase))
continue;
binaryCombinations.Add(result);
}
foreach (var binaryCombination in binaryCombinations)
{
var nIndex = 0;
var resultWord = string.Empty;
foreach (var wordIndex in binaryCombination.ToCharArray())
{
if (wordIndex.Equals('1'))
{
resultWord += splitdata[nIndex] + " ";
}
nIndex++;
}
searchTags.Add(resultWord.Trim());
}
}
-
Aug 12th, 2014, 07:07 AM
#10
Re: Generate set of words from a sentense
You can split based on a space, and then loop through the results to print it. You would need to use 2 for loops.
Example:
Code:
string statement = "This is a test statement";
string[] splitStatement = statement.Split(' ');
for (int i = 0; i <= splitStatement.Length; i++)
{
for (int x = 0; x < i; x++)
{
Console.Write(splitStatement[x] + " ");
} //end for
Console.WriteLine("");
} //end for
-
Aug 12th, 2014, 06:31 PM
#11
Thread Starter
^:^...ANGEL...^:^
Re: Generate set of words from a sentense
Originally Posted by kfcSmitty
You can split based on a space, and then loop through the results to print it. You would need to use 2 for loops.
Thanks. That will produce a subset of desired output but not all.
Code:
This
This is
This is test
This is test statement
It will not produce the combinations below.
Code:
this test
this statement
this is statement
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|