【发布时间】:2019-05-21 16:38:27
【问题描述】:
我有一个程序可以从用户那里获取 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);
}
【问题讨论】: