【问题标题】:How to Get Shortest/Longest Posting Lists如何获得最短/最长的发布列表
【发布时间】:2013-05-02 09:49:57
【问题描述】:

我在这里写了Class InvertedIndexTable { }

public interface IInvertedIndex
{
    int IndexSize(string path);
    void Load(string path);
}
class InvertedIndexTable : IInvertedIndex
{
     Dictionary<string, List<string>> index = new Dictionary<string, List<string>>();
     CreateMatrix r = new CreateMatrix(); // an object of another class contains stopwords{A,AN,...}
                                          // and also contains RemoveStopword() method
     public HashSet<string> DistincTerms = new HashSet<string>();
     public List<string> filesCollection = new List<string>();
     public int IndexSize(string pa)
     {
         Load(pa);
         return index.Count;
     }
     public void Load(string path)
      {
          string[] filePaths = Directory.GetFiles(Path.GetFullPath(path));
          foreach (string file in filePaths)
          {
              string contents = File.ReadAllText(file);
              contents = RemoveNonAlphaChars(contents);
              String[] tokensCollection = r.RemoveStopsWords(contents.ToUpper().Split(' '));
              foreach (string token in tokensCollection)
              {
                  if (!r.booleanOperator.Contains(token) && !DistincTerms.Contains(token))
                  {
                      DistincTerms.Add(token);
                  }
              }
          }
          Frequenty(filePaths);
      }
     public void Frequenty(string[] path1)
      {
        foreach (string d in DistincTerms)
        {
            foreach (string f in path1)
            {
                if (File.ReadAllText(f).Contains(d))
                {
                    filesCollection.Add(f);
                }

            }
            index.Add(d, filesCollection);
          }
      }
     private string RemoveNonAlphaChars(string content)
      {
          StringBuilder sb = new StringBuilder();

          foreach (char c in content.ToCharArray())
          {
              if (char.IsLetter(c) || char.IsSeparator(c))
              {
                  sb.Append(c);
              }
          }
          return sb.ToString();
      }
     public  string GetSmallestPosting(string p)
      {
          List<int> numbers = new List<int>();
          if (index != null)
          {
              foreach( KeyValuePair<string,List<string>> i in index)
              {
                  string content= i.Value.ToString();
                  String[] itemsList = content.ToUpper().Split(' ');
                  numbers.Add(itemsList.Length); 
              }

              return numbers.Min().ToString();
          }
          return null;
      }
     public string GetLongestPosting(string p)
      {
          List<int> numbers = new List<int>();
          if (index != null)
          {
              foreach (KeyValuePair<string, List<string>> i in index)
              {

                  string content = i.Value.ToString();
                  String[] itemsList = content.ToUpper().Split(' ');
                  numbers.Add(itemsList.Count());
              }
              return numbers.Max().ToString(); 
          }
          return null;
      }
}

我将准备 button6 来显示 Class InvertedIndexTable { } 的最小和最长发布列表以及 Dictionary&lt;string,List&lt;string&gt;&gt; index 的 KeyValuePair 的数量。 它可以正常工作,没有任何错误和异常,但问题是:DictionaryPairsNumbers 的返回值是正确的,但 MinSizePosting 和 MaxSizePosting 的返回值是错误的,代码总是为它们返回值“1”。为什么?怎么了?

我为 button6 编写的代码就在这里:

    `  InvertedIndexTable i = new InvertedIndexTabe(); 
    private void button5_Click(object sender, EventArgs e)
    {
     MessageBox.Show("DictionaryPairsNumbers: " + i.IndexSize(textBox1.Text)+"\n\rMaxSizePosting: " + i.GetLongestPosting(textBox1.Text)+"\n\rMinSizePosting: "+ i.GetSmallestPosting(textBox1.Text));
    }
    `

请告诉我是否有任何方法可以达到我的预期结果。 我需要的结果是Dictionary index 中最短和最长List&lt;string&gt; 的大小我以为我为GetSmallestPosting()GetLongestPosting() 方法编写了正确的代码,但似乎我错了,请告诉我这两种方法有什么问题?为什么他们总是返回相同的值???为什么这个值是“1”,总是???

顺便说一句,GetSmallestPosting() 找到最短的List&lt;string&gt;of Dictionary&lt;string,List&lt;string&gt;&gt; indexGetLongestPosting() 找到最长的。

感谢您的宝贵时间。

【问题讨论】:

  • 它不起作用,因为您无法获得Collection&lt;List&lt;int&gt;&gt; 的最小值,这就是您所写的。 “最小张贴”究竟是什么意思?您是否尝试找到List&lt;int&gt; 的元素较少的元素?
  • @ppetrov 我已经完全编辑了问题,现在它可以工作了,但有两个结果与我预期的不一样。 GetSmallestPosting() 和 GetLongestPosting 似乎都有问题,但我不知道它是什么。有什么帮助或解释吗?
  • @ppetrov,请检查整个问题。我完全改变了代码
  • @Olivia:从一开始到现在,您最初的问题似乎发生了很大变化。我认为如果您发布一个问题并等待答案然后发布一个新问题而不是更改您最初的问题,这对每个人来说都会容易得多。
  • @wonko79 对此我感到非常抱歉,但我无法提出新问题,每次单击按钮时都会收到此消息“抱歉,我们不再接受来自此帐户的问题。”,似乎站点管理员阻止了我,我不知道如何解决它无法在聊天室中与任何人交谈,因为我没有足够的声誉。所以我只是改变我的老问题,直到我的问题得到解决。

标签: c# winforms information-retrieval


【解决方案1】:

首先我改变了Frequenty() { }方法,应该是这样的

public void Frequenty(string[] path1)
      {
         List<string> filesCollection = new List<string>();
         for (int i = 0; i < DistincTerms.Count(); i++ )
         {
             string d = DistincTerms.ElementAt(i);
             foreach (string f in path1)
             {
                 string c = File.ReadAllText(f);
                 c = r.RemoveNonAlphaChars(c);
                 String[] T = r.RemoveStopsWords(c.ToUpper().Split(' '));
                 foreach (string term in T)
                 {
                     if (term.Equals(d) && !filesCollection.Contains(f))
                     {
                         filesCollection.Add(f);
                     }
                 }

             }
             countor.Add(filesCollection.Count);
             index.Add(d, countor);
             filesCollection.Clear();
         }

现在,我已经打开了 GetSmallest/LongestPostingList 两种方法:

 public string GetSmallestPosting(string p)
      {
          if (index != null)
          {
            return countor.Min().ToString();
          }
          return null;
      }
     public string GetLongestPosting(string p)
      {
          if (index != null)
          {
              return countor.Max().ToString();
          }
          return null;
      }

它有效。我已经测试过了。

【讨论】:

  • 而不是 numbers.Add(count) 写 numbers.Add(pair.Vaue.Count) 并删除不必要的代码。或移动 int count = 0;向下 2 行。
  • @wonko79 我做到了,但它仍然返回相同的值,它必须返回 1 作为帖子的最小大小,但它返回 4
  • @wonko79 在 Dictionary> 索引被某个键/值对填充的情况下,Frequency() 方法可能有问题。我想尽办法找出它是什么,但在我的代码中找不到任何错误的行,这是怎么回事?
  • @wonko79 哦,我改变了你所说的方法,但现在代码为 Min 和 Max 发布列表返回相同的值,并且这个值都是 5。
  • @wonko79 非常感谢你帮助我,向我解释,最后我得到了答案。
【解决方案2】:

您可以使用 Linq 来执行此操作。

向 InvertedIndex 类添加两个新方法。

Min 遍历字典中的所有键 (X) 值 (List) 对,并返回项目数最少的列表。 Max 正好相反。

public List<T> GetSmallestPosting()
{
    if(_Index!=null)
       return  _Index.Values.First(v => v.Count == _Index.Min(kv => kv.Value.Count)).ToList();

    return null;
}

public List<T> GetLongestPosting()
{
    if(_Index!=null)
      return   _Index.Values.First(v => v.Count == _Index.Max(kv => kv.Value.Count)).ToList();

    return null;
}

【讨论】:

  • 你能解释一下吗?它到底是做什么的?
  • 我试过了,但我遇到了这样的异常:“空引用异常未处理”......我应该怎么做?在哪里?我在 Visual C# 的 windows 应用程序模式下尝试它,当我单击按钮时,我必须在消息框中看到最短和最长发布列表的大小作为结果。我每次都得到那个异常,怎么了?
  • 哦,我刚才看到了。我的代码假设 Value(List)永远不会为空。
  • 是的,很抱歉这个错误。相应地编辑了我的答案。
  • 输出对我来说是这样的:'MinSizePosting: System.Collections.Generic.List'1[System.String]' 但我需要一个 int,或者至少是一个真正的 List ,而不是类似的东西,所以请告诉我有什么问题?
猜你喜欢
  • 2019-11-02
  • 2020-11-27
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多