我有一个程序,可以从用户那里获取url,并在网站上搜索最常用的词。
public void url_input_Click(Object sender, EventArgs e)
{
string StringFromTheInput = TextBox1.Text;
var request_ = (HttpWebRequest)WebRequest.Create(StringFromTheInput);
WebResponse response = request_.GetResponse();
Stream data = response.GetResponseStream();
string content = String.Empty;
using (var client = new WebClient())
{
content= client.DownloadString(StringFromTheInput);
}
WordCount(content);
}
public static Dictionary<string, int> WordCount(string content, int numWords = int.MaxValue)
{
var delimiterChars = new char[] { ' ', ',', ':', '\t', '\"', '\r', '{', '}', '[', ']', '=', '/' };
return content
.Split(delimiterChars)
.Where(x => x.Length > 0)
.Select(x => x.ToLower())
.GroupBy(x => x)
.Select(x => new { Word = x.Key, Count = x.Count() })
.OrderByDescending(x => x.Count)
.Take(numWords)
.ToDictionary(x => x.Word, x => x.Count);
}
答案 0 :(得分:0)
我已经测试了解决方案,并且效果很好。我确实尝试了http://google.com URL。
调试器不会跳过LINQ行,它会执行它,然后通过移至方法的末尾而返回。
我建议将返回结果添加到变量中,并在最后一个括号中添加一个断点。
var result = WordCount(content);
} // put a break point here