/// <summary> /// Attempts to remove a dictionary from the internal collection. /// </summary> /// <param name="dictionary">The dictionary to remove.</param> /// <returns>Whether the dictionary was successfully removed.</returns> public bool Remove(IRsgDictionary dictionary) { if (dictionary is null) { return(false); } return(dictionaryConfiguration.Dictionaries.Remove((RsgDictionary)dictionary)); }
private async Task <IEnumerable <string> > CreateWordListFromFile(IRsgDictionary dictionary, CancellationToken cancellationToken) { try { var wordList = await IOUtility.ReadLinesAsync(dictionary.WordListOptions.Source, dictionary.WordListOptions.Delimiter); return(wordList); } catch { return(Enumerable.Empty <string>()); } }
private async Task <IDictionary <int, string> > CreateAsyncInternal(IRsgDictionary dictionary, CancellationToken cancellationToken) { var wordDictionary = new Dictionary <int, string>(); var wordList = dictionary.WordListOptions.IsSourceLocal ? await CreateWordListFromFile(dictionary, cancellationToken) : await CreateWordListFromHttp(dictionary, cancellationToken); var index = 0; wordDictionary = wordList.ToDictionary(e => { return(index++); }); return(wordDictionary); }
/// <summary> /// Attempts to add a dictionary to the internal collection. /// </summary> /// <param name="dictionary">The dictionary to add.</param> /// <exception cref="ArgumentException">If the <paramref name="dictionary"/> is <see langword="null"/>.</exception> public void Add(IRsgDictionary dictionary) { if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } if (DoesDictionaryExist(dictionary.Name)) { throw new ArgumentException("Cannot add dictionary, name must be case insensitively unique!"); } dictionaryConfiguration.Dictionaries.Add((RsgDictionary)dictionary); }
private async Task <IEnumerable <string> > CreateWordListFromHttp(IRsgDictionary dictionary, CancellationToken cancellationToken) { try { var wordListAsString = await DownloadUtility.DownloadFileAsStringAsync(dictionary.WordListOptions.Source, cancellationToken); var wordList = wordListAsString.Split(dictionary.WordListOptions.Delimiter, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); return(wordList); } catch { return(Enumerable.Empty <string>()); } }
/// <summary> /// Creates a sequence of words from an <see cref="IRsgDictionary"/>. /// <para>Returns an empty sequence if unable to read/download dictionary from /// the source.</para> /// </summary> /// <param name="dictionary">A contract representing a <see cref="IRsgDictionary"/>.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel this task.</param> /// <returns>A new <see cref="IEnumerable{string}"/> containing the new word list.</returns> public async Task <IDictionary <int, string> > CreateWordListAsync(IRsgDictionary dictionary, CancellationToken cancellationToken) { return(await CreateAsyncInternal(dictionary, cancellationToken)); }