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

各元のテキストにはいくつかのスピン オプションを含めることができるため、各単語はインデックス ポインティング スピン オプション ワードを開始しています...

例: "man" の最初の出現にはスピンオプション "{man|Person}" があります.. "man" の 2 番目の出現にはスピンオプション "{man|male}" があります...

元のテキストが変更された場合、すべての単語インデックスが変更されます...新しいインデックスを指定したい...

 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);
     }            
 }

これが私がやっている方法です...

この辞書キーは、文字列内の各単語のインデックスです

助けてください よろしくお願いします!

4

3 に答える 3

0

文字列は C# では不変です。これは、一度設定すると変更されないことを意味します。変数を再度設定すると、実際には新しい文字列が割り当てられ、古い文字列はクリーンアップされます (参照されなくなった場合)。

必要なものを実現するには、オブジェクトから文字列を取り出す必要があります。要求されるたびに、文字列ビルダーを使用してプロパティを生成することをお勧めします。また、dirty フラグを使用して、辞書が変更されたときに生成する必要があるかどうかを確認することもできます。

于 2013-08-30T03:13:52.907 に答える
0

文中の単語を置き換える場合は、 string.Replace() ベースのアプローチを使用します

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

このようにして、インデックスが常に変化するサブ文字列を交換することを気にする必要はありません。

于 2013-08-30T03:27:36.447 に答える