我在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 来向我展示最小和最长的发布列表以及 .keyValuePair 的Class InvertedIndexTable { }
数量Dictionary<string,List<string>> index
。它可以正常工作,没有任何错误和异常,但问题是: 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));
}
`
请让我知道是否有任何方法可以达到我的预期结果。我需要的结果是最短和最长List<string>
的大小,我认为我为和方法Dictionary index
编写了正确的代码,但似乎我错了,请告诉我这两种方法有什么问题?为什么他们总是返回相同的值???为什么这个值是“1”,总是???GetSmallestPosting()
GetLongestPosting()
顺便说一句,GetSmallestPosting()
找到最短List<string>
的Dictionary<string,List<string>> index
并GetLongestPosting()
找到最长的。
谢谢你的时间。