Exemplo n.º 1
0
        /// <summary>
        /// Use wordnik to show online sample usages only, the translation provided by wordnik doesn't seem to be good quality (missing many things)
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public async Task <TranslationResult> Translate(string phrase)
        {
            //use below only if provider supports multiple langiages...
            Language sourceLang = Languages.SelectedTranslation.SourceLang;
            Language targetLang = Languages.SelectedTranslation.TargetLang;


            //List<string> lst = new List<string> { "bsss", "kuku", "abe" };
            //bool supportMultiLang = !AvailableLanguages.Any();
            //check if passed source lang(static) is in the supported lang list, if not, choose default pair from the list (first and second in the list)
            //or skip that part if there are no support for multiple languages
            //we assume that there can be any pair of supported (source - target languages), only restriction is both must be supported
            if (AvailableLanguages.Any())
            {
                if (!AvailableLanguages.Contains(sourceLang))
                {
                    sourceLang = AvailableLanguages.FirstOrDefault <Language>();
                }
                if (!AvailableLanguages.Contains(targetLang))
                {
                    //TODO: check if the list contain 2 items, if not then take the first one
                    targetLang = AvailableLanguages[1];
                }
            }



            string APIKey = settings.APIKeys.WordnikAPIKey;
            string req    = $"https://api.wordnik.com/v4/word.json/{phrase}/examples?includeDuplicates=false&useCanonical=false&limit={limit}&api_key={APIKey}";

            //wordnik swagger
            //https:\/\/developer.wordnik.com\/api-docs\/swagger.json
            //wordnik get word definitions

            var client = new RestClient(req);

            var request = new RestRequest();

            request.Method = Method.GET;



            var response = await client.ExecuteAsync(request);

            Task <IRestResponse> t = client.ExecuteAsync(request);


            var content = JsonConvert.DeserializeObject <JToken>(response.Content);



            translationResultRaw = response.StatusCode == HttpStatusCode.OK ? content.ToObject <WordnikAPIResponseRaw>() : null;



            //CONVERT TO FINAL RESPONSE
            return(mapResponse());
        }
Exemplo n.º 2
0
        public async Task <TranslationResult> Translate(string phrase)
        {
            var jt = JToken.Parse(response.GetWordnikAPIMockResponse()).ToString();

            translationResultRaw = JsonConvert.DeserializeObject <JToken>(JToken.Parse(response.GetWordnikAPIMockResponse()).ToString())
                                   .ToObject <WordnikAPIResponseRaw>();


            //CONVERT TO FINAL RESPONSE
            return(mapResponse());
        }