我制作了一种使用 thesaurus.com 查找单词同义词的方法,我正在寻找对它的评论和反馈。我可以通过哪些方式改进它,无论是在速度、安全性、可靠性(无论依赖第三方网站进行查找有多“可靠”)等方面。
/// <summary>
/// This method relies heavily on thesaurus.com for synonym lookups. It is not completely reliable, but is deemed reliable enough in instances where you dont have your own thesaurus
/// </summary>
public static string[] GetSynonyms(string word)
{
string url = string.Format("http://thesaurus.com/search?q={0}", word);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
List<string> synonyms = new List<string>();
StringBuilder data = new StringBuilder();
string line;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//we know that the synonyms is in the upper-part of the html stream so we do not want to read the entire stream.
while((line = reader.ReadLine()) != null) {
var index = line.IndexOf("<span class=\"text\">");
if(index > 0)
{
index = index + "<span class=\"text\">".Length;
synonyms.Add(line.Substring(index).Replace("</span>", ""));
}
//break when we come to the Antonyms section of the page
if (line.Contains("container-info antonyms"))
{
break;
}
}
}
return synonyms.ToArray<string>();
}
else
{
return null;
}
}
编辑:例如,现在大约需要 3.5 秒才能找到单词“old”的同义词。