Пример #1
0
        public async Task <ParsedSentence> ParseSentenceAsync(string sentence)
        {
            using (var client = _httpClientFactory.CreateClient())
            {
                var ls = _connectionStringProvider.GetString("languageService");
                if (string.IsNullOrEmpty(ls))
                {
                    ls = "http://localhost:5000";
                }
                var qs = new StringBuilder("properties=")
                         .Append(WebUtility.UrlEncode(JsonConvert.SerializeObject(new
                {
                    annotators = "tokenize,ssplit,pos,ner,depparse,openie,sentiment"
                }))).Append("&pipelineLanguage=en");
                var uriBuilder = new UriBuilder(ls);
                uriBuilder.Query = qs.ToString();
                var message = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri);
                message.Content = new FormUrlEncodedContent(new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>(string.Empty, sentence)
                });
                try
                {
                    var response = await client.SendAsync(message);

                    var content = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        var entity = JObject.Parse(content);

                        return(ParsedSentence.Build(entity));
                    }
                }
                catch
                {
                }

                return(new ParsedSentence());
            }
        }