【问题标题】:Add parsed string list to Dictionary wpf c#将解析的字符串列表添加到字典 wpf c#
【发布时间】:2012-11-10 17:32:08
【问题描述】:

我正在尝试将话语解析为单词并将这些单词保存在列表中。我想用话语数字键将每个列表添加到字典中。我想将每个话语与其他话语的相似度进行比较。我试过了,但没有用。谁能帮帮我!

谢谢

public string[] utterance = new string[4];

    Dictionary<string, List> wording = new Dictionary<string, List>();

    public void splitit()
    {          
     utterance[0] = "Fish attacked Nemo's parents";
     utterance[1] = "Only one fish egg left after fish attacked Nemo's parents and that was Nemo.";
     utterance[2] = "Nemo grow up and went to school.";
     utterance[3] = "Nemo got bored during the lecture and went to ocean with his friends.";

        for (int x=0; x < 4; x++)
        {

            string[] words = utterance[x].Split(' ');
            List<string> Tokens = new List<string>();

            foreach (string word in words)
            {
                Tokens.Add(word);
            }

            //string parsed = Tokens[1];
            //foreach(string tok in Tokens)
            //{
            //   List<string> listing = new List<string>();
            //    listing.Add (tok);
            //    wording.Add("utterance"+x, listing);
            //    //listBox1.Items.Add("utterance"+x+" : "+tok);
            //}

            for (int w = 0; w < 4; w++)
            { 
            wording.Add("utterance"+x,Tokens);
            }

        }
    }

}

我这样解决了这个问题,现在可以了:

for (int x=0; x

            string[] words = utterance[x].Split(' ');
            ArrayList Tokens = new ArrayList();


            foreach (string word in words)
            {
                Tokens.Add(word);
            }


            ArrayList listing = new ArrayList();
            foreach (string tok in Tokens)
            {
                listing.Add(tok);
            }
                wording.Add("utterance" + x, listing);

               counting = wording["utterance0"].Count;

      }

【问题讨论】:

  • 请定义“它不起作用”

标签: c# wpf list dictionary


【解决方案1】:

只需改变几件事:

像这样声明DisctionaryDictionary&lt;string, List&lt;string&gt;&gt;,最后为每个句子添加tockens,像这样wording.Add(utterance[w],Tokens);

Dictionary<string, List<string>> wording = new Dictionary<string, List<string>>();

public void splitit()
{          
 utterance[0] = "Fish attacked Nemo's parents";
 utterance[1] = "Only one fish egg left after fish attacked Nemo's parents and that was Nemo.";
 utterance[2] = "Nemo grow up and went to school.";
 utterance[3] = "Nemo got bored during the lecture and went to ocean with his friends.";

    for (int x=0; x < 4; x++)
    {

        string[] words = utterance[x].Split(' ');
        List<string> Tokens = new List<string>();

        foreach (string word in words)
        {
            Tokens.Add(word);
        }

        //string parsed = Tokens[1];
        //foreach(string tok in Tokens)
        //{
        //   List<string> listing = new List<string>();
        //    listing.Add (tok);
        //    wording.Add("utterance"+x, listing);
        //    //listBox1.Items.Add("utterance"+x+" : "+tok);
        //}

        for (int w = 0; w < 4; w++)
        { 
        wording.Add(utterance[w],Tokens);
        }

    }
}

}

【讨论】:

    【解决方案2】:
    public string[] utterance = new string[4];
    
    Dictionary<string, List<string>> wording = new Dictionary<string, List<string>>();
    
    public void splitit()
    {          
     utterance[0] = "Fish attacked Nemo's parents";
     utterance[1] = "Only one fish egg left after fish attacked Nemo's parents and that was Nemo.";
     utterance[2] = "Nemo grow up and went to school.";
     utterance[3] = "Nemo got bored during the lecture and went to ocean with his friends.";
    
        for (int x=0; x < 4; x++)
        {
            wording.Add("utterance"+x,utterance[x].Split(' ').ToList());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多