On Wed, 17 Sep 2008 13:28:31 -0700, <mo*****@gmail.comwrote:
There is no predefined phrase to be searched. I am assuming that a
complete sentence can be a phrase.
Okay, so in the context of this problem, does that mean that "a phrase" is
simply "a complete sentence"? Or can "a phrase" be some subset of "a
complete sentence"?
As long as we can assume that you already have code to parse out "a
phrase" and that the phrase is always represented by an instance of the
String class, I think your other questions can be answered without the
above information. But having that information would sure help us to
better understand what you're doing.
So I can open a txt file read all
the sentences in an array and loop and find out the longest repetitive
phrase, but I cannot use Dictionary or Hast Table to store my info
because the key value has to be distinct.
I don't understand that statement. Based on your description of the
problem so far, any two "phrases" that are not distinct would be
considered the same, and thus would simply increment the counter in your
Dictionary.
It's true that a Dictionary can only have one element per given key, but
it seems like in this case that would be fine. What about that rule is
causing you trouble?
I am setting the phrase as
key value. Now before inserting into a dictionary/hashtable I can
check whether the key exists or not, if it already exists I only want
to increment its value. But am not able to do so.
Why not? What problem are you having?
For the record: you have asked your questions in a very vague,
nearly-useless way. You say you've had problems, but you don't describe
the problems in any specific way. You are obviously doing this in the
context of some actual code, but you haven't posted any actual code.
People can only help you insofar-as you provide enough detail for them to
answer your questions.
That said, applying a bit of my "psychic debugging" skills, I'll point out
that when dealing with a Dictionary that contains value types or immutable
reference types as the TValue parameter, the only way to update the value
associated with a specific key is to replace it.
So, if you've got a Dictionary<string, int>, then when you find that the
key already exists in the Dictionary, to increment your counter you need
to get the current value, add one to it, and store the new result back
into the Dictionary. Something like this won't work:
Dictionary<string, intdict = ...;
string strPhrase = ...;
// ...
dict[strPhrase]++;
You need something more like this:
dict[strPhrase] = dict[strPhrase] + 1;
The first version attempts to increment what's returned by
"dict[strPhrase]", which works in the sense that the value can be
incremented, but doesn't work in that there's nothing in that line of code
that stores the newly incremented value back to the Dictionary. The
expression "dict[strPhrase]" doesn't refer to the actual value in the
Dictionary, but rather the value returned by the indexer.
The second version corrects this problem by providing the compiler with
explicit instructions to get the current value, add one to it, and store
the result back into the Dictionary.
If that doesn't help, you'll need to be less vague about your question.
We can only get so far by guessing.
Pete