请求定义使用Wordnik API [英] Requesting Definitions Using the Wordnik API

查看:253
本文介绍了请求定义使用Wordnik API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只在一个很小的感觉,所以我一直想尝试一下如何做到这一点了一段时间曾与API的。好了,所以这是我迄今为止,它的工作原理,但它返回的定义的一切。所以,我有几个问题:

I've only worked with APIs in a very minimal sense so I've been wanting to try out how to do this for some time. Ok so this is what I have so far and it works but it returns everything of the definition. So I have a few questions:

  1. 有没有办法要求只是没有别的?
  2. 的定义
  3. 请我只是分析数据?我看到Wordnik API中,我可以包括XML标签......所以我可以用一个XMLReader抢的定义?
  4. 现在,怎么样,要求这两个定义,如果是名词/动词的/ etc一次?

最终目标是创建的定义,我可以做的东西有一个列表。任何帮助将大大AP preciated。这是我的code到目前为止:

The ultimate goal would be to create a list of definitions that I could do stuff with. Any help would be greatly appreciated. Here's my code so far:

class Program
{

    static void Main(string[] args)
    {
        string apiKey = "***************";
        string wordToSearch = "";

        do
         {
            Console.Write("Please type a word to get the definition: ");
            wordToSearch = Console.ReadLine();

            if (!wordToSearch.Equals("q"))
            {
                string url = "http://api.wordnik.com/v4/word.json/" + wordToSearch + "/definitions?api_key=" + apiKey;
                WebRequest request = WebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "application/json";

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        string responseFromWordnik = reader.ReadToEnd();
                        Console.WriteLine(responseFromWordnik);
                    }
                }
            }

        } while (!wordToSearch.Equals("q"));
    }
}

谢谢, 贾斯汀

thanks, Justin

推荐答案

下面是一个例子,以得到一个字的定义。您需要替换的API密钥用自己的API密钥。

Here's an example to get definitions for a word. You need to replace the api key with your own api key.

public class Word
{
    public string word { get; set; }
    public string sourceDictionary { get; set; }
    public string partOfSpeech { get; set; }
    public string text { get; set; }
}

public class WordList
{
    public List<Word> wordList { get; set; }
}


string url = "http://api.wordnik.com:80/v4/word.json/" + word + "/definitions?limit=200&includeRelated=false&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5";

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Accept = "application/json";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
webRequest.Referer = "http://developer.wordnik.com/docs.html";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
webRequest.Host = "api.wordnik.com";

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

string enc = webResponse.ContentEncoding;
using (Stream stream = webResponse.GetResponseStream())
{
    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
    String responseString = "{\"wordList\":" + reader.ReadToEnd() + "}";

    if (responseString != null)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        WordList words = ser.Deserialize<WordList>(responseString);    

    }
}

这篇关于请求定义使用Wordnik API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆