public HttpResponseMessage GetWordDescriptionFromWiki(string keyWord) { try { var strippedWord = WordFilterTool.RemoveSpecialCharacters(keyWord); var cleanWordResult = WordFilterTool.RemoveAccentOnVowels(strippedWord); var urlToSearch = string.Format( "http://es.wikipedia.org/w/index.php?action=render&title={0}&prop=revisions&rvprop=content", cleanWordResult); string encodedJsonResult; using (var webClient = new WebClient()) { var jsonResult = webClient.DownloadString(urlToSearch); byte[] bytes = Encoding.Default.GetBytes(jsonResult); encodedJsonResult = Encoding.UTF8.GetString(bytes); } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, encodedJsonResult)); } catch (Exception) { return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, "<p>No encontre resultados para esta palabra</p> ")); } }
public HttpResponseMessage GetWordDescriptionFromRae(string keyWord) { try { var strippedWord = WordFilterTool.RemoveSpecialCharacters(keyWord); var cleanWordResult = WordFilterTool.RemoveAccentOnVowels(strippedWord); var urlToSearch = string.Format("http://lema.rae.es/drae/srv/search?val={0}", cleanWordResult); return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, urlToSearch)); } catch (Exception) { return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid words")); } }
public HttpResponseMessage GetAllWordsInDictionary() { var wordList = this.aWordService.GetAllWords(); if (!wordList.Any()) { return(this.ControllerContext.Request.CreateResponse( HttpStatusCode.BadRequest, "There is no words in dictionary")); } foreach (var aWord in wordList) { aWord.Image = WordFilterTool.CheckUrlIsAValidImageInWord(aWord); } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, wordList)); }
public async Task <HttpResponseMessage> AddPhrase(string listOfWords, int dictionaryId) { var errorMessage = new StringBuilder(string.Empty); var listOfNotAcceptedWords = new List <string>(); const string Delimiter = " "; if (listOfWords.Length == 0) { return(this.ControllerContext.Request.CreateResponse( HttpStatusCode.BadRequest, "Invalid parameters, Please check there is elements in array")); } var language = this.aDictionaryService.GetADictionary(dictionaryId).DictionaryName; var wordList = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(listOfWords, typeof(List <WordDto>)); var deserializeWordList = wordList.ToObject <List <WordDto> >(); var verifiedWordList = WordFilterTool.GetListWithValidWordName(deserializeWordList); try { var listNoRepeatedElements = WordFilterTool.ListNoRepeatedElements(verifiedWordList); var pathToDictionary = HttpContext.Current.Server.MapPath(string.Format("{0}{1}.txt", ConfigurationManager.AppSettings["Dictionary"], language)); WordFilterTool.GetWordsNotAccepted( listNoRepeatedElements, language, pathToDictionary, out listOfNotAcceptedWords); WordFilterTool.AddExtraInformationWordList(listNoRepeatedElements); await this.aWordService.BulkInsertOfWords(listNoRepeatedElements); if (verifiedWordList.Count == 1) { return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK)); } var aPhrase = this.PreparePhraseToAdd(dictionaryId, verifiedWordList, Delimiter); this.aGenericPhraseService.InsertPhrase(aPhrase); return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, wordList)); } catch (DuplicateKeyException) { var listRepeatedWords = this.aWordService.GetRepeatedWords(verifiedWordList); errorMessage.Append(string.Format( "Cannot insert duplicate words: {0}", string.Join(",", listRepeatedWords))); } catch (FormatException) { foreach (var aWord in deserializeWordList) { foreach (var notAcceptedWord in listOfNotAcceptedWords) { if (aWord.WordName == notAcceptedWord) { aWord.Repeated = true; } } } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, wordList)); } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, errorMessage.ToString())); }
public async Task <HttpResponseMessage> AddPhrase(string listOfWords, int dictionaryId) { var errorMessage = new StringBuilder(string.Empty); var listOfNotAcceptedWords = new List <string>(); const string Delimiter = " "; if (listOfWords.Length == 0) { return(this.ControllerContext.Request.CreateResponse( HttpStatusCode.BadRequest, "Invalid parameters, Please check there is elements in array")); } var language = this.aDictionaryService.GetADictionary(dictionaryId).DictionaryName; var wordList = Json.Deserialize <List <WordDto> >(listOfWords); var verifiedWordList = WordFilterTool.GetListWithValidWordName(wordList); verifiedWordList.Select(c => { c.CreationTime = DateTime.Now; c.WordDescription = string.IsNullOrEmpty(c.WordDescription) ? "No description" : c.WordDescription; return(c); }).ToList(); try { var listNoRepeatedElements = WordFilterTool.ListNoRepeatedElements(verifiedWordList); var pathToDictionary = HttpContext.Current.Server.MapPath(string.Format("{0}{1}.txt", ConfigurationManager.AppSettings["Dictionary"], language)); WordFilterTool.GetWordsNotAccepted( listNoRepeatedElements, language, pathToDictionary, out listOfNotAcceptedWords); foreach (var aWord in listNoRepeatedElements) { aWord.Image = CheckUrlIsAValidImage(aWord); } await this.aWordService.BulkInsertOfWords(listNoRepeatedElements); if (verifiedWordList.Count == 1) { return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK)); } var sentence = verifiedWordList.Select(i => i.WordName).Aggregate((i, j) => i + Delimiter + j); var listOfWordsId = await this.aWordService.GetIdOfWords(verifiedWordList); var aPhrase = new PhraseDto { PhraseText = sentence, CreationTime = DateTime.Now, WordsIds = string.Join(",", listOfWordsId), AssignedDictionaryId = dictionaryId, UserName = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().Username }; this.aGenericPhraseService.InsertPhrase(aPhrase); return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, wordList)); } catch (DuplicateKeyException) { var listRepeatedWords = this.aWordService.GetRepeatedWords(verifiedWordList); errorMessage.Append(string.Format( "Cannot insert duplicate words: {0}", string.Join(",", listRepeatedWords))); } catch (FormatException) { foreach (var aWord in wordList) { foreach (var notAcceptedWord in listOfNotAcceptedWords) { if (aWord.WordName == notAcceptedWord) { aWord.Repeated = true; } } } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.OK, wordList)); } return(this.ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, errorMessage.ToString())); }