示例#1
0
    public IEnumerator GetSynonymsForWord(string word, Word result, System.Func <Word, Word> OnComplete /*OnSynonymCompleteDelegate onSynonymComplete*/)
    {
        //Word result = new Word();
        result.isDone     = false;
        result.WordString = word;
        string requestStr = GetRequestString(word);

        WWW returnedQuery = GET(requestStr);

#if DEBUG
        int count = 0;
#endif
        while (!returnedQuery.isDone)
        {
            yield return(null);

#if DEBUG
            if (count > 300)
            {
                Debug.LogError("GET request timed out");
            }
            else
            {
                count++;
            }
#endif
        }//do nothing while it's processing

        // HANDLE ERRORS HERE!
        if (returnedQuery.error != null)
        {
            if (returnedQuery.error.ToLower().Contains("404 not found"))
            {
                result.AddWord("NoData");
                result.WordString = "NoData";
            }
        }
        else
        {
            string jsonString = returnedQuery.text; //for now we only support JSON

            var N = JSON.Parse(jsonString);

            //JSON parsing//
            JSONArray arr;
            string[]  typesOfSpeech = { "verb", "adverb", "noun", "adjective" };
            foreach (string key in typesOfSpeech)
            {
                if (N[key] != null)
                {
                    if (N[key]["syn"] != null)
                    {
                        arr = N[key]["syn"].AsArray;
                        foreach (JSONNode str in arr)
                        {
                            //Only add the word if it doesn't contain a space
                            if (!str.Value.Contains(" "))
                            {
                                string sanString = Utility.SanitizeString(str.Value);
                                result.AddWord(sanString);
                            }
                        }
                    }
                }
            }
        }


        result.isDone = true;
        //return result;

        //run the delegate passed!
        // onSynonymComplete();
        OnComplete(result);
    }