1
string originaltext = "A man meet a man";
string spintext ="A {man|Person} meet a {man|male}";

Each original text can have some spin options, so each word is starting index pointing spin option word...

For example : First occurance of "man" have spinoption "{man|Person}".. Second occurance of "man" have spinoption "{man|male}"...

If original text changed then all word index is changing... So that I want to point new index...

 public Dictionary<int, SpintaxMappedValue> SpintaxListDict
 {
     get
     {
         return _spintaxListDict;
     }
     set
     {
         _spintaxListDict = value;
     }
 }

 internal static void AlterSpintaxList(string _NeworiginalText)
 {
     //Build new dictionary for current text
     var _NeworiginalTextDict = Regex.Matches(_NeworiginalText, @"\b\w+\b").Cast<Match>().ToDictionary(m => m.Index, m => m.Value);

     //Comparing old original value dict with new original value dict
     bool dictionariesEqual =
         _NeworiginalTextDict.Keys.Count == Init.SpintaxEditorPropertyMain.OriginalTextDict.Keys.Count &&
         _NeworiginalTextDict.Keys.All(k => Init.SpintaxEditorPropertyMain.OriginalTextDict.ContainsKey(k) && 
         object.Equals(Init.SpintaxEditorPropertyMain.OriginalTextDict[k], _NeworiginalTextDict[k]));

     //do mapping if dictionaries are not Equal
     if (dictionariesEqual == false)
     {
         Dictionary<int, SpintaxMappedValue> TempSpintaxDict = new Dictionary<int, SpintaxMappedValue>(Init.SpintaxEditorPropertyMain.SpintaxListDict);
         Dictionary<int, SpintaxMappedValue> NewSpintaxListDict = new Dictionary<int, SpintaxMappedValue>();

         foreach (KeyValuePair<int, SpintaxMappedValue> olditem in TempSpintaxDict)
         {
             foreach (KeyValuePair<int, string> newitem in _NeworiginalTextDict)
             {
                 if (olditem.Key != newitem.Key)
                 {
                     if (olditem.Value.OriginalWord == newitem.Value)
                     {
                         //if (newitem.Key > olditem.Key)
                         //{
                         olditem.Value.OriginalWordStartingPosition = newitem.Key;
                         olditem.Value.SpinWordStartingPosition = newitem.Key;
                         NewSpintaxListDict.Add(newitem.Key, olditem.Value);
                         //}
                         break;
                     }
                 }                        
             }
         }

         Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<int, SpintaxMappedValue>(NewSpintaxListDict);
     }            
 }

This is how i am doing...

This dictionary key is index of each word in the string

Please help me out Thanks in advance!

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
Jay
  • 33
  • 5
  • 3
    I think you need to edit your question a bit. It's not making sense to me. – Enigmativity Aug 30 '13 at 03:13
  • use stringbuilder instead of string... – Maruti Aug 30 '13 at 03:18
  • Maybe I'm not fully understanding what you're trying to do, but Dictionary seems like the wrong structure to be using. Why aren't you just using an array to store the words? With a Dictionary, you'll basically need to rebuild some or all of it each time you modify the larger string. – hatchet - done with SOverflow Aug 30 '13 at 03:27
  • I believe he would like to link the two. If you change one the other updates. This isn't possible with a string type due to it being immutable. – Jason Aug 30 '13 at 03:31
  • It looks to me like he's building a keyed list of word starting positions and the words at those positions. I don't think the immutable nature of strings is an issue here. – hatchet - done with SOverflow Aug 30 '13 at 03:46
  • Unless this structure exists in order to support other functionality I would consider string.replace instead – TGH Aug 30 '13 at 03:49
  • Please read this http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem and consider editing your question. – hatchet - done with SOverflow Aug 30 '13 at 03:52
  • I use Can IDynamicMetaObjectProvider to solve this – Jay Aug 30 '13 at 04:45
  • If the issue is to replace words in a string then have a look at http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words – Jason Aug 30 '13 at 05:41

3 Answers3

0

String are immutable in C#. This means that once they are set they don't change. When you set the variable again you are actually assigning a new string, and the old one is cleaned up (if no longer referenced).

String would have to come off your object to achieve what you require, and I would suggest using a string builder to generate the property each time is requested. Also you could use a dirty flag to see if it needs to be generated when the dictionary has changed.

Jason
  • 840
  • 11
  • 23
  • From what I understand IDynamicMetaObjectProvider doesn't stop string from being immutable. How were you thinking of using IDynamicMetaObjectProvider? – Jason Aug 30 '13 at 04:55
  • Try this answer http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words – Jason Aug 30 '13 at 05:40
0

If you're replacing words in a sentence I would just go with a string.Replace() based approach

originalString = originalString.Replace("man","women and man");

This way you don't need to care about swapping out sub strings with ever changing indexes.

TGH
  • 38,769
  • 12
  • 102
  • 135
0
var original = "A man meet a man";

//updated in response to comment
//var dict = Regex.Matches(original, @"\b\w+\b").Cast<Match>().ToDictionary(m => m.Index, m=> m.Value);

var dict = Regex.Matches(original, @"\b\w+\b")
    .Cast<Match>()
    .GroupBy(m => m.Value, m=> m.Index)
    .ToDictionary(g => g.Min(), g=> g.Key);

When the string changes, recreate the dictionary.

To use a dictionary:

string s = dict[2];
Console.WriteLine(s=="man"); //prints True

dict[2] = "new string";
Console.WriteLine(dict[2]=="new string"); //prints True
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • What do you want it to do? Do you want it only once in the dictionary? – Zev Spitz Aug 30 '13 at 05:30
  • The index is mapping List of values... How can i move List of values to the corresponding index – Jay Aug 30 '13 at 05:42
  • Dictionary – Jay Aug 30 '13 at 05:43
  • Is there any way to move MappedValue also in the new dictionary – Jay Aug 30 '13 at 06:02
  • Yes Thank you... But I want all words in the string with key, Each idex mapping some list...Actually I want to update new index of the Existing list(if the string changed)... – Jay Aug 30 '13 at 09:08
  • @user2731383 Please update your question with post some C# code demonstrating what you want to do with this collection. Use an indexer to indicate what kind of value should be going in and out of the collection. Use a `foreach` loop to show collections. See my answer for an example. – Zev Spitz Aug 30 '13 at 09:50
  • @user2731383 Why can't you recreate the collection? Again, please post some C# code (not just a type and a pair of variable assignments) that demonstrates why you need to reuse the collection. – Zev Spitz Aug 30 '13 at 09:50
  • @user2731383 I repeat my previous two comments. You have not posted some C# demonstrating how you want to **use** this collection, given an `originaltext`; you have posted another unrelated **definition** of the collection. It is still very unclear from your question exactly what it is you want to do. I understand that you are note vey fluent in English, but perhaps you can make yourself better understood by showing **via code** how you want to use this collection. – Zev Spitz Aug 31 '13 at 18:55