public void KeywordValidator_passRightAndWringStrings_validateResults() { var sut = new KeywordValidator(); Assert.True(sut.Validate("word"), "Simple word should pass"); Assert.True(sut.Validate("Word"), "Words with different cases should pass"); Assert.True(sut.Validate("PhD"), "Words such as abbreviations should pass"); Assert.True(sut.Validate("One two three"), "Composed words acceptable"); Assert.True(sut.Validate("français"), "All UTF8 word symbols should pass"); Assert.True(sut.Validate("français русский übersetzen"), "All UTF8 word symbols should pass"); Assert.True(sut.Validate("dad's"), "' should pass"); Assert.True(sut.Validate("ABC."), "dot is acceptable"); Assert.True(sut.Validate("data-structure"), "dash is ok"); Assert.True(sut.Validate("1"), "Numbers are ok You can find something like 3-d say in MOBY examples"); Assert.True(sut.Validate("test1"), "Numbers are ok You can find something like 3-d say in MOBY examples"); Assert.True(sut.Validate("$100-a-plate dinner"), "Numbers are ok You can find something like $100-a-plate dinner say in MOBY examples"); Assert.False(sut.Validate("five%"), "% is not a word symbol"); Assert.False(sut.Validate("#1"), "# is not a word symbol"); Assert.False(sut.Validate("synonym #4"), "# is not a word symbol"); Assert.False(sut.Validate("contains\n"), "terminal symbols should rise error"); Assert.False(sut.Validate("\n"), "terminal symbols should rise error"); Assert.False(sut.Validate("False!"), "Sentence marks are not ok"); Assert.False(sut.Validate("True?"), "Sentence marks are not ok"); }
/// <summary> /// Reads file with synonyms from Ripicts format (word, syn1;raiing1,syn2;raiting2 etc.) /// </summary> /// <param name="filename">The filename.</param> /// <param name="datastructure">The in memory storage structure.</param> /// <exception cref="System.ArgumentNullException">data structure must me initialized and could not be null</exception> /// <exception cref="System.IO.FileNotFoundException">Passed filename with keywords not found.</exception> /// <exception cref="System.IO.IOException">Failed to read file.</exception> public void ReadFile(string filename, ref IinMemoryStorageStructure datastructure) { if (datastructure == null) { throw new ArgumentNullException("datastructure must me initialized and could not be null"); } if (!File.Exists(filename)) { throw new FileNotFoundException("Passed filename with keywords not found.", filename); } int stringCounter = 0; using (StreamReader reader = new System.IO.StreamReader(filename)) { KeywordValidator validator = new KeywordValidator(); while (!reader.EndOfStream) { stringCounter++; var input = reader.ReadLine(); // empty string ignore them if (!input.Trim().Equals(string.Empty)) { string keyword; var synRelevanceCollection = new List<Tuple<string, int>>(); if (input.IndexOf(',') > 0) { keyword = input.Substring(0, input.IndexOf(",")); var collection = input.Substring(input.IndexOf(",") + 1).Trim().Split(',').Select(s => s.Trim()).ToArray(); for (int i = 0; i < collection.Length; i++) { var element = collection[i]; var pair = element.Trim().Split(';'); if (pair.Length > 0 && validator.Validate(pair[0])) { var synvalue = pair[0].Trim(); int relValue = 0; if (pair.Length > 1) { int.TryParse(pair[1], out relValue); } if (!synvalue.Equals(string.Empty)) { synRelevanceCollection.Add(new Tuple<string, int>(synvalue, relValue)); } } else { throw new FormatException(string.Format("Format on the line {0} is not right. Missing ';' symbol or value '{1}' illegal.", stringCounter, element)); } } } else { // means that this is just a word without synonyms; keyword = input.Trim(); } try { datastructure.AddSynonyms(keyword, synRelevanceCollection.ToArray()); } catch (Exception ex) { throw new Exception(string.Format("Format on the line {0}.", stringCounter), ex); } } } } }