iciba.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. namespace TxtDictionary
  7. {
  8. public class iciba
  9. {
  10. WebClient wc = new WebClient();
  11. string urlBase = "http://dict-co.iciba.com/api/dictionary.php?key=EBCFD059A10094F790FD6A3F56E3CA6A&w=";
  12. public Word QueryWord(string wordText)
  13. {
  14. var data = wc.DownloadData(urlBase + wordText);
  15. var htmlStr = Encoding.UTF8.GetString(data);
  16. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  17. doc.LoadHtml(htmlStr);
  18. //var word = doc.DocumentNode.SelectNodes("dict/key")[0].InnerText;
  19. List<Meaning> lm = new List<Meaning>();
  20. string en, us;
  21. try
  22. {
  23. en = "["+doc.DocumentNode.SelectNodes("dict/ps")[0].InnerText + "]";
  24. }
  25. catch
  26. {
  27. en = "";
  28. }
  29. try
  30. {
  31. us = "[" + doc.DocumentNode.SelectNodes("dict/ps")[1].InnerText + "]";
  32. }
  33. catch
  34. {
  35. us = "";
  36. }
  37. List<CixingChinese> cixings = new List<CixingChinese>();
  38. CixingChinese cixing = new CixingChinese();
  39. int counts = 0;
  40. try
  41. {
  42. counts = doc.DocumentNode.SelectNodes("dict/pos").Count;
  43. for (int i = 0; i < counts; i++)
  44. {
  45. cixing = new CixingChinese();
  46. cixing.Cixing = doc.DocumentNode.SelectNodes("dict/pos")[i].InnerText;
  47. cixing.Cixing = cixing.Cixing.Replace("&amp;", "&");
  48. cixing.Chinese = doc.DocumentNode.SelectNodes("dict/acceptation")[i].InnerText;
  49. cixing.Chinese = cixing.Chinese.Replace("&lt;", "<").Replace("&gt;", ">");
  50. cixings.Add(cixing);
  51. }
  52. }
  53. catch
  54. {
  55. return null;
  56. cixing.Chinese = "";
  57. cixing.Cixing = "";
  58. }
  59. Meaning meaning = new Meaning(en, us, cixings);
  60. lm.Add(meaning);
  61. Word wordToReturn = new Word(wordText, lm);
  62. return wordToReturn;
  63. }
  64. }
  65. }