【发布时间】: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<string,List<string>> 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<string> 的大小我以为我为GetSmallestPosting() 和GetLongestPosting() 方法编写了正确的代码,但似乎我错了,请告诉我这两种方法有什么问题?为什么他们总是返回相同的值???为什么这个值是“1”,总是???
顺便说一句,GetSmallestPosting() 找到最短的List<string>of Dictionary<string,List<string>> index 和 GetLongestPosting() 找到最长的。
感谢您的宝贵时间。
【问题讨论】:
-
它不起作用,因为您无法获得
Collection<List<int>>的最小值,这就是您所写的。 “最小张贴”究竟是什么意思?您是否尝试找到List<int>的元素较少的元素? -
@ppetrov 我已经完全编辑了问题,现在它可以工作了,但有两个结果与我预期的不一样。 GetSmallestPosting() 和 GetLongestPosting 似乎都有问题,但我不知道它是什么。有什么帮助或解释吗?
-
@ppetrov,请检查整个问题。我完全改变了代码
-
@Olivia:从一开始到现在,您最初的问题似乎发生了很大变化。我认为如果您发布一个问题并等待答案然后发布一个新问题而不是更改您最初的问题,这对每个人来说都会容易得多。
-
@wonko79 对此我感到非常抱歉,但我无法提出新问题,每次单击按钮时都会收到此消息“抱歉,我们不再接受来自此帐户的问题。”,似乎站点管理员阻止了我,我不知道如何解决它无法在聊天室中与任何人交谈,因为我没有足够的声誉。所以我只是改变我的老问题,直到我的问题得到解决。
标签: c# winforms information-retrieval