2013-08-08 6 views
2

저는 약간의 의미에서 API로만 작업 했으므로 잠시 동안이 방법을 시험해보고 싶었습니다. 좋아, 이것이 내가 지금까지 가지고있는 것과 작동하지만 정의의 모든 것을 반환한다. 그래서 몇 가지 질문이 있습니다.Wordnik API를 사용하여 정의 요청하기

  1. 다른 정의없이 정의 만 요청할 수있는 방법이 있습니까?
  2. 데이터를 구문 분석합니까? Wordnik API에서 XML 태그를 포함 할 수 있습니다 ... XMLReader를 사용하여 정의를 가져올 수 있습니까?
  3. 이제는 두 정의를 모두 요청하는 방법과 한 번에 명사/동사/기타 일 경우 어떻게해야합니까?

궁극적 인 목표는 내가 할 수있는 정의 목록을 만드는 것입니다. 어떤 도움이라도 대단히 감사하겠습니다. 여기 내 코드는 지금까지의 :

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")); 
    } 
} 

덕분에, 저스틴

답변

1
  1. API 설명서는 아마 그에게 말할 것이다.
  2. 예, 데이터를 구문 분석하십시오. 데이터가 XML로 내려 오면 XMLReader를 사용하여 데이터를 파싱하거나 XMLReader를 XMLDocument에로드 할 수 있습니다. JSON을 요구하는 것 같습니다. 그렇다면 JSON 파서가 필요합니다. Json.Net을 확인하십시오.
  3. 다시 API 문서를 확인하십시오.

문서 페이지가 의심스러운 것으로 드뭅니다. Google 그룹 또는 support page에 나열된 다른 출처 중 하나에서 더 잘 반응 할 것입니다.

1

다음은 단어에 대한 정의를 가져 오는 예제입니다. api 키를 자신의 api 키로 대체해야합니다.

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);  

    } 
} 
관련 문제